我正在尝试创建一个转换长度单位的向导。我可以创建domain.net/EmpArea/Login/EmpArea/Timesheet
并让用户选择要执行的转换类型;但是,我无法在最后OptionDialog
显示结果。这是我正在使用的代码:
MessageDialog
我需要做些什么才能让结果显示在最后?
答案 0 :(得分:0)
该应用有效,但设计非常糟糕。
这是更好的例子
public class LengthConversion {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
double cmValue, inchValue;
int choice;
//Customise Object text
Object[] options = {"cm-to-inch", "inch-to-cm", "Cancel"};
//Option Dialog box
choice = JOptionPane.showOptionDialog(null, "Choice an option", "Length Conversion", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[2]);
String inputType;
switch(choice) {
case JOptionPane.YES_OPTION:
try {
//Show Input Dialog Box
inputType = JOptionPane.showInputDialog("Enter the cm to convert:");
cmValue = Double.parseDouble(inputType);
//Compute cm to inch
inchValue = cmValue / 2.54;
//Preview result
JOptionPane.showMessageDialog(null, + inchValue + " inches");
}
catch(NumberFormatException e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
break;
case JOptionPane.NO_OPTION:
try {
//Ask user to enter value
inputType = JOptionPane.showInputDialog("Enter the inch to convert:");
//Read value that user type
inchValue = Double.parseDouble(inputType);
//compute inch to cm
cmValue = inchValue * 2.54;
//Display result
JOptionPane.showMessageDialog(null, + cmValue + " cm");
}
catch(NumberFormatException e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
break;
}
}
}
答案 1 :(得分:0)
考虑选择= 0(选择= 1,解决方案类似),此代码包含以下行:
cmValue = fromKeyboard.nextDouble();
基本上等着你从键盘输入一个数字。程序挂在那里等你输入一个数字。
对于解决方案,请将其评论并添加:
// cmValue = fromKeyboard.nextDouble();
cmValue = Integer.parseInt(inputType);