Java向JOptionPane添加组件

时间:2013-03-11 18:57:20

标签: java swing joptionpane

我正在尝试为简单的数据输入对话框创建自定义面板。我创建了一个自定义面板,我添加了标签和文本字段。我正在尝试将它添加到JOptionPane中进行显示。

当我调用它时,我的组件不会显示,但JOptionPane按钮会显示。这样做的正确方法是什么?先谢谢!!

这是我创建自定义面板并调用JOptionPane的地方:

    public class ListenCustEdit implements ActionListener {
    @Override
    @SuppressWarnings("empty-statement")
    public void actionPerformed(ActionEvent e) {
        TestPanel panel = new TestPanel();
        int input = JOptionPane.showConfirmDialog(frame, panel, "Edit Customer:"
                        ,JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        if (input == 0) {
            // OK
            JOptionPane.showMessageDialog(frame,"Changes savewd");
        } else {
            // Cancel
            JOptionPane.showMessageDialog(frame, "No changes were saved");
        }
    }
}

这是我的自定义面板类:

public class TestPanel extends JPanel {

JTextField custIdTextField;
JTextField companyTextField;
JTextField firstNameTextField;
JTextField lastNameTextField;

public TestPanel() {
    initUI();
}

public final void initUI() {

    // create the panel and set the layout
    JPanel main = new JPanel();
    main.setLayout(new GridLayout(4,4));

    // create the labels
    JLabel custIdLabel = new JLabel("Cust Id: ");
    JLabel companyLabel = new JLabel("Company: ");
    JLabel firstNameLabel = new JLabel("First Name: ");
    JLabel lastNameLabel = new JLabel("Last Name: ");

    // create the text fields
    custIdTextField = new JTextField();
    companyTextField = new JTextField();
    firstNameTextField = new JTextField();
    lastNameTextField = new JTextField();

    // add componets to panel
    main.add(custIdLabel);
    main.add(custIdTextField);
    main.add(companyLabel);
    main.add(companyTextField);
    main.add(firstNameLabel);
    main.add(firstNameTextField);
    main.add(lastNameLabel);
    main.add(lastNameTextField);
}

3 个答案:

答案 0 :(得分:4)

您需要将主面板添加到TestPanel。

public final void initUI() {
    // ...
    add(main);
}

答案 1 :(得分:3)

在initUI中,您创建了一个JPanel(名为" main")并且填充了很多组件,但是没有对它做任何事情。你需要添加" main"到TestPanel的实际实例,或者只是跳过创建" main"的步骤。小组一起。

第一种方式:

public final void initUI() {
    // ... everything as before, then 
    this.add(main);
}

第二种方式:

public final void initUI() {
    this.setLayout(new GridLayout(4,4));

    // create the labels
    JLabel custIdLabel = new JLabel("Cust Id: ");
    JLabel companyLabel = new JLabel("Company: ");
    JLabel firstNameLabel = new JLabel("First Name: ");
    JLabel lastNameLabel = new JLabel("Last Name: ");

    // create the text fields
    custIdTextField = new JTextField();
    companyTextField = new JTextField();
    firstNameTextField = new JTextField();
    lastNameTextField = new JTextField();

    // add componets to panel
    this.add(custIdLabel);
    this.add(custIdTextField);
    this.add(companyLabel);
    this.add(companyTextField);
    this.add(firstNameLabel);
    this.add(firstNameTextField);
    this.add(lastNameLabel);
    this.add(lastNameTextField);
}

("此。"&nt严格来说不是必需的,但我添加了它们以供说明。)

希望这有帮助!

答案 2 :(得分:1)

您没有在构造函数中将主Panel添加到TestPanel实例。