【问题标题】:swing panel resizable gap摆动面板可调整大小的间隙
【发布时间】:2012-11-27 04:09:44
【问题描述】:

我正在使用 eclipse 和 windowsbuilder 来制作 java gui 应用程序。我在维护摆动面板中组件的填充时遇到了麻烦。我正在使用 windowbuilder 设计器中的组布局来定位组件。但遗憾的是,当窗口重新调整大小时,我无法保持填充。


普通窗口

调整大小或最大化时的窗口

当窗口调整大小时,我希望让我的应用程序自动调整水平和垂直间隙的大小,就像第三张图片一样,以便正确保持组件填充。

代码:

public myframe() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.CENTER);

        JButton btnNewButton = new JButton("New button");
        GroupLayout gl_panel = new GroupLayout(panel);
        gl_panel.setHorizontalGroup(
            gl_panel.createParallelGroup(Alignment.TRAILING)
                .addGroup(Alignment.LEADING, gl_panel.createSequentialGroup()
                    .addGap(166)
                    .addComponent(btnNewButton)
                    .addContainerGap(169, Short.MAX_VALUE))
        );
        gl_panel.setVerticalGroup(
            gl_panel.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_panel.createSequentialGroup()
                    .addGap(37)
                    .addComponent(btnNewButton)
                    .addContainerGap(192, Short.MAX_VALUE))
        );
        panel.setLayout(gl_panel);
    }

【问题讨论】:

标签: java swing layout layout-manager grouplayout


【解决方案1】:

我已经找到了解决方案::) 只需将 .addGap() 替换为 .addContainerGap() 即可。

【讨论】:

    猜你喜欢
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    • 2013-02-19
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    相关资源
    最近更新 更多