使用组合框时,SetAlignment方法在FlowLayout中不起作用

时间:2015-11-17 21:42:52

标签: 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);

}
}

1 个答案:

答案 0 :(得分:2)

validate();

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

相反,你应该使用:

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