[英]Multiple JOptionPane input dialogs?
我一直在寻找过去的一个小时,但是我一直找不到想要的解决方案。
我想使用JOptionPane
从用户那里获取多个输入,但是我不希望它们全部都在一个对话框窗口中。 我希望它过渡到下一个,或者只是弹出下一个。 有没有办法使用JOptionPane
做到这一点?
这是我到目前为止的内容:
import java.util.Scanner;
import javax.swing.*;
public class HomeWork2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
Scanner input3 = new Scanner(System.in);
Scanner input4 = new Scanner(System.in);
int days, assignments;
double temperature;
boolean isRaining;
JOptionPane.showInputDialog("How many days are left?");
days = input.nextInt();
JOptionPane.showInputDialog("How many assignments are due?");
assignments = input2.nextInt();
JOptionPane.showInputDialog("What is the temperature outside?");
temperature = input3.nextDouble();
JOptionPane.showInputDialog("Is it raining today?");
isRaining = input4.nextBoolean();
if(assignments<=0)
JOptionPane.showMessageDialog(null, "Why are you asking in the first place?");
else
if(days<5)
JOptionPane.showMessageDialog(null, "You need to hurry up, time is short.");
else
if(assignments>4)
JOptionPane.showMessageDialog(null, "You need to hurry up before the assignments pile up. Oh wait...");
else
if(temperature<50)
JOptionPane.showMessageDialog(null, "You should start working, it's not like it's warm eoungh to do anything.");
else
if(isRaining==true)
JOptionPane.showMessageDialog(null, "It's raining, you might as well start on your assignments.");
else
JOptionPane.showMessageDialog(null, "It's nice out and you have some time to spare, go have fun.");
input.close();
input2.close();
input3.close();
input4.close();
}
}
除了我上面的建议外,这里还需要一些其他的东西来理解以下代码( 请仅在阅读代码部分之前阅读所有内容 )
阅读布局管理器及其工作原理,尤其是在不了解本教程的情况下,请阅读Google的Grid Layout和Box Layout以获得示例和说明。
阅读什么是方法以及它们如何工作。
阅读有关事件调度线程(EDT)及其功能的信息 。
注意不要混合使用控制台应用程序范例和GUI应用程序范例。 使用一个或另一个。
了解如何使用对话框
阅读如何将String转换为int并查看如何转换为double
。
对于您的boolean
字段,我将使用一个包含ButtonGroup
的JRadioButton
以及如何获取在buttongroup中选择哪个单选按钮的方法 :
此代码应为您自己完成此方法提供一个起点
较短的annoyingGui
并不是我的最爱,因为每次您想要从用户那里获得输入时,它都会为用户打开一个新对话框,这很烦人。
singleDialogInformation()
使用JPanel
和GridLayout
来显示更复杂的GUI,以请求用户信息,并使用BoxLayout
将其显示回给用户,请注意,我没有使用2个不同的变量,而是将pane
变量重新分配给的新实例。具有不同布局的JPanel
。
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class UsingDialogsExample {
private JFrame frame;
private JPanel pane;
private JTextField daysField;
private JTextField assignmentField;
private int days = 0;
private int assignments = 0;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
//Comment / uncomment one of them to see the output related to each sample method.
// new UsingDialogsExample().annoyingGui();
new UsingDialogsExample().singleDialogInformation();
}
});
}
public void annoyingGui() {
frame = new JFrame("My Frame's Title");
String daysInput = JOptionPane.showInputDialog(frame, "How many days are left?"); //Get user input on the textfield as a String
String assignmentsInput = JOptionPane.showInputDialog(frame, "How many assignments are due?");
try {
days = Integer.parseInt(daysInput); //Convert the string gotten above to an int
assignments = Integer.parseInt(assignmentsInput);
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
JOptionPane.showMessageDialog(frame, "The number of days left is: " + days);
JOptionPane.showMessageDialog(frame, "The number of assignments due is: " + assignments);
}
public void singleDialogInformation() {
pane = new JPanel();
pane.setLayout(new GridLayout(0, 2, 2, 2));
daysField = new JTextField(5);
assignmentField = new JTextField(5);
pane.add(new JLabel("How many days are left?"));
pane.add(daysField);
pane.add(new JLabel("How many assignments are due?"));
pane.add(assignmentField);
int option = JOptionPane.showConfirmDialog(frame, pane, "Please fill all the fields", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (option == JOptionPane.YES_OPTION) {
String daysInput = daysField.getText();
String assignmentsInput = assignmentField.getText();
try {
days = Integer.parseInt(daysInput);
assignments = Integer.parseInt(assignmentsInput);
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
pane = new JPanel();
pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
pane.add(new JLabel("Days left: " + days));
pane.add(new JLabel("Assignments due: " + assignments));
JOptionPane.showMessageDialog(frame, pane);
}
}
}
annoyingGui
屏幕截图:
singleDialogInformation
屏幕截图:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.