[英]Text Based game JTextArea does not displaying JTextField
Ok, so I am making a text-based java game like Zork. 好的,所以我正在制作基于文本的Java游戏,例如Zork。 I am doing with a simple GUI, that has a JTextField JTextArea ETC. 我正在使用一个简单的GUI,它具有JTextField JTextAreaETC。 I am having extreme difficulty having the user type in the textField for instance, "North" and have the JTextArea even say north let alone do a command. 我很难让用户在textField中输入用户名,例如“ North”,让JTextArea甚至说North更不用说执行命令了。 Here is my code. 这是我的代码。
import java.awt.Color;
import java.awt.Desktop.Action;
import java.util.Scanner;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class gameFrame {
public static void main(String[] args) {
//create variables for graphics
String a = ("Adventure");
JFrame frame = new JFrame(a);
JPanel panel = new JPanel();
JTextField input = new JTextField(25);
JTextArea output = new JTextArea("Hello", 65, 30);
JMenu exit = new JMenu("Exit");
//add main parts together
frame.add(panel);
panel.add(input);
panel.add(output);
//frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
frame.setSize(900, 600);
//panel
panel.setBackground(Color.WHITE);
//input
input.setForeground(Color.MAGENTA);
String inputBox = input.getText();
//output
output.setEditable(false);
output.setForeground(Color.MAGENTA);
}
}
Ok, so you compare strings in java with a do .... while ....
loop. 好的,所以您将Java中的字符串与do .... while ....
循环进行比较。 Have you tried something like this? 你尝试过这样的事情吗?
String inputBox = input.getText();
do {
// Code to do whatever when inputBox equals "north"
output.append("You went: North");
}
while(inputBox.equals/*IgnoreCase*/("north");
What you can also do is add an ActionListener
to the JTextField. 您还可以做的是向JTextField添加一个ActionListener
。 When clicked enter, display the contents of the JTextField on the JTextArea. 单击回车后,在JTextArea上显示JTextField的内容。
input.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
output.append(inputBox.getText());
}
}
);
This is uncompiled, and unchecked. 这是未编译且未经检查的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.