H5W3
当前位置:H5W3 > 其他技术问题 > 正文

Java GUI GridBagLayout布局生成的组件太小

这是需要的效果
image
这是实际的效果
image
下面是对应代码

public class TestDemo {
    public static void main(String[] args) {
      JFrame frame = new JFrame("我的窗体");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(640, 640);
      JPanel panel = new JPanel();
      panel.setSize(640,640);
      frame.setResizable(false);
      frame.setLocationRelativeTo(null);
      GridBagLayout gridBagLayout = new GridBagLayout();
      panel.setLayout(gridBagLayout);
      GridBagConstraints constraints = new  GridBagConstraints();
      constraints.fill = GridBagConstraints.BOTH;
      constraints.gridx = 1;
      constraints.gridy = 2;
      constraints.gridwidth = 3;
      constraints.gridheight = 3;
      JTextArea textArea = new JTextArea("文本区");
      gridBagLayout.setConstraints(textArea, constraints);
      constraints.gridx = 4;
      constraints.gridy = 2;
      constraints.gridwidth = 1;
      constraints.gridheight = 1;
      JButton buttonConfirm = new JButton("确定");
      gridBagLayout.setConstraints(buttonConfirm, constraints);
      constraints.gridx = 5;
      constraints.gridy = 2;
      constraints.gridwidth = 2;
      constraints.gridheight = 1;
      JButton buttonCancel = new JButton("取消");
      gridBagLayout.setConstraints(buttonCancel, constraints);
      ButtonGroup group = new ButtonGroup();
      constraints.gridx = 4;
      constraints.gridy = 3;
      constraints.gridwidth = 1;
      constraints.gridheight = 1;
      JRadioButton buttonBold = new JRadioButton("粗体");
      gridBagLayout.setConstraints(buttonBold, constraints);
      constraints.gridx = 5;
      constraints.gridy = 3;
      constraints.gridwidth = 2;
      constraints.gridheight = 1;
      JRadioButton buttonItalic = new JRadioButton("斜体");
      gridBagLayout.setConstraints(buttonItalic, constraints);
      constraints.gridx = 4;
      constraints.gridy = 4;
      constraints.gridwidth = 2;
      constraints.gridheight = 1;
      JRadioButton buttonCommon = new JRadioButton("常规");
      gridBagLayout.setConstraints(buttonCommon, constraints);
      constraints.gridx = 5;
      constraints.gridy = 4;
      constraints.gridwidth = 2;
      constraints.gridheight = 1;
      JRadioButton buttonBoldItalic = new JRadioButton("粗体/斜体");
      gridBagLayout.setConstraints(buttonBoldItalic, constraints);
      constraints.gridx = 1;
      constraints.gridy = 5;
      constraints.gridwidth = 6;
      constraints.gridheight = 1;
      JTextField textField = new JTextField("输入你的姓名");
      gridBagLayout.setConstraints(textField, constraints);
      group.add(buttonBold);
      group.add(buttonItalic);
      group.add(buttonCommon);
      group.add(buttonBoldItalic);
      panel.add(textArea);
      panel.add(buttonConfirm);
      panel.add(buttonCancel);
      panel.add(buttonBold);
      panel.add(buttonItalic);
      panel.add(buttonCommon);
      panel.add(buttonBoldItalic);
      panel.add(textField);
      frame.add(panel);
      frame.setVisible(true);
   }
}

回答:

设置 GridBagConstraints 对象的 weightxweighty,这是自动调整大小的比重。

而且建议你每个组件给个新的 GridBagConstraints 对象,免得属性污染。

回答:

没想到还有人邀请我看Swing的问题,确实上次用到Swing还是我大学刚学Java(。・∀・)ノ゙…

好久没看了,上午还是去简单熟悉了哈GridBagLayout,从最终展示效果来看,其实已经满足了题主GridBagConstraints的设置,只是问题应该是感觉整个panel不够大,因为现在的效果是按照每个组件最小的宽度和高度+匹配GridBagConstraints设置的值形成的

所以如果设置 weightxweighty,确实可以调整额外空间的分布方式,但是调整之后,比如给JTextArea("文本区")JButton("确定")JButton("取消")分别设置一个weightx(50,20,40),确实大了,但是也铺满了。。。
image.png

铺满的原因也是因为默认的JFrame布局是BorderLayout,所以自动铺满了,因此想要居中的话,可以给这个布局中的EASTWEST加一个隐藏的component,这样中间的panel就可以减少点宽度了,类似这样

image.png

效果如下
image.png

当然你还可以自己去调整两边的隐藏component宽度,找到一个合适的,同理高度也是一样。

我也不是很专业,可能还有很多其他方案,不过看起来现在我的方案大体满足要求吧,题主可以自行试试,我把代码放下面了,可以看看(我看题主虽然newButtonGroup,但是最终没有放到panel里,所以我下面代码干脆就删除了ButtonGrou部分,题主需要可以自行加上)

public class TestDemo3 {
    public static void main(String[] args) {
         JFrame frame = new JFrame("我的窗体");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setSize(640, 640);
         JPanel panel = new JPanel();
         panel.setSize(640,640);
         frame.setResizable(false);
         frame.setLocationRelativeTo(null);
         GridBagLayout gridBagLayout = new GridBagLayout();
         panel.setLayout(gridBagLayout);
         
         GridBagConstraints constraints = new GridBagConstraints();
         constraints.fill = GridBagConstraints.BOTH;
         constraints.weightx = 50;
         constraints.gridx = 1;
         constraints.gridy = 2;
         constraints.gridwidth = 3;
         constraints.gridheight = 3;
         JTextArea textArea = new JTextArea("文本区");
         gridBagLayout.setConstraints(textArea, constraints);
         
         GridBagConstraints constraints1 = new GridBagConstraints();
         constraints1.fill = GridBagConstraints.BOTH;
         constraints1.weightx = 20;
         constraints1.gridx = 4;
         constraints1.gridy = 2;
         constraints1.gridwidth = 1;
         constraints1.gridheight = 1;
         JButton buttonConfirm = new JButton("确定");
         gridBagLayout.setConstraints(buttonConfirm, constraints1);
         
         GridBagConstraints constraints2 = new GridBagConstraints();
         constraints2.fill = GridBagConstraints.BOTH;
         constraints2.weightx = 40;
         constraints2.gridx = 5;
         constraints2.gridy = 2;
         constraints2.gridwidth = 2;
         constraints2.gridheight = 1;
         JButton buttonCancel = new JButton("取消");
         gridBagLayout.setConstraints(buttonCancel, constraints2);
         
         GridBagConstraints constraints3 = new GridBagConstraints();
         constraints3.fill = GridBagConstraints.BOTH;
         constraints3.gridx = 4;
         constraints3.gridy = 3;
         constraints3.gridwidth = 1;
         constraints3.gridheight = 1;
         JRadioButton buttonBold = new JRadioButton("粗体");
         gridBagLayout.setConstraints(buttonBold, constraints3);
         
         GridBagConstraints constraints4 = new GridBagConstraints();
         constraints4.fill = GridBagConstraints.BOTH;
         constraints4.gridx = 5;
         constraints4.gridy = 3;
         constraints4.gridwidth = 2;
         constraints4.gridheight = 1;
         JRadioButton buttonItalic = new JRadioButton("斜体");
         gridBagLayout.setConstraints(buttonItalic, constraints4);
         
         GridBagConstraints constraints5 = new GridBagConstraints();
         constraints5.fill = GridBagConstraints.BOTH;
         constraints5.gridx = 4;
         constraints5.gridy = 4;
         constraints5.gridwidth = 2;
         constraints5.gridheight = 1;
         JRadioButton buttonCommon = new JRadioButton("常规");
         gridBagLayout.setConstraints(buttonCommon, constraints5);
         
         GridBagConstraints constraints6 = new GridBagConstraints();
         constraints6.fill = GridBagConstraints.BOTH;
         constraints6.gridx = 5;
         constraints6.gridy = 4;
         constraints6.gridwidth = 2;
         constraints6.gridheight = 1;
         JRadioButton buttonBoldItalic = new JRadioButton("粗体/斜体");
         gridBagLayout.setConstraints(buttonBoldItalic, constraints6);
         
         GridBagConstraints constraints7 = new GridBagConstraints();
         constraints7.fill = GridBagConstraints.BOTH;
         constraints7.gridx = 1;
         constraints7.gridy = 5;
         constraints7.gridwidth = 6;
         constraints7.gridheight = 1;
         JTextField textField = new JTextField("输入你的姓名");
         gridBagLayout.setConstraints(textField, constraints7);
         
         panel.add(textArea);
         panel.add(buttonConfirm);
         panel.add(buttonCancel);
         panel.add(buttonBold);
         panel.add(buttonItalic);
         panel.add(buttonCommon);
         panel.add(buttonBoldItalic);
         panel.add(textField);
         
         frame.add(panel);
         
         int gap = 120;
         frame.add(Box.createHorizontalStrut(gap), BorderLayout.EAST);
         frame.add(Box.createHorizontalStrut(gap), BorderLayout.WEST);
         
         frame.setVisible(true);
 }
}

`

本文地址:H5W3 » Java GUI GridBagLayout布局生成的组件太小

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址