簡體   English   中英

Java向JOptionPane添加組件

[英]Java adding components to 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);
}

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

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

在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);
}

(“這個。”並不是絕對必要的,但我添加它們是為了說明。)

希望這可以幫助!

您沒有在構造函數中將主Panel添加到TestPanel實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM