【发布时间】:2023-01-02 21:18:15
【问题描述】:
在此站点上,我找到了一个很好的示例,说明如何将 JScrollPane 应用于 JButton 矩阵。下面提供了代码。
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int x = 10;
int y = 5;
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(x, y));
for (int i = 0; i < x * y; i++) {
JButton button = new JButton(String.valueOf(i));
button.setPreferredSize(new Dimension(100, 20));
panel.add(button);
}
JPanel container = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
container.add(panel);
JScrollPane scrollPane = new JScrollPane(container);
f.getContentPane().add(scrollPane);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
如果我希望我的按钮具有任意大小和任意位置并具有相同的滚动可能性,我该如何修改代码。非常感谢任何提示或建议。
【问题讨论】:
标签: java jscrollpane