Java JTextArea中的控制台输出和用户输入
我正在使用GUI,我希望用户能够按下“应答”按钮,然后控制台将在启用用户输入的情况下在Java JTextArea中的控制台输出和用户输入,java,swing,jtextarea,Java,Swing,Jtextarea,我正在使用GUI,我希望用户能够按下“应答”按钮,然后控制台将在启用用户输入的情况下在textarea2上打印。我需要做什么才能让JTextArea(textarea2)起到控制台的作用,并在接受用户输入的同时以insertquestions方法打印文本 package Scrambler; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import jav
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);
}
}
包扰频器;
导入java.awt.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.*;
导入java.util.Random;
导入java.util.Scanner;
公共类GUI扩展JFrame{
私人杰门努巴·梅努巴;
私人按钮1;
私人按钮2;
私人JPanel小组1;
私人JPanel小组2;
私有JTextField textfield2;
私人JTEXTEXTAREA textarea;
私人区域文本区域2;
私有JTextField textfield5;
私有JTextField textfield 7;
公共图形用户界面(){
本文件的标题(“解读”);
这个。设置大小(500400);
生成单元();
这个.setJMenuBar(菜单栏);
JPanel contentPane=新的JPanel(null);
setPreferredSize(新维度(500400));
挫折背景(新颜色(192192192));
button1=新的JButton();
按钮1.立根(563411331);
按钮1.挫折背景(新颜色(214217223));
按钮1.设置前景(新颜色(51153,0));
按钮1.setEnabled(真);
按钮1.设置字体(新字体(“sansserif”,0,12));
按钮1.setText(“打印文字”);
按钮1.设置可见(真);
button1.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件evt){
插入词(evt);
}
});
button2=新的JButton();
按钮2.立根(3113419531);
按钮2.后退(新颜色(214217223));
按钮2.设置前景(新颜色(51153,0));
按钮2.setEnabled(真);
按钮2.设置字体(新字体(“sansserif”,0,12));
按钮2.setText(“应答”);
按钮2.setVisible(真);
button2.addActionListener(新建ActionListener(){
已执行的公共无效操作(操作事件evt){
插入问题(evt);
}
});
panel1=新JPanel(空);
panel1.setBorder(BorderFactory.CreateEdtchedBorder(1));
小组1.立根(22,92,179,237);
镶板1.背景(新颜色(214217223));
面板1.设置前景(新颜色(0,0,0));
panel1.setEnabled(真);
面板1.setFont(新字体(“sansserif”,0,12));
面板1.设置可见(真);
panel2=新JPanel(空);
panel2.setBorder(BorderFactory.createEtchedBorder(1));
镶板2.立根(229170258161);
镶板2.背景(新颜色(214217223));
面板2.设置前景(新颜色(0,0,0));
panel2.setEnabled(真);
面板2.setFont(新字体(“sansserif”,0,12));
面板2.设置可见(真);
textfield2=新的JTextField();
textfield2.挫折(74,65,89,25);
textfield2.挫折背景(新颜色(153153153));
textfield2.setForeground(新颜色(255、255、255));
textfield2.setEnabled(true);
textfield2.setFont(新字体(“sansserif”,0,12));
textfield2.setText(“单词列表”);
textfield2.setVisible(true);
textarea=新的JTextArea();
textarea.setBounds(10,9,156,219);
textarea.setBackground(新颜色(255、255、255));
设置前景(新颜色(0,0,0));
textarea.setEnabled(true);
setFont(新字体(“sansserif”,0,12));
textarea.setText(“”);
textarea.setVisible(true);
textarea2=新的JTextArea();
文本区域2.立根(9,9,237,143);
文本区域2.收进背景(新颜色(255、255、255));
textarea2.设置前景(新颜色(0,0,0));
textarea2.setEnabled(真);
textarea2.setFont(新字体(“sansserif”,0,12));
textarea2.setText(“”);
textarea2.setVisible(true);
textfield5=新的JTextField();
textfield5.挫折(307140,89,28);
textfield5.挫折背景(新颜色(153153153));
textfield5.setForeground(新颜色(255、255、255));
textfield5.setEnabled(true);
textfield5.setFont(新字体(“sansserif”,0,12));
textfield5.setText(“在这里回答:”);
textfield5.setVisible(true);
textfield7=新的JTextField();
textfield7.挫折(164,10,175,37);
textfield7.挫折背景(新颜色(102102102));
textfield7.setForeground(新颜色(255、255、255));
textfield7.setEnabled(true);
textfield7.setFont(新字体(“Tahoma”,0,18));
textfield7.setText(“解读者游戏”);
textfield7.setVisible(true);
contentPane.add(按钮1);
contentPane.add(按钮2);
contentPane.add(panel1);
contentPane.add(panel2);
contentPane.add(textfield2);
面板1.添加(文本区域);
面板2.添加(文本区域2);
contentPane.add(textfield5);
contentPane.add(textfield7);
添加(contentPane);
此.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
此.setLocationRelativeTo(空);
这个包();
此.setVisible(true);
}
私有void insertwords(ActionEvent evt){
随机r=新随机();
String word1=“应用程序”;
String word2=“编程”;
String word3=“变量”;
String word4=“方法”;
String word5=“class”;
String word6=“package”;
String word7=“导入”;
字符串word8=“boolean”;
String word9=“调试器”;
String word10=“语法”;
String word11=“编译”;
String word12=“计算机”;
字符串line1=“----------------------------------------------”;
word1=紧急停堆