Java JOptionPane并从输入中获取文本

Java JOptionPane并从输入中获取文本,java,string,swing,joptionpane,Java,String,Swing,Joptionpane,我创建了一个按钮,可以打开作业窗格。它允许用户输入string..>>string str=JOptionPane.showInputDialog如何获取用户输入到JOptionPane的文本并使用它搜索用户对象 非常感谢您的目的有点不清楚,但从我的理解来看,您只想知道如何输入信息,只需调用变量即可 要查看变量中的内容,请使用System.out.println(变量名) 请定义用户对象 希望这有帮助。返回的字符串是用户输入的字符串,如果用户选择取消,则返回null: String whatTh

我创建了一个按钮,可以打开
作业窗格
。它允许用户输入
string..>>string str=JOptionPane.showInputDialog
如何获取用户输入到JOptionPane的文本并使用它搜索用户对象


非常感谢

您的目的有点不清楚,但从我的理解来看,您只想知道如何输入信息,只需调用变量即可

要查看变量中的内容,请使用System.out.println(变量名)

请定义用户对象


希望这有帮助。

返回的字符串是用户输入的字符串,如果用户选择取消,则返回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;
}
}


可以找到相同的代码

您是如何存储userobjects的?