我只想要一个简单的弹出消息框,要求多个输入等。
System.out.println("Please enter Name");
Name = name.nextLine();
System.out.println("Please enter Age");
age=ages.nextInt();
System.out.println("Please enter Gender");
Gender =gender.nextLine();
System.out.println("Please enter Contact Number");
ConNum =number.nextLine();-->
有没有办法为此制作GUI?(其基础级项目)
使用JOptionPane
进行输入:
String name = JOptionPane.showInputDialog(null, "Please enter Name");
编辑:
您可以向用户询问一个框上的所有问题(在此示例中,确保它们通过a space 将答案分开。
String input = JOptionPane.showInputDialog(null, "Please enter name.nPlease enter age.nPlease enter gender.");
String[] allInput = input.split("\s+");
System.out.println("Name: " + allInput[0] + " Age: " + allInput[1] + " Gender: " + allInput[2]);
注意:显然会有问题,例如不使用空间或使用全名(杰克·拼发)等;但这给了您一个一般的想法。
正如@notyou提到的,您可以使用JOptionPane
,将其实现在代码中非常简单,请检查此示例:
import java.io.*;
import javax.swing.*;
public class SystemOutDialog {
public static void main(String[] args) {
// set up a custom print stream
CustomPrintStream printStream = new CustomPrintStream();
System.setOut(printStream);
// from now on, the System.out.println() will shows a dialog message
System.out.println("hello!");
System.out.println("how are you?");
}
}
class CustomPrintStream extends PrintStream {
public CustomPrintStream() {
super(new ByteArrayOutputStream());
}
public void println(String msg) {
JOptionPane.showMessageDialog(null, msg);
}
}
源链接