将 JButton 添加到布局为“GridLayout”的 JPanel
我正在开发一个 GUI,使用 java Swing 库。我想向 Jpanel 添加一些 JButton,该 Jpanel 使用 Gridlayout 作为其布局,但我有一个问题,面板的大小大约是显示器的整个大小,但是 JButton '大小可以不同,我可能会向面板添加一个 3*3 的 Jbutton 数组,而我可能会向面板添加一个 10 * 10 数组,问题是如果我添加一个 3*3 ,jbuttons 将大到占据整个显示屏,甚至当前 JPanel 顶部的 JPanel (名为 options ),我应该做什么才能为 JButtons 设置恒定大小所以即使它们是 2 个 Jbutton(现在占据整个显示器),它们的大小也不会改变( setSize 函数不起作用,我希望布局是 GridLayout ,而不是 null ),这是代码的一些部分:
public class Edit extends JFrame{
public Edit ()
{
width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
newer = new JButton(new ImageIcon(b)) ;
done = new JButton(new ImageIcon(f)) ;
savior = new JButton(new ImageIcon(d)) ;
undo = new JButton(new ImageIcon(h)) ;
newer.setSize(200, 60);
done.setSize(200, 60);
savior.setSize(200, 60);
undo.setSize(200, 60);
options = new JPanel();
options.setSize(width , 100);
options.setLayout(new GridLayout(1,5,(width- 1000)/6,20)) ;
options.add(newer);
options.add(done);
options.add(savior);
options.add(undo);
options.setBackground(Color.magenta);
options.add(selector);
this.setExtendedState(this.MAXIMIZED_BOTH);
this.setUndecorated(true);
this.setVisible(true);
view = new JPanel();
regions = new JButton[3][3];
view.setSize(width, height - 100) ;
view.setLayout(new GridLayout(3,3));
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
regions[i][j] = new JButton();
regions[i][j].setSize(80,80) ;
view.add(regions[i][j]);
}
editPhase = new JPanel();
editPhase.setLayout(null);
editPhase.add(options,BorderLayout.NORTH);
editPhase.add(view,BorderLayout.SOUTH);
this.add(editPhase);
}
}
提前致谢。
i am developing a GUI , using java Swing library .I want to add some JButtons to a Jpanel which is Using a Gridlayout as its layout , but i have a problem , the panel's size is about the whole size of the monitor , but the JButtons' size can be different , I may add a 3*3 array of Jbuttons to the panel , while I may add a 10 * 10 array to the panel , the problem is that if i add a 3*3 , the jbuttons would be as large as to occupy the whole display , even the JPanel on the top of the current JPanel (which is named options ), what should i do in order to set a constant size for JButtons so their size in not changed even if they are 2 Jbuttons ( which now takes the whole display) ( the setSize function wont work , and I want the layout to be a GridLayout , not null ) , here's some parts of the code :
public class Edit extends JFrame{
public Edit ()
{
width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
newer = new JButton(new ImageIcon(b)) ;
done = new JButton(new ImageIcon(f)) ;
savior = new JButton(new ImageIcon(d)) ;
undo = new JButton(new ImageIcon(h)) ;
newer.setSize(200, 60);
done.setSize(200, 60);
savior.setSize(200, 60);
undo.setSize(200, 60);
options = new JPanel();
options.setSize(width , 100);
options.setLayout(new GridLayout(1,5,(width- 1000)/6,20)) ;
options.add(newer);
options.add(done);
options.add(savior);
options.add(undo);
options.setBackground(Color.magenta);
options.add(selector);
this.setExtendedState(this.MAXIMIZED_BOTH);
this.setUndecorated(true);
this.setVisible(true);
view = new JPanel();
regions = new JButton[3][3];
view.setSize(width, height - 100) ;
view.setLayout(new GridLayout(3,3));
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
regions[i][j] = new JButton();
regions[i][j].setSize(80,80) ;
view.add(regions[i][j]);
}
editPhase = new JPanel();
editPhase.setLayout(null);
editPhase.add(options,BorderLayout.NORTH);
editPhase.add(view,BorderLayout.SOUTH);
this.add(editPhase);
}
}
thanks in advance .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是使用评论中的想法的实现:
Here's an implementation using ideas from the comment: