[英]How to remove extra spaces in JFrame using GridBagLayout?
当我在我的框架中插入任何按钮或任何组件并将组件gridx
和gridy
为值0
,它不会将该组件放在框架的开头,而是将该组件放在中心的某个位置。
如何删除空格并将我的组件放在框架的最开始?
以下是代码:
JFrame frame = new JFrame("Grigbag layout");
JPanel pane = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton button = new JButton("First button");
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
我建议您使用 Flow Layout 并添加此代码
frame.pack();
正如上面安德鲁汤普森所提到的
更多信息: https : //docs.oracle.com/javase/tutorial/uiswing/layout/flow.html
使用frame.pack()
方法并删除frame.setSize();
方法。
frame 使用默认布局。 用类似的东西设置框架的布局
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.