【发布时间】:2016-05-09 22:02:22
【问题描述】:
我在一个框架中有以下结构:
Frame->contentPane 面板->topPanel 面板->table
这是代码:
private JPanel contentPane;
private JTable table;
private JPanel topPanel;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ShowList frame = new ShowList();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ShowList() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 675, 433);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
topPanel = new JPanel();
topPanel.setBounds(76, 76, 491, 245);
contentPane.add(topPanel);
topPanel.setLayout(null);
table = new JTable();
table.setBounds(0, 0, 491, 245);
topPanel.add(table);
}
我想要实现的是,当我调整框架的大小使窗口变大时,topPanel 和它包含的表格也会调整大小,并且不会保持与调整框架大小之前相同的大小。我已阅读有关 Layouts 的信息,但无法使其正常工作。有什么建议吗?
【问题讨论】:
-
你为什么用new关键字调用ShowList()?它既不是类也不是方法。
-
Java GUI 必须在不同的操作系统、屏幕尺寸、屏幕分辨率等上使用不同的语言环境中的不同 PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them 以及 white space 的布局填充和边框。
标签: java swing jpanel layout-manager null-layout-manager