java - SetAlignment 方法在带有组合框的 FlowLayout 中不起作用

标签 java swing combobox flowlayout

我试图弄清楚如何更改 FlowLayout 与组合框的对齐方式,但由于某种原因 setAlignment 方法对我不起作用;我的代码没有做任何事情。当我创建一个全新的对象时它会起作用,例如:

buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT);

但是 HGap 和 VGap 部件无法正常工作;它不是在组件之间创建空间,而是分别水平和垂直扩展面板。非常感谢任何帮助!

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.border.TitledBorder;

public class HW3LayoutSettings extends JFrame {
private JPanel buttonPanel = new JPanel();
private JPanel propertiesPanel = new JPanel();
private JButton comp0 = new JButton("Component 0");
private JButton comp1 = new JButton("Component 1");
private JButton comp2 = new JButton("Component 2");
private JButton comp3 = new JButton("Component 3");
private JButton comp4 = new JButton("Component 4");
private JButton comp5 = new JButton("Component 5");
private JButton comp6 = new JButton("Component 6");
private JButton comp7 = new JButton("Component 7");
private JButton comp8 = new JButton("Component 8");
private JButton comp9 = new JButton("Component 9");
private JButton comp10 = new JButton("Component 10");
private JButton comp11 = new JButton("Component 11");
private JButton comp12 = new JButton("Component 12");
private JButton comp13 = new JButton("Component 13");
private JButton comp14 = new JButton("Component 14");
private FlowLayout flow = new FlowLayout();
private JLabel alignLabel = new JLabel("Alignment");
private JLabel hGapLabel = new JLabel("HGap:");
private JLabel vGapLabel = new JLabel("VGap:");
private JTextField hGapField = new JTextField();
private JTextField vGapField = new JTextField();
private GridLayout grid = new GridLayout(3, 3);
private String[] align = {"LEFT", "CENTER", "RIGHT"};
private JComboBox combobox = new JComboBox(align);
private Integer hGapInt;
private Integer vGapInt;

public HW3LayoutSettings() {
    buttonPanel.setLayout(flow);
    buttonPanel.add(comp0);
    buttonPanel.add(comp1);
    buttonPanel.add(comp2);
    buttonPanel.add(comp3);
    buttonPanel.add(comp4);
    buttonPanel.add(comp5);
    buttonPanel.add(comp6);
    buttonPanel.add(comp7);
    buttonPanel.add(comp8);
    buttonPanel.add(comp9);
    buttonPanel.add(comp10);
    buttonPanel.add(comp11);
    buttonPanel.add(comp12);
    buttonPanel.add(comp13);
    buttonPanel.add(comp14);
    propertiesPanel.setLayout(grid);
    propertiesPanel.add(alignLabel);
    propertiesPanel.add(combobox);
    propertiesPanel.add(hGapLabel);
    propertiesPanel.add(hGapField);
    propertiesPanel.add(vGapLabel);
    propertiesPanel.add(vGapField);

    add(buttonPanel, BorderLayout.CENTER);
    add(propertiesPanel, BorderLayout.SOUTH);

    TitledBorder titled1 = new TitledBorder("Container of FlowLayout");
    buttonPanel.setBorder(titled1);
    TitledBorder titled2 = new TitledBorder("FlowLayout Properties");
    propertiesPanel.setBorder(titled2);

    combobox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String s = (String) combobox.getSelectedItem();
            switch (s) {
                case "LEFT": {
                    flow.setAlignment(FlowLayout.LEFT);
                    validate();
                    break;
                }
                case "CENTER": {
                    flow.setAlignment(FlowLayout.CENTER);
                    validate();
                    break;
                }
                case "RIGHT": {
                    flow.setAlignment(FlowLayout.RIGHT);
                    validate();
                    break;
                }
        }
        }
    });

    hGapField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            hGapInt = Integer.parseInt(hGapField.getText());
            flow.setHgap(hGapInt);
            setSize((int) (getWidth() + hGapInt), getHeight());
            validate();
        }
    });

    vGapField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            vGapInt = Integer.parseInt(vGapField.getText());
            flow.setVgap(vGapInt);
            setSize(getWidth(), (int) (getHeight() + vGapInt));
            validate();
        }
    });
}

public static void main(String[] args) {
    HW3LayoutSettings frame = new HW3LayoutSettings();
    frame.setTitle("HW3LayoutSettings");
    frame.setSize(400, 320);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

}
}

最佳答案

validate();

更改布局属性时不要使用 validate()。

您应该使用:

buttonPanel.revalidate(); // invokes the layout manager
buttonPanel.repaint();  // repaints components

关于java - SetAlignment 方法在带有组合框的 FlowLayout 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33767558/

相关文章:

java - 向列表添加元素太慢

java - swing flowlayout preferredSize 不会自动改变

c# - 带有 MVVM 模式的 WPF 中的组合框列绑定(bind) (Galasoft)

java - 如何使用其中包含 actionPerformed 的 IF 语句从另一个类检查一个类中的 JToggleButton 状态?

c# - 检查 ComboBox 是否包含项目

java - 删除 JComboBox 内的文件

java - 从 Java (JFrame) 包中加载图像

java - JTable 中 fireTableDataChanged 的​​奇怪行为

java - 有没有更快的方法来生成 N 整数列表

Java JPA、Swing 和 Beans 绑定(bind) : Changes to a OneToMany collection in entity not immediately reflected in GUI