我的程序中有一个附加到 JButton 的 Action 监听器。 单击该按钮时,将打开一个对话框,要求用户输入数字,然后单击“确定”。
这部分工作正常,我遇到的问题是调用用户输入的数字作为 if 语句的一部分。
有人可以告诉我如何调用用户输入的这个号码吗?这是迄今为止我的代码。
public void actionPerformed(ActionEvent e) {
if (e.getSource() == t1) {
String Message = "Enter an Amount ";
String number = JOptionPane.showInputDialog(null, Message,
JOptionPane.QUESTION_MESSAGE);
}
if () { // ideally here i would want to say if the user number is bigger
// than 0 then do this...
}
}
更改后我的代码现在看起来像这样
公共(public)无效actionPerformed(ActionEvent e) {
if (e.getSource() == t1)
{
String Message = "Enter an Amount ";
String number =
JOptionPane.showInputDialog(null,
Message,
JOptionPane.QUESTION_MESSAGE);
if(Integer.valueOf(number) > 0)
{
String s = a.gettext();
getContentPane().removeAll();
repaint();
new TaxiFrame(Integer.parseInt(s));
}
else{}
}
else{}
}
}
最佳答案
try {
//.Convert string to Integer
if (Integer.valueOf(number) > 0) {
//. Do that
} else {
//. Otherwise
}
} catch (NumberFormatException err) {
err.printStackTrace();
//.Conversion failed. The user entered a non numeric string
}
关于java - 如何调用作为 Action 监听器一部分输入对话框的信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22454650/