【问题标题】:Changing the constraints of a component in a GridBagLayout after they've been added to the frame将组件添加到框架后更改 GridBagLayout 中组件的约束
【发布时间】:2020-04-02 02:06:56
【问题描述】:

我在 stackoverflow 上看到了另外两篇关于此的帖子,在这两个帖子中,解决方案都使用了 setConstraints(myComponent, anotherConstraint),当我尝试在 java 中使用它时,它甚至没有成为可用的方法.

want to change an Inset in a gridbag layout dynamically

Change the component weight dynamically in GridBagLayout

按下按钮后我还能如何更改组件的weightx

实际问题是我在屏幕底部有两个组件,我需要将其中一个组件设置为按下按钮后屏幕的最大宽度。

【问题讨论】:

  • void setConstraints(Component comp, GridBagConstraints constraints) 在 GridBagLayout 的 JavaDocs 中。

标签: java swing layout-manager gridbaglayout


【解决方案1】:

我无法让setConstraints() 工作..

然后看来代码是错误的。从红色面板的 RHS 与 Articuno 标签的 LHS 整齐对齐的事实来看,我怀疑包含红色面板的网格袋单元不跨越一列,并且目前完全填充了该列。

可能还有其他原因,但短minimal reproducible example我不会进一步推测。

这是一个简化的示例,展示了如何执行此操作。请注意,必须先致电revalidate(),然后才能看到更改。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.image.BufferedImage;

public class FullWidthToggle {

    private JComponent ui = null;

    FullWidthToggle() {
        initUI();
    }

    public final void initUI() {
        if (ui != null) {
            return;
        }

        GridBagConstraints gbc = new GridBagConstraints();
        GridBagLayout gbl = new GridBagLayout();
        ui = new JPanel(gbl);
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));

        BufferedImage image = new BufferedImage(
                160, 20, BufferedImage.TYPE_INT_RGB);
        gbc.insets = new Insets(5, 5, 5, 5);
        ui.add(new JLabel(new ImageIcon(image)), gbc);

        final JCheckBox checkBox = new JCheckBox("Full Width");
        gbc.gridx = 1;
        gbc.anchor = GridBagConstraints.LINE_START;
        ui.add(checkBox, gbc);

        final JLabel label = new JLabel("Am I full width?");
        label.setBorder(new LineBorder(Color.RED, 2));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 2;
        ui.add(label, gbc);

        ActionListener actionListener = (ActionEvent e) -> {
            if (checkBox.isSelected()) {
                gbc.fill = GridBagConstraints.HORIZONTAL;
            } else {
                gbc.fill = GridBagConstraints.NONE;
            }
            gbl.setConstraints(label, gbc);
            ui.revalidate(); // <- important! 
        };
        checkBox.addActionListener(actionListener);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception useDefault) {
            }
            FullWidthToggle o = new FullWidthToggle();

            JFrame f = new JFrame(o.getClass().getSimpleName());
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.setLocationByPlatform(true);

            f.setContentPane(o.getUI());
            f.pack();
            f.setMinimumSize(f.getSize());

            f.setVisible(true);
        };
        SwingUtilities.invokeLater(r);
    }
}

【讨论】:

    猜你喜欢
    • 2014-09-18
    • 2015-08-05
    • 2011-12-13
    • 1970-01-01
    • 2016-06-22
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多