我创建了一个按钮,打开一个JOptionPane
。它允许用户输入一个string..>> String str = JOptionPane.showInputDialog
如何获取用户输入到joptionpane中的文本并使用它来搜索用户对象?JOptionPane和从输入中获取文本
非常感谢
我创建了一个按钮,打开一个JOptionPane
。它允许用户输入一个string..>> String str = JOptionPane.showInputDialog
如何获取用户输入到joptionpane中的文本并使用它来搜索用户对象?JOptionPane和从输入中获取文本
非常感谢
你的目的,它有点不清楚,但是从我如何理解它,你只是想知道如何进入信息,这可以通过简单地调用变量来完成。
要查看变量中的内容,请使用System.out.println(变量名称);
请定义userobjects?
希望这会有所帮助。
返回的字符串是用户输入的内容,或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
import javax.swing.*;
class Test
{
public static void main (String args[])
{
String name = JOptionPane.showInputDialog("Enter the name");
}
}
你是怎么保存的userobjects? – Jomoos