JTextArea中的控制台输出和用户输入

时间:2017-03-31 16:31:50

标签: java swing jtextarea

我正在使用GUI,我希望用户能够按下"答案"按钮,然后控制台将在textarea2上打印,同时启用用户输入。我需要做些什么才能让JTextArea(textarea2)作为控制台运行并在insertquestions方法中打印文本并接受用户输入?

package Scrambler;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

import java.util.Random;
import java.util.Scanner;

public class GUI extends JFrame {

    private JMenuBar menuBar;
    private JButton button1;
    private JButton button2;
    private JPanel panel1;
    private JPanel panel2;
    private JTextField textfield2;
    private JTextArea textarea;
    private JTextArea textarea2;
    private JTextField textfield5;
    private JTextField textfield7;

    public GUI() {

        this.setTitle("UnScrambleGUI");
        this.setSize(500, 400);
        generateMenu();
        this.setJMenuBar(menuBar);

        JPanel contentPane = new JPanel(null);
        contentPane.setPreferredSize(new Dimension(500, 400));
        contentPane.setBackground(new Color(192, 192, 192));


        button1 = new JButton();
        button1.setBounds(56, 341, 103, 31);
        button1.setBackground(new Color(214, 217, 223));
        button1.setForeground(new Color(51, 153, 0));
        button1.setEnabled(true);
        button1.setFont(new Font("sansserif", 0, 12));
        button1.setText("Print Words");
        button1.setVisible(true);

        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                insertwords(evt);
            }
        });


        button2 = new JButton();
        button2.setBounds(311, 341, 95, 31);
        button2.setBackground(new Color(214, 217, 223));
        button2.setForeground(new Color(51, 153, 0));
        button2.setEnabled(true);
        button2.setFont(new Font("sansserif", 0, 12));
        button2.setText("Answer");
        button2.setVisible(true);

        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                insertquestions(evt);
            }
        });


        panel1 = new JPanel(null);
        panel1.setBorder(BorderFactory.createEtchedBorder(1));
        panel1.setBounds(22, 92, 179, 237);
        panel1.setBackground(new Color(214, 217, 223));
        panel1.setForeground(new Color(0, 0, 0));
        panel1.setEnabled(true);
        panel1.setFont(new Font("sansserif", 0, 12));
        panel1.setVisible(true);

        panel2 = new JPanel(null);
        panel2.setBorder(BorderFactory.createEtchedBorder(1));
        panel2.setBounds(229, 170, 258, 161);
        panel2.setBackground(new Color(214, 217, 223));
        panel2.setForeground(new Color(0, 0, 0));
        panel2.setEnabled(true);
        panel2.setFont(new Font("sansserif", 0, 12));
        panel2.setVisible(true);

        textfield2 = new JTextField();
        textfield2.setBounds(74, 65, 89, 25);
        textfield2.setBackground(new Color(153, 153, 153));
        textfield2.setForeground(new Color(255, 255, 255));
        textfield2.setEnabled(true);
        textfield2.setFont(new Font("sansserif", 0, 12));
        textfield2.setText("    Word List");
        textfield2.setVisible(true);

        textarea = new JTextArea();
        textarea.setBounds(10, 9, 156, 219);
        textarea.setBackground(new Color(255, 255, 255));
        textarea.setForeground(new Color(0, 0, 0));
        textarea.setEnabled(true);
        textarea.setFont(new Font("sansserif", 0, 12));
        textarea.setText("");
        textarea.setVisible(true);

        textarea2 = new JTextArea();
        textarea2.setBounds(9, 9, 237, 143);
        textarea2.setBackground(new Color(255, 255, 255));
        textarea2.setForeground(new Color(0, 0, 0));
        textarea2.setEnabled(true);
        textarea2.setFont(new Font("sansserif", 0, 12));
        textarea2.setText("");
        textarea2.setVisible(true);

        textfield5 = new JTextField();
        textfield5.setBounds(307, 140, 89, 28);
        textfield5.setBackground(new Color(153, 153, 153));
        textfield5.setForeground(new Color(255, 255, 255));
        textfield5.setEnabled(true);
        textfield5.setFont(new Font("sansserif", 0, 12));
        textfield5.setText(" Answer Here:");
        textfield5.setVisible(true);

        textfield7 = new JTextField();
        textfield7.setBounds(164, 10, 175, 37);
        textfield7.setBackground(new Color(102, 102, 102));
        textfield7.setForeground(new Color(255, 255, 255));
        textfield7.setEnabled(true);
        textfield7.setFont(new Font("Tahoma", 0, 18));
        textfield7.setText(" Unscrambler Game");
        textfield7.setVisible(true);

        contentPane.add(button1);
        contentPane.add(button2);
        contentPane.add(panel1);
        contentPane.add(panel2);
        contentPane.add(textfield2);
        panel1.add(textarea);
        panel2.add(textarea2);
        contentPane.add(textfield5);
        contentPane.add(textfield7);

        this.add(contentPane);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.pack();
        this.setVisible(true);


    }

    private void insertwords(ActionEvent evt) {

        Random r = new Random();

        String word1 = "application";
        String word2 = "programming";
        String word3 = "variable";
        String word4 = "method";
        String word5 = "class";
        String word6 = "package";
        String word7 = "import";
        String word8 = "boolean";
        String word9 = "debugger";
        String word10 = "syntax";
        String word11 = "compile";
        String word12 = "computer";

        String line1 = "-----------------------------------------";
        word1 = scramble(r, word1);
        String one = "#1- " + word1;
        word2 = scramble(r, word2);
        String two = "#2- " + word2;
        word3 = scramble(r, word3);
        String three = "#3- " + word3;
        word4 = scramble(r, word4);
        String four = "#4-  " + word4;
        word5 = scramble(r, word5);
        String five = "#5- " + word5;
        word6 = scramble(r, word6);
        String six = "#6- " + word6;
        word7 = scramble(r, word7);
        String seven = "#7- " + word7;
        word8 = scramble(r, word8);
        String eight = "#8- " + word8;
        word9 = scramble(r, word9);
        String nine = "#9- " + word9;
        word10 = scramble(r, word10);
        String ten = "#10- " + word10;
        word11 = scramble(r, word11);
        String elev = "#11- " + word11;
        word12 = scramble(r, word12);
        String twel = "#12- " + word12;
        String line2 = ("-----------------------------------------");

        textarea.setText(one + "\n" + two + "\n" + three + "\n" + four + "\n" + five + "\n" + six + "\n" + seven + "\n" + eight + "\n" + nine + "\n" + ten + "\n" + elev + "\n" + twel + "\n");


    }


    private void insertquestions(ActionEvent evt) {
        boolean incorrect = true;

        for (int attempts = 0; attempts < 3 && incorrect; attempts++) {
            Scanner input = new Scanner(System.in);
            System.out.println("What is the answer to #1?");
            String answer1 = input.nextLine();

            if (!"application".equals(answer1)) {
                System.out.println("Incorrect. Hint: This runs on your computer.");
            } else {
                System.out.println("Correct.");
                incorrect = false;

            }
        }
        boolean incorrect2 = true;

        for (int attempts = 0; attempts < 3 && incorrect2; attempts++) {
            Scanner input = new Scanner(System.in);
            System.out.println("What is the answer to #2?");
            String answer2 = input.nextLine();
            if (!"programming".equals(answer2)) {
                System.out.println("Incorrect. Hint: Writing code for a computer.");
            } else {
                System.out.println("Correct.");
                incorrect2 = false;
            }
        }
        boolean incorrect3 = true;

        for (int attempts = 0; attempts < 3 && incorrect3; attempts++) {
            Scanner input = new Scanner(System.in);
            System.out.println("What is the answer to #3?");
            String answer3 = input.nextLine();
            if (!"variable".equals(answer3)) {
                System.out.println("Incorrect. Hint: A value in a program that can be changed.");
            } else {
                System.out.println("Correct.");
                incorrect3 = false;
            }
        }

        boolean incorrect4 = true;

        for (int attempts = 0; attempts < 3 && incorrect4; attempts++) {
            Scanner input = new Scanner(System.in);
            System.out.println("What is the answer to #4?");
            String answer4 = input.nextLine();
            if (!"method".equals(answer4)) {
                System.out.println("Incorrect. Hint: Tells the computer to do a procedure.");
            } else {
                System.out.println("Correct.");
                incorrect4 = false;
            }
        }

        boolean incorrect5 = true;

        for (int attempts = 0; attempts < 3 && incorrect5; attempts++) {
            Scanner input = new Scanner(System.in);
            System.out.println("What is the answer to #5?");
            String answer5 = input.nextLine();
            if (!"class".equals(answer5)) {
                System.out.println("Incorrect. Hint: Used to describe one or more objects in a program.");
            } else {
                System.out.println("Correct.");
                incorrect5 = false;
            }
        }

        boolean incorrect6 = true;

        for (int attempts = 0; attempts < 3 && incorrect6; attempts++) {
            Scanner input = new Scanner(System.in);
            System.out.println("What is the answer to #6?");
            String answer6 = input.nextLine();

            if (!"package".equals(answer6)) {
                System.out.println("Incorrect. Hint: Organizes a set of classes or interfaces.");
            } else {
                System.out.println("Correct.");
                incorrect6 = false;
            }
        }

        boolean incorrect7 = true;

        for (int attempts = 0; attempts < 3 && incorrect7; attempts++) {
            Scanner input = new Scanner(System.in);
            System.out.println("What is the answer to #7?");
            String answer7 = input.nextLine();
            if (!"import".equals(answer7)) {
                System.out.println("Incorrect. Hint: To send data from one program to another.");
            } else {
                System.out.println("Correct.");
                incorrect7 = false;
            }
        }

        boolean incorrect8 = true;

        for (int attempts = 0; attempts < 3 && incorrect8; attempts++) {
            Scanner input = new Scanner(System.in);
            System.out.println("What is the answer to #8?");
            String answer8 = input.nextLine();
            if (!"boolean".equals(answer8)) {
                System.out.println("Incorrect. Hint: True or False?");
            } else {
                System.out.println("Correct.");
                incorrect8 = false;
            }
        }

        boolean incorrect9 = true;

        for (int attempts = 0; attempts < 3 && incorrect9; attempts++) {
            Scanner input = new Scanner(System.in);
            System.out.println("What is the answer to #9?");
            String answer9 = input.nextLine();
            if (!"debugger".equals(answer9)) {
                System.out.println("Incorrect. Hint: Used to exterminate bugs.");
            } else {
                System.out.println("Correct.");
                incorrect9 = false;
            }
        }

        boolean incorrect10 = true;

        for (int attempts = 0; attempts < 3 && incorrect10; attempts++) {
            Scanner input = new Scanner(System.in);
            System.out.println("What is the answer to #10?");
            String answer10 = input.nextLine();
            if (!"syntax".equals(answer10)) {
                System.out.println("Incorrect. Hint: Set of rules to follow while programming.");
            } else {
                System.out.println("Correct.");
                incorrect10 = false;
            }
        }

        boolean incorrect11 = true;

        for (int attempts = 0; attempts < 3 && incorrect11; attempts++) {
            Scanner input = new Scanner(System.in);
            System.out.println("What is the answer to #11?");
            String answer11 = input.nextLine();
            if (!"compile".equals(answer11)) {
                System.out.println("Incorrect. Hint: To translate code into a understandable language for a machine.");
            } else {
                System.out.println("Correct.");
                incorrect11 = false;
            }
        }

        boolean incorrect12 = true;

        for (int attempts = 0; attempts < 3 && incorrect12; attempts++) {
            Scanner input = new Scanner(System.in);
            System.out.println("What is the answer to #12?");
            String answer12 = input.nextLine();
            if (!"computer".equals(answer12)) {
                System.out.println("Incorrect. Hint: What you are using right now.");
            } else {
                System.out.println("Correct.");
                incorrect12 = false;
            }


        }

    }

    public void generateMenu() {
        menuBar = new JMenuBar();

        JMenu file = new JMenu("File");
        JMenu tools = new JMenu("Tools");
        JMenu help = new JMenu("Help");

        JMenuItem open = new JMenuItem("Open   ");
        JMenuItem save = new JMenuItem("Save   ");
        JMenuItem exit = new JMenuItem("Exit   ");
        JMenuItem preferences = new JMenuItem("Preferences   ");
        JMenuItem about = new JMenuItem("About   ");


        file.add(open);
        file.add(save);
        file.addSeparator();
        file.add(exit);
        tools.add(preferences);
        help.add(about);

        menuBar.add(file);
        menuBar.add(tools);
        menuBar.add(help);
    }


    public static void main(String[] args) {
        System.setProperty("swing.defaultlaf", "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new GUI();
            }
        });
    }


    public static String scramble(Random random, String inputString) {

        char a[] = inputString.toCharArray();

        for (int n = 0; n < a.length - 1; n++) {
            int k = random.nextInt(a.length - 1);

            char temp = a[n];
            a[n] = a[k];
            a[k] = temp;
        }

        return new String(a);
    }
}

1 个答案:

答案 0 :(得分:1)

正如评论中所提到的,你不希望像你提议的那样将控制台程序插入到GUI中,因为虽然这是可行的,但它需要跳过线程箍,并且会编程反对 GUI库的事件驱动范例。

相反,我强烈建议你“重构”这个程序,即重新构造并从头开始重新编写它,着眼于创建一组可在事件驱动的环境中运行良好的强大类。 。我会将问题解构为非GUI模型类和GUI或“视图”类,最重要的是前者,模型。

例如,对于模型,请考虑创建以下类:

  • ScrambledWord:一个接受并保存测试字符串的类,它接受并存储一个“提示”字符串,该字符串可以生成测试字符串的随机加扰版本,该字符串具有可以测试的public boolean evaluate(String)方法字符串,以查看它是否正确,并且具有适当的构造函数,setter和getter。
  • 学生:使用您的GUI保存学生考试成绩的班级
  • 测试:一个包含ScrambledWord对象的集合(例如ArrayList)的类,它包含一个Student对象,可以遍历此集合,返回列表中的下一个ScrambledWord,可以更改学生的测试分数。

然后对于GUI,考虑创建

  • 一个QuestionPanel JPanel,它有一个显示当前测试的乱码字的JLabel,一个用户可以输入猜测的JTextField,以及一个学生想要提交猜测的JButton。如果猜测错误,可以显示错误JOptionPane,同时显示提示。如果猜测正确,则显示下一个问题。也许还给它另一个JButton允许用户跳过当前问题,如果他们无法解决它。
  • 一个主要的JPanel,它包含Question JPanel并允许用户开始测试,完成测试,显示用户的分数。

这里的键是按钮的ActionListeners。提交按钮的侦听器将通过调用其传递文本字段文本的evaluate(...)方法,针对当前显示的ScrambledWord对象测试JTextField中的文本。如果evaluate返回true,则递增学生的分数并显示其中的下一个问题QuestionPanel JPanel,可能带有public void setScrambledWord(ScrambledWord sWord)

这些只是建议,如果需要,您可以遵循,但我的主要建议是不要尝试强制您的控制台应用程序进入GUI。如果你这样做,你将不会是一个快乐的程序员。