简体   繁体   English

如何将所有输入显示在一个JOptionPane中,而不是一次显示一个窗口?

[英]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.

相关问题 如何在GUI中出现另一个JOptionPane时解除一个JOptionPane - How can I dismiss one JOptionPane upon emergence of another JOptionPane in the GUI 如何为一个视图接一个动画而不是同时显示所有视图? - How can I animate one View after another instead of all at the same time? 在while循环之后将所有输出行添加到一个JOptionPane窗口中 - Adding all lines of output to one JOptionPane window after while loop 多个JOptionPane输入发送到一个String输出 - Multiple JOptionPane inputs sent to one String output JOptionPane使用一个窗口进行输入和输出 - JOptionPane using one window for input and output 如何按组而不是一对一处理所有已读项目? - How can I process all readed items in groups instead one to one? 我怎样才能检查数组一侧的所有元素,而不是停在一个局部最小值上? - How can I get this to check all of the elements on one side of the array instead of stopping at one local minimum? 有人知道我怎么能只使用一个JOptionPane.showMessageDialog来进行切换吗? - Anyone know how I can just use one JOptionPane.showMessageDialog for the switch cases? 如何使用两个按钮而不是 JOptionPane.showOptionDialog? - How can I use two buttons instead of JOptionPane.showOptionDialog? 一次并非一次全部显示jtextfield中的数据 - Display data in jtextfield one by one not all at a time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM