如何将具有扫描仪输入的控制台模拟或带入jFrame应用程序

时间:2013-12-15 14:08:33

标签: java swing

我想将控制台带入jFrame窗口应用程序,并能够与扫描仪输入进行交互。基本上,无论控制台中发生什么,我都希望在TextArea的应用程序窗口中显示它。有一个简单的解决方案吗?

这是我非常简单的代码

ConsoleLogic类:

import java.util.Scanner;


public class ConsoleLogic {

    public static void main(String[] args) {

        System.out.println("How old are you?");

        Scanner scanner = new Scanner(System.in);
        int input = scanner.nextInt();

        System.out.println("");
        System.out.println("How many siblings do you have?");
        int input2 = scanner.nextInt();

        System.out.println("Thank you for your answer!");
        System.out.println("You are "+input+ " years old and you have "+input2+" 2siblings.");

    }

}

ConsoleGui类:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JLabel;

public class ConsoleGui extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ConsoleGui frame = new ConsoleGui();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public ConsoleGui() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblDisplayOutput = new JLabel("Display output:");
        lblDisplayOutput.setBounds(22, 25, 124, 16);
        contentPane.add(lblDisplayOutput);

        JTextArea textArea = new JTextArea();// ideally I would like to bring
                                                // console and the inputs inside
                                                // here
        textArea.setBounds(22, 65, 402, 185);
        contentPane.add(textArea);
    }
}

非常感谢你的帮助。

编辑:我想在这张照片中有类似的东西:(在photoshop中制作)

enter image description here

1 个答案:

答案 0 :(得分:2)

除非您知道如何编写Swing gui程序,否则请忘记框架并使用JOptionPane,如果您只想将文本放入textField中。试试这个:

public static void main(String[] args) {

    System.out.println("How old are you?");

    Scanner scanner = new Scanner(System.in);
    int input = scanner.nextInt();

    System.out.println("");
    System.out.println("How many siblings do you have?");
    int input2 = scanner.nextInt();

    String s1 = "Thank you for your answer!";
    String s2 = "You are "+input+ " years old and you have "+input2+" 2siblings.";

    JOptionPane.showMessageDialog(null, s1 + "\n" + s2);  // pop up dialog
}

修改

public static void main(String[] args) {

    String age = JOptionPane.showInputDialog(null, "How old are you?");
    System.out.println(age);

    String siblings = JOptionPane.showInputDialog(null, "How many siblings do you have?");
    System.out.println(siblings);

    String s1 = "Thank you for your answer!";
    String s2 = "You are "+age+ " years old and you have "+siblings+" 2siblings.";

    JOptionPane.showMessageDialog(null, s1 + "\n" + s2);  // pop up dialog
}

编辑2:

我不想为你做这件事,因为你不会那样学习。所以这是我将要做的一步一步的逻辑。

  1. JTextArea显示对话
  2. 有一个JTextField来获取输入
  3. 提出一系列问题
  4. 使用第一个问题初始化文本区域
  5. 从文本字段中获取输入并将其附加到文本区域,但先将其保存到变量
  6. 将数组中的第二个打印到文本区域。
  7. 从文本字段中获取下一个答案,将其保存到变量并打印到文本区域
  8. 组成最终字符串并将其附加到文本区域。
  9. 注意:每次用户点击文本字段中的输入时,索引变量应该递增,以获得问题数组中的下一个索引。