【发布时间】:2012-01-10 21:52:03
【问题描述】:
我创建了一个打开JOptionPane 的按钮。它允许用户输入string..>> String str = JOptionPane.showInputDialog 如何获取用户输入到 joptionpane 的文本并使用它来搜索用户对象?
非常感谢
【问题讨论】:
-
你是如何存储用户对象的?
标签: java string swing joptionpane
我创建了一个打开JOptionPane 的按钮。它允许用户输入string..>> String str = JOptionPane.showInputDialog 如何获取用户输入到 joptionpane 的文本并使用它来搜索用户对象?
非常感谢
【问题讨论】:
标签: java string swing joptionpane
你的目的有点不清楚,但据我了解,你只是想知道如何输入信息,这可以通过简单地调用变量来完成。
要查看变量中的内容,请使用 System.out.println(variable name);
请定义用户对象?
希望这会有所帮助。
【讨论】:
返回的 String 是用户输入的内容,如果用户选择取消,则返回 null:
String whatTheUserEntered = JOptionPane.showInputDialog(...);
if (whatTheUserEntered == null) {
System.out.println("The user canceled");
}
【讨论】:
虽然@JB Nizet 已经给出了很好的答案。如果有人再次寻找这个问题,我想添加一个简短的代码示例以供参考。
public class JOptionPaneExample
{ 私人双倍价格;
private JTextField priceField;
private JLabel priceLabel;
public JOptionPaneExample()
{
priceField = new JTextField(10);
}
public void createAndDisplayGUI()
{
int selection = JOptionPane.showConfirmDialog(null, getPanel(), "Price Form : ", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (selection == JOptionPane.OK_OPTION)
{
price = Double.valueOf(priceField.getText());
JOptionPane.showMessageDialog(null, "Price is : " + Double.toString(price), "Price : ", JOptionPane.PLAIN_MESSAGE);
}
else if (selection == JOptionPane.CANCEL_OPTION)
{
// Do something here.
}
}
private JPanel getPanel()
{
JPanel basePanel = new JPanel();
basePanel.setOpaque(true);
basePanel.setBackground(Color.BLUE.darker());
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(3, 2, 5, 5));
centerPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
centerPanel.setOpaque(true);
centerPanel.setBackground(Color.WHITE);
priceLabel = new JLabel("Enter Price : ");
centerPanel.add(priceLabel);
centerPanel.add(priceField);
basePanel.add(centerPanel);
return basePanel;
}
}
可以找到相同的代码this blog
【讨论】: