惠峰 2021-02-19 21:44 采纳率: 33.3%
浏览 95

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

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);
    }
}
  • 写回答

2条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-07 17:58
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境