【发布时间】:2017-01-27 22:55:57
【问题描述】:
过去一个小时我一直在寻找,但我一直无法找到我正在寻找的解决方案。
我想使用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();
}
}
【问题讨论】:
-
在编码论坛上提问之前,你应该知道java和js的区别
-
对不起...它在推荐之下。就像我说的,我只写了几个星期的代码。
-
1) 您正在混合 2 种范式,控制台应用程序和 GUI 应用程序,如果您已经编写了几个星期的代码,我建议您首先学习控制台应用程序的基础知识。 2)例如
Scanner input = new Scanner(System.in);你应该只有其中一个,然后你可以像days = input.nextInt();和assignments = input.nextInt();一样做。 3) 要在基于 GUI 的应用程序中获取用户输入,您可以使用JOptionPane#showInputDialog... -
... 4) 你的程序不过是一个
main方法,学会使用方法和参数。 5)JOptionPane.showMessageDialog(null, "You should start working, it's not like it's warm eoungh to do anything.");,第一个参数不应为空,而是对您的JFrame的引用。 6)你的缩进是错误的,elif应该都在同一级别,建议使用花括号({和}),它们会为你节省很多未来的问题 -
7) 看来您可能只想在一个窗口中获取所有用户输入(恕我直言,如果弹出多个窗口一直要求我输入 1 个字段,我会很恼火),或者如果您仍然想惹恼您的最终用户,然后尝试使用Card Layout
标签: java swing joptionpane