当我将JList插入到GridLayout对话框中时,为什么组件会变大?

时间:2014-08-01 10:55:32

标签: java swing jlist

如果我使用JList,我的JDialog会变得非常大,而JTextFields会自己调整大小。但如果我不使用JList,一切都还可以。

要查看我的大按钮和文本字段,请运行此演示:

package demo;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class AddInventoryDialog extends JDialog {

    private static AddInventoryDialog dialog;

    private AddInventoryDialog() {
        super(null, "Add inventory", ModalityType.APPLICATION_MODAL);
        JPanel contentPane = (JPanel) super.getContentPane();
        contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        super.setLayout(new BorderLayout(0, 20));
        super.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(10, 2, 10, 5));

        JLabel nameLabel = new JLabel("Name");
        JLabel sizeLabel = new JLabel("Size");
        JLabel brandLabel = new JLabel("Brand");
        JLabel patternLabel = new JLabel("Pattern");
        JLabel colorLabel = new JLabel("Color");
        JLabel boughtLabel = new JLabel("Bought date(dd MM yyyy)");
        JLabel lastWashedLabel = new JLabel("Last washed(dd MM yyyy)");
        JLabel seasonLabel = new JLabel("Season");
        JLabel categoryLabel = new JLabel("Category");

        final JTextField nameField = new JTextField();
        final JTextField sizeField = new JTextField();
        final JTextField brandField = new JTextField();
        final JTextField patternField = new JTextField();
        final JTextField colorField = new JTextField();
        final JTextField boughtField = new JTextField();
        final JTextField lastWashedField = new JTextField();
        final JList<String> categoryField = new JList<>(new String[] { "ietm1", "ietm1", "ietm1", "ietm1" });

        panel.add(nameLabel);
        panel.add(nameField);
        panel.add(sizeLabel);
        panel.add(sizeField);
        panel.add(brandLabel);
        panel.add(brandField);
        panel.add(patternLabel);
        panel.add(patternField);
        panel.add(colorLabel);
        panel.add(colorField);
        panel.add(boughtLabel);
        panel.add(boughtField);
        panel.add(lastWashedLabel);
        panel.add(lastWashedField);
        panel.add(categoryLabel);
        panel.add(categoryField);

        final JButton cancelButton = new JButton("Cancel");
        final JButton okButton = new JButton("OK");
        panel.add(cancelButton);
        panel.add(okButton);

        contentPane.add(new JLabel("Add inventory"), BorderLayout.NORTH);
        contentPane.add(panel);

        super.pack();
    }

    public static void undisplay() {
        getInstance().dispose();
    }

    public static void display() {
        getInstance().setVisible(true);
    }

    public static AddInventoryDialog getInstance() {
        if (dialog == null) {
            dialog = new AddInventoryDialog();
        }
        return dialog;
    }

    public static void main(String[] args) {
        getInstance().setVisible(true);
    }
}

我试图将JList嵌入到JScrollPane中,但它没有帮助。

如何让JTextFields和JButton具有自然尺寸(不是那么大)?

谢谢!

0 个答案:

没有答案