2015-05-09 31 views
1

我想要使輸入區域只有一個確定按鈕的JOptionPane。輸入對話框沒有圖標,只有確定選項

當試圖做到這一點,沒有圖標,但附加的取消按鈕:

String name = (String) JOptionPane.showInputDialog(null, "Please enter your name", "Name required", JOptionPane.PLAIN_MESSAGE, null, null, "name"); 

而且當我這樣做有一個圖標:

String name = (String) JOptionPane.showInputDialog(null, "Please enter your name", "Name required", JOptionPane.OK_OPTION, null, null, "name"); 

有沒有辦法來組合二?我不明白後者是如何工作的,因爲我使用null來放置圖標。

回答

4

事情是這樣的:

JTextField field = new JTextField(20); 
JLabel label = new JLabel("Enter your text here"); 
JPanel p = new JPanel(new BorderLayout(5, 2)); 
p.add(label, BorderLayout.WEST); 
p.add(field); 
JOptionPane.showMessageDialog(null, p, "Name required", JOptionPane.PLAIN_MESSAGE, null); 
String text = field.getText(); 
System.out.println("You've entered: " + text); 
+0

謝謝,這個工作,這是否意味着有一個在它的東西沒有建立?爲什麼選擇OK_OPTION時忽略圖標的null參數? –

+0

也可以將它與showInputDialog而不是showMessageDialog結合使用? –

+0

對於[示例](http://stackoverflow.com/a/3002830/230513)。 – trashgod

相關問題