Java JOptionPane 多个输入和单个输出中的算术运算
Java JOptionPane Multiple Inputs and Arithmetic Operations In a Single Output
我已经开始 class 关于 Java 的学习,刚刚了解了 JOptionPane 并且我们对用户在输入对话框中给出的输入进行了一些算术运算 window但是每个输入都有不同的对话框,然后执行操作并在消息框中向用户显示输出。
我想知道的是,如何在一个框中获取用户的所有输入并在消息框中向用户显示输出?
这是我们处理过的示例的代码。
double ap = 0.3;
double op = 0.2;
double fp = 0.5;
String a = JOptionPane.showInputDialog(null,"Input A:");
String b = JOptionPane.showInputDialog(null,"Input B:");
String c = JOptionPane.showInputDialog(null,"Input C:");
String d = JOptionPane.showInputDialog(null,"Input D:");
double aValue = (Double.parseDouble(a)*ap);
double bcValue = (((Double.parseDouble(b)/2(Double.parseDouble(c)/2))*op);
double fValue = (Double.parseDouble(d)*fp);
double res = aValue+bcValue+fValue;
JOptionPane.showMessageDialog(null,"Result : " +res);
好吧...如果您想在一个对话框中进行多个输入,那么您可以使用此处的 JOptionPane.showConfirmDialog() method and supply it with a custom JPanel (some examples)。换句话说,您创建了一个自定义对话框。
在您的情况下,您可以采用以下一种方法:
String dialogMessage = "Please suppliy digits to the following<br>input boxes:";
int btns = JOptionPane.OK_CANCEL_OPTION;
BorderLayout layout = new BorderLayout();
JPanel panel = new JPanel(layout);
JLabel label = new JLabel("<html>" + dialogMessage + "<br><br><br></html>");
panel.add(label, BorderLayout.NORTH);
JPanel p = new JPanel(new BorderLayout(5, 5));
JPanel labels = new JPanel(new GridLayout(0, 1, 2, 2));
labels.add(new JLabel("Input A", SwingConstants.RIGHT));
labels.add(new JLabel("Input B", SwingConstants.RIGHT));
labels.add(new JLabel("Input C", SwingConstants.RIGHT));
labels.add(new JLabel("Input D", SwingConstants.RIGHT));
p.add(labels, BorderLayout.WEST);
JPanel controls = new JPanel(new GridLayout(0, 1, 2, 2));
JTextField inputA = new JTextField();
controls.add(inputA);
JTextField inputB = new JTextField();
controls.add(inputB);
JTextField inputC = new JTextField();
controls.add(inputC);
JTextField inputD = new JTextField();
controls.add(inputD);
p.add(controls, BorderLayout.CENTER);
panel.add(p);
// Get Input from User...
int res = JOptionPane.showConfirmDialog(null, panel, "My Dialog Title", btns);
if (res == JOptionPane.OK_OPTION) {
System.out.println("Input A is: " + inputA.getText());
System.out.println("Input B is: " + inputB.getText());
System.out.println("Input C is: " + inputC.getText());
System.out.println("Input D is: " + inputD.getText());
}
您可以修改此概念以满足您的需要。
我已经开始 class 关于 Java 的学习,刚刚了解了 JOptionPane 并且我们对用户在输入对话框中给出的输入进行了一些算术运算 window但是每个输入都有不同的对话框,然后执行操作并在消息框中向用户显示输出。
我想知道的是,如何在一个框中获取用户的所有输入并在消息框中向用户显示输出?
这是我们处理过的示例的代码。
double ap = 0.3;
double op = 0.2;
double fp = 0.5;
String a = JOptionPane.showInputDialog(null,"Input A:");
String b = JOptionPane.showInputDialog(null,"Input B:");
String c = JOptionPane.showInputDialog(null,"Input C:");
String d = JOptionPane.showInputDialog(null,"Input D:");
double aValue = (Double.parseDouble(a)*ap);
double bcValue = (((Double.parseDouble(b)/2(Double.parseDouble(c)/2))*op);
double fValue = (Double.parseDouble(d)*fp);
double res = aValue+bcValue+fValue;
JOptionPane.showMessageDialog(null,"Result : " +res);
好吧...如果您想在一个对话框中进行多个输入,那么您可以使用此处的 JOptionPane.showConfirmDialog() method and supply it with a custom JPanel (some examples)。换句话说,您创建了一个自定义对话框。
在您的情况下,您可以采用以下一种方法:
String dialogMessage = "Please suppliy digits to the following<br>input boxes:";
int btns = JOptionPane.OK_CANCEL_OPTION;
BorderLayout layout = new BorderLayout();
JPanel panel = new JPanel(layout);
JLabel label = new JLabel("<html>" + dialogMessage + "<br><br><br></html>");
panel.add(label, BorderLayout.NORTH);
JPanel p = new JPanel(new BorderLayout(5, 5));
JPanel labels = new JPanel(new GridLayout(0, 1, 2, 2));
labels.add(new JLabel("Input A", SwingConstants.RIGHT));
labels.add(new JLabel("Input B", SwingConstants.RIGHT));
labels.add(new JLabel("Input C", SwingConstants.RIGHT));
labels.add(new JLabel("Input D", SwingConstants.RIGHT));
p.add(labels, BorderLayout.WEST);
JPanel controls = new JPanel(new GridLayout(0, 1, 2, 2));
JTextField inputA = new JTextField();
controls.add(inputA);
JTextField inputB = new JTextField();
controls.add(inputB);
JTextField inputC = new JTextField();
controls.add(inputC);
JTextField inputD = new JTextField();
controls.add(inputD);
p.add(controls, BorderLayout.CENTER);
panel.add(p);
// Get Input from User...
int res = JOptionPane.showConfirmDialog(null, panel, "My Dialog Title", btns);
if (res == JOptionPane.OK_OPTION) {
System.out.println("Input A is: " + inputA.getText());
System.out.println("Input B is: " + inputB.getText());
System.out.println("Input C is: " + inputC.getText());
System.out.println("Input D is: " + inputD.getText());
}
您可以修改此概念以满足您的需要。