我正在使用 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
并向其中添加 titlePane
和 componentPane
,然后将其添加到 CENTER
位置BorderLayout
您可以为此面板使用 GridLayout
或 GridBagLayout
...
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/