从对话框中获取文本?

时间:2013-11-21 21:26:27

标签: java swing user-interface joptionpane

我正在尝试从我的程序创建的弹出窗口中获取值。它是这样的:

private void btnCheckoutActionPerformed(java.awt.event.ActionEvent evt) {                                            


    do
        JOptionPane.showInputDialog("Enter item price","");
        itemPrice = Double.parseDouble() 

    while();

那么如何从输入对话框中获取文本?

3 个答案:

答案 0 :(得分:3)

showInputDialog返回文本条目的String或组合框选择的Object。请参阅"How to Make Dialogs#Getting the User's Input from a Dialog"

boolean valid = false;
do {
    try {
        itemPrice = Double.parseDouble(JOptionPane.showInputDialog("Enter item price"));
        valid = true;
    } catch (NumberFormatException e) {}
} while(!valid);

答案 1 :(得分:2)

由于JOptionPane.showInputDialog ..返回一个String,你需要做的就是将它声明为一个像这样的字符串

String input = JOptionPane.showInputDialog("Enter item price","");

答案 2 :(得分:1)

试试这个

private void btnCheckoutActionPerformed(java.awt.event.ActionEvent evt) {                                            

do
    String value = JOptionPane.showInputDialog("Enter item price","");
    itemPrice = Double.parseDouble(value) 

while();

}