Java 如何控制面板的大小
我正在学习swing gui,当我使用以下代码时得到了这个结果:- 我使用的代码是:Java 如何控制面板的大小,java,swing,jpanel,Java,Swing,Jpanel,我正在学习swing gui,当我使用以下代码时得到了这个结果:- 我使用的代码是: private void initUI() { JTextArea visualize=new JTextArea(); visualize.setEditable(false); //DEFINE BUTTONS.... JButton[] buttons1={addition,subtraction,division,multiplica
private void initUI() {
JTextArea visualize=new JTextArea();
visualize.setEditable(false);
//DEFINE BUTTONS....
JButton[] buttons1={addition,subtraction,division,multiplication};
JButton [] buttons2={expr,date,conversion};
JPanel numerical=new JPanel(new FlowLayout());
numerical.setPreferredSize(new Dimension(350, 50));
for(int i=0;i<buttons1.length;i++){
numerical.add(buttons1[i]);
}
numerical.setBorder(new TitledBorder("Numerical Operations"));
JPanel nonnum=new JPanel(new FlowLayout());
nonnum.setPreferredSize(new Dimension(500, 50));
for(int i=0;i<buttons2.length;i++){
nonnum.add(buttons2[i]);
}
nonnum.setBorder(new TitledBorder("Non-numerical Operations"));
JPanel operations = new JPanel(new BorderLayout(2,2));
operations.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
operations.setSize(800, 100);
operations.add(numerical,BorderLayout.WEST);
operations.add(nonnum,BorderLayout.EAST);
JTable sheet = new JTable(10,5);
add(visualize, BorderLayout.NORTH);
add(sheet,BorderLayout.SOUTH);
add(operations,BorderLayout.CENTER);
pack();
setSize(1000, 700);
setTitle("Spreadsheet Application");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void initUI(){
JTextArea visualize=新的JTextArea();
visualize.setEditable(false);
//定义按钮。。。。
JButton[]buttons1={加、减、除、乘};
JButton[]buttons2={expr,date,conversion};
JPanel数值=新的JPanel(新的FlowLayout());
数值.setPreferredSize(新尺寸(350,50));
对于(int i=0;i,因为这就是边界布局
的工作原理,请仔细观察。中心位置将占据框架的所有剩余空间,而北部位置和南部位置将尝试遵守组件的首选尺寸
您可以使用GridBagLayout
,这将允许您更好地控制布局或使用一系列复合布局
类似于
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.BOTH;
add(visualize, gbc);
add(operations, gbc);
gbc.gridy = 1;
add(sheet, gbc);
通常不鼓励从顶级容器(如JFrame
)进行扩展,相反,您应该使用JPanel
来定义UI,然后将它们添加到所需的容器中。这增加了可用性,并使其更易于在不同的容器上使用
您可能还想看一看,因为这就是边界布局
的工作原理,请仔细看一看。中间位置
将占据框架的所有剩余空间,而北部位置
和南部位置
将尝试并遵循组件的首选尺寸
您可以使用GridBagLayout
,这将允许您更好地控制布局或使用一系列复合布局
类似于
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.BOTH;
add(visualize, gbc);
add(operations, gbc);
gbc.gridy = 1;
add(sheet, gbc);
通常不鼓励从顶级容器(如JFrame
)进行扩展,相反,您应该使用JPanel
来定义UI,然后将它们添加到所需的容器中。这增加了可用性,并使其更易于在不同的容器上使用
您可能还想看一看我们中的一些人发现GridBagLayout是一个巨大的难题,您可能不必使用它来做您想做的事情
布局管理器背后的想法是让布局管理器控制其组件的大小和位置,这样您就不必与它们纠缠不休。MP是对的,GridBag允许您进行大量控制,但这也意味着您必须正确处理许多事情,才能让它完成您希望它完成的任务
因此,另一种选择是:制作一个JPanel来保存可视化
和操作
面板;给这个新面板一个Y_轴方向的框布局,然后按您希望的顺序从上到下添加它们
然后将sheet
放在JFrame的BorderLayout.CENTER中。事实上,我认为您应该听取MP的建议,阅读关于JScrollPane的教程;我记得最清楚的是,您创建了面板,然后使用面板作为构造参数创建了JScrollPane实例,然后将scrollpane实例添加到JFrame中(在中间,在你的情况下)
处于中间位置时,它将随着用户更改窗口大小而展开和收缩
祝你好运。Swing需要一些时间来适应。我们中的一些人发现GridBagLayout是一个非常痛苦的东西,你可能不需要用它来做你想做的事情
布局管理器背后的想法是让布局管理器控制其组件的大小和位置,这样您就不必与它们纠缠不休。MP是对的,GridBag允许您进行大量控制,但这也意味着您必须正确处理许多事情,才能让它完成您希望它完成的任务
因此,另一种选择是:制作一个JPanel来保存可视化
和操作
面板;给这个新面板一个Y_轴方向的框布局,然后按您希望的顺序从上到下添加它们
然后将sheet
放在JFrame的BorderLayout.CENTER中。事实上,我认为您应该听取MP的建议,阅读关于JScrollPane的教程;我记得最清楚的是,您创建了面板,然后使用面板作为构造参数创建了JScrollPane实例,然后将scrollpane实例添加到JFrame中(在中间,在你的情况下)
处于中间位置时,它将随着用户更改窗口大小而展开和收缩
祝你好运。Swing需要一些时间来适应。尝试使用GroupLayout。
我也是SwingGUI新手,也遇到过类似的问题——GroupLayout挽救了这一天
JPanel complete=new JPanel();
GroupLayout gl=new GroupLayout(complete);
complete.setLayout(gl);
gl.setAutoCreateContainerGaps(true);
gl.setHorizontalGroup(gl.createParallelGroup() //this is parallel bcz you need components vertically
.addComponent(visualize) //you MUST add components to both horizontal and vertical groups
.addComponent(operations)
.addComponent(sheet)
);
gl.setVerticalGroup(gl.createSequentialGroup() //NOTE that this is sequential
.addComponent(visualize)
.addComponent(operations)
.addGap(50) //you can add gaps if you want
.addComponent(sheet)
);
add(complete);
尝试使用GroupLayout。
我也是SwingGUI新手,也遇到过类似的问题——GroupLayout挽救了这一天
JPanel complete=new JPanel();
GroupLayout gl=new GroupLayout(complete);
complete.setLayout(gl);
gl.setAutoCreateContainerGaps(true);
gl.setHorizontalGroup(gl.createParallelGroup() //this is parallel bcz you need components vertically
.addComponent(visualize) //you MUST add components to both horizontal and vertical groups
.addComponent(operations)
.addComponent(sheet)
);
gl.setVerticalGroup(gl.createSequentialGroup() //NOTE that this is sequential
.addComponent(visualize)
.addComponent(operations)
.addGap(50) //you can add gaps if you want
.addComponent(sheet)
);
add(complete);
请看(是的)