有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java更改GridBagLayout中组件添加到框架后的约束

我在stackoverflow上看到了另外两篇关于这一点的文章,在这两篇文章中,解决方案都使用了setConstraints(myComponent, anotherConstraint),当我尝试在java中使用它时,它甚至都不是一个可用的方法

want to change an Inset in a gridbag layout dynamically

Change the component weight dynamically in GridBagLayout

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

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

enter image description here


共 (1) 个答案

  1. # 1 楼答案

    I couldn't get setConstraints() to work..

    那么代码似乎是错的。从红色面板的RHS与Articuno标签的LHS整齐对齐的事实来看,我怀疑包含红色面板的网格袋单元没有跨越多个列,并且目前完全填充该列

    可能还有其他原因,但我不会进一步猜测

    enter image description hereenter image description here

    下面是一个简单的示例,演示如何执行此操作。注意,在看到更改之前,有必要调用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);
        }
    }