java - JFrame 相互重叠

标签 java swing user-interface

我正在使用 java 中的 JFrame 来制作 GUI。我遇到了一个问题,我的 2 个 JFrame 相互重叠。

public VidbergGUI() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
    super("Automatic Output Verifier");
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    setBounds(100, 100, 600, 400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container con = this.getContentPane();
    con.add(titlePane);

    titlePane.setLayout(new BoxLayout(titlePane, BoxLayout.PAGE_AXIS));
    componentPane.setLayout(new BoxLayout(componentPane, BoxLayout.LINE_AXIS));

    programsLoaded = new JTable(data, columnNames) {
        @Override
        public boolean isCellEditable(int row, int col) {
            if (col == 3) return false;
            return true;
        }
    };
    programsLoaded.getColumnModel().getColumn(2).setCellEditor(new FileChooserEditor());

    tableHolder = new JScrollPane(programsLoaded);

    titleLabel.setFont(new Font("Ariel", Font.BOLD, 28));

    addButton.setSize(40, 40);
    removeButton.setSize(40, 40);

    titlePane.add(titleLabel, BorderLayout.PAGE_START);
    con.add(componentPane);
    componentPane.add(tableHolder, BorderLayout.LINE_END);
    componentPane.add(addButton, BorderLayout.EAST);
    componentPane.add(removeButton, BorderLayout.EAST);
    setVisible(true); // make frame visible
}

通过此设置,只有 componentPane 可见。如果我注释掉 con.add(componentPane),则只有 titlePane 可见。有没有办法可以分配某种布局,以便 2 个框架垂直堆叠?

最佳答案

您确实了解 BorderLayout 只能在其 5 个可用插槽中的每个插槽中布局单个组件吗?可能不是...

创建第三个 JPanel 并向其中添加 titlePanecomponentPane,然后将其添加到 CENTER 位置BorderLayout

您可以为此面板使用 GridLayoutGridBagLayout...

JPanel centerPane = new JPanel(new GridLayout(2, 1));
centerPane.add(titlePane);
centerPane.add(componentPane);
con.add(centerPane);

或者只是将 titlePane 添加到 NORTH 位置...

con.add(titlePane, BorderLayout.NORTH);

关于java - JFrame 相互重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26900608/

相关文章:

java - 将最终变量传递给 MouseListener 方法并返回结果

java - 在程序启动时清除java swing表单的所有文本字段

cocoa - Cocoa 中的 iTunes 10 风格底部栏按钮

java - ye olde "org.hibernate.NonUniqueObjectException"...我被这个难住了

java - 汽车/移动 PC 平台是否需要基于触摸屏的 UI?

java - 除非选择单元格,否则 JTable 数据是不可见的

java - 仅最后一个 JPanel ActionListener 工作

java - 如何在 Java 中的两个 GUI 之间使用相同的数据?

java - 对泛型类的引用的比较

java - 将数据插入数据库 - SQLException