2013-03-11 59 views
2

我想創建一個簡單的數據輸入對話框的自定義面板。我創建了一個自定義面板,添加了我的標籤和文本字段。我試圖將它添加到JOptionPane中進行顯示。Java將組件添加到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); 
} 

回答

4

您需要到主面板添加到您的パ。

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

謝謝!對此,我真的非常感激。我仍然在學習所有這些GUI的東西。 – wyoskibum 2013-03-11 19:26:36

1

您沒有將主面板添加到您的TestPanel實例的構造函數中。

+0

謝謝!你是對的。 – wyoskibum 2013-03-11 19:27:01

3

在initUI中,您創建一個JPanel(稱爲「main」)並填充組件,但不要對其執行任何操作。您需要將「main」添加到TestPanel的實際實例中,或者完全跳過創建「主」面板的步驟。

第一種方式:

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

(其中 「本。」 的不是嚴格必要的,但我加入它們用於說明。)

希望這會有所幫助!

+0

謝謝!幫助很多! – wyoskibum 2013-03-11 19:32:18