我想創建一個簡單的數據輸入對話框的自定義面板。我創建了一個自定義面板,添加了我的標籤和文本字段。我試圖將它添加到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);
}
謝謝!對此,我真的非常感激。我仍然在學習所有這些GUI的東西。 – wyoskibum 2013-03-11 19:26:36