[英]How can I display all of my inputs into one JOptionPane instead of one window at a time?
Below is my code. 下面是我的代码。 I am very new to Java and programming in general this is my 2nd day in class, when i run this i end up being able to only open one dialog box at a time instead of being able to input all of the data and having it all show in the dialog box.
我对Java和编程非常陌生,这是我上课的第二天,当我运行它时,我最终只能一次打开一个对话框,而不能输入所有数据并拥有全部显示在对话框中。 Any help would be greatly appreciated!
任何帮助将不胜感激! Looking forward to learning more about programming and hopefully one day helping others.
期待了解有关编程的更多信息,并希望有一天能帮助其他人。
import java.util.Scanner;
import javax.swing.JOptionPane;
public class EchoProgram {
public static void main(String[] args) {
byte byteValue;
short svalue;
int ivalue;
long lvalue;
float fvalue;
double dvalue;
char charValue;
boolean booleanvalue;
Scanner keyboard= new Scanner(System.in);
System.out.println("Enter a byte value (-128 through 127)");
byteValue = keyboard.nextByte();
JOptionPane.showMessageDialog(null, "You have entered " + byteValue);
System.out.println("Enter a short value (-32,768 through 32,767)");
svalue = keyboard.nextShort();
JOptionPane.showMessageDialog(null, "You have entered " + svalue);
System.out.println("Enter a int value (-2,147,483,648 through 2,147,483,647)");
ivalue = keyboard.nextInt();
JOptionPane.showMessageDialog(null, "You have entered " + ivalue);
System.out.println("Enter a long value (-9,223,372,036,854,775,808 through 9,223,372,036,854,775,807)");
lvalue = keyboard.nextLong();
JOptionPane.showMessageDialog(null, "You have entered " + lvalue);
System.out.println("Enter a float value (number with decimals)");
fvalue = keyboard.nextFloat();
JOptionPane.showMessageDialog(null, "You have entered " + fvalue);
System.out.println("Enter a double value (number with decimals)");
dvalue = keyboard.nextDouble();
JOptionPane.showMessageDialog(null, "You have entered " + dvalue);
System.out.println("Enter a char value (a through z)");
charValue = keyboard.next().charAt(0);
JOptionPane.showMessageDialog(null, "You have entered " + charValue);
System.out.println("Enter a boolean value (true or false)");
booleanvalue = keyboard.nextBoolean();
JOptionPane.showMessageDialog(null, "You have entered " + booleanvalue);
keyboard.close();
}
}
A little late, but here it goes: 有点晚了,但是在这里:
import java.awt.GridLayout;
import java.util.Scanner;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class EchoProgram {
public static void main(String[] args) {
Byte byteValue;
Short svalue;
int ivalue;
long lvalue;
float fvalue;
double dvalue;
char charValue;
boolean booleanvalue;
JTextField answer1 = new JTextField();
JTextField answer2 = new JTextField();
JTextField answer3 = new JTextField();
JTextField answer4 = new JTextField();
JTextField answer5 = new JTextField();
JTextField answer6 = new JTextField();
JTextField answer7 = new JTextField();
JTextField answer8 = new JTextField();
JLabel jLabel1 = new JLabel("Enter a byte value (-128 through 127)");
JLabel jLabel2 = new JLabel("Enter a short value (-32,768 through 32,767)");
JLabel jLabel3 = new JLabel("Enter a int value (-2,147,483,648 through 2,147,483,647)");
JLabel jLabel4 = new JLabel("Enter a long value (-9,223,372,036,854,775,808 through 9,223,372,036,854,775,807)");
JLabel jLabel5 = new JLabel("Enter a float value (number with decimals)");
JLabel jLabel6 = new JLabel("Enter a double value (number with decimals)");
JLabel jLabel7 = new JLabel("Enter a char value (a through z)");
JLabel jLabel8 = new JLabel("Enter a boolean value (true or false)");
JPanel jPanel = new JPanel();
jPanel.setLayout(new GridLayout(8, 2));
Scanner keyboard= new Scanner(System.in);
System.out.println("Enter a byte value (-128 through 127)");
byteValue = keyboard.nextByte();
System.out.println("Enter a short value (-32,768 through 32,767)");
svalue = keyboard.nextShort();
System.out.println("Enter a int value (-2,147,483,648 through 2,147,483,647)");
ivalue = keyboard.nextInt();
System.out.println("Enter a long value (-9,223,372,036,854,775,808 through 9,223,372,036,854,775,807)");
lvalue = keyboard.nextLong();
System.out.println("Enter a float value (number with decimals)");
fvalue = keyboard.nextFloat();
System.out.println("Enter a double value (number with decimals)");
dvalue = keyboard.nextDouble();
System.out.println("Enter a char value (a through z)");
charValue = keyboard.next().charAt(0);
System.out.println("Enter a boolean value (true or false)");
booleanvalue = keyboard.nextBoolean();
jPanel.add(jLabel1);
answer1.setText(byteValue.toString());
jPanel.add(answer1);
jPanel.add(jLabel2);
answer2.setText(svalue.toString());
jPanel.add(answer2);
jPanel.add(jLabel3);
answer3.setText(String.valueOf(ivalue));
jPanel.add(answer3);
jPanel.add(jLabel4);
answer4.setText(String.valueOf(lvalue));
jPanel.add(answer4);
jPanel.add(jLabel5);
answer5.setText(String.valueOf(fvalue));
jPanel.add(answer5);
jPanel.add(jLabel6);
answer6.setText(String.valueOf(dvalue));
jPanel.add(answer6);
jPanel.add(jLabel7);
answer7.setText(charValue + "");
jPanel.add(answer7);
jPanel.add(jLabel8);
answer8.setText(booleanvalue + "");
jPanel.add(answer8);
jPanel.setVisible(true);
JOptionPane.showMessageDialog(null,jPanel,"Information",JOptionPane.INFORMATION_MESSAGE);
keyboard.close();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.