2015-05-09 53 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

相关问题