如何在文字冒险游戏中获取用户输入 (Java/Applet)
How to get users input in a text adventure game (Java/Applet)
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
class DD {
public static void main(String[] args){
JFrame myFrame = new JFrame("#########");
myFrame.setSize(640,480);
myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
myFrame.setVisible(true);
JTextArea textArea = new JTextArea();
myFrame.add(textArea);
new JTextArea();
textArea.setEditable(false);
//System objects
Scanner in = new Scanner(System.in);
boolean running = true;
textArea.append("\t\n########################################");
textArea.append("\t\n>#############");
textArea.append("\t\n>Th#############11!");
textArea.append("\t\n>Typ#############enture!");
textArea.append("\t\n########################################");
String input = in.nextLine();
if(input.equals("start")){
{ ///beginning of story.
if(running)
textArea.append("\t\nYo#############.");
textArea.append("\t\n#############");
textArea.append("\t\n1.#############t.");
textArea.append("\t\n2.G#############t.");
String input1 = in.nextLine();
if(input1.equals("1")){
textArea.append("\n>Y#############");
textArea.append("\n>#############");
textArea.append("\n>A#############");
textArea.append("\n>1.#############");
textArea.append("\n>2.#############");
if(input.equals("1")){
textArea.append("\n>#############");
textArea.append("\n>#############");
textArea.append("\n>Ga#############d.");
}
if(input.equals("2")){
textArea.append("\n>#############");
textArea.append("\n>#############");
textArea.append("\n>Y#############ars");
textArea.append("\n>Y#############");
}
}
else if(input1.equals("2")){
textArea.append("\n>Y#############.");
textArea.append("\n>Y#############.");
}
}
}
}
}
这是我的文字冒险游戏,我被困在如何让用户输入的问题上。我读过 'action listener' 我不知道如何使用它,但我真的希望像在控制台或 terminal/cmd 程序中一样输入输入。用户只需输入 1、2 或 3 即可执行操作。
转储 Scanner
开始。使用 JTextField
。您需要了解您是在一个事件驱动的环境中操作,这意味着您需要使用 Observer Pattern 在某些事情发生变化时得到通知
首先查看 Creating a GUI With JFC/Swing, How to Use Text Fields, How to Write an Action Listeners 了解更多详情
对于初学者来说这里有漏洞:
myFrame.add(textArea);
**new JTextArea();**
textArea.setEditable(false);
(粗体字位于main的第一行,需要修改或去掉)
关于你的实际问题,我认为既然它是一个基于文本的游戏,你可以做这样的事情来接受用户输入:
(...)
public static void main(String[] args)
{
(...)
// create a scanner to read the command-line input
Scanner scanner = new Scanner(System.in);
bool running = true;
init();
//game loop
while (running)
{
if(scanner.next() == 1)
{
// do this
}else if ( == 2)
{
// do that
}//etc
//and simply change running to false, when you want the game to end
}
(...)
}
//this is simply a function to present the initial choices and general info to the user.
private void init(){System.out.println("Add instructions for the user here, e.g. telling them that they have to press 1,2, or 3");}
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
class DD {
public static void main(String[] args){
JFrame myFrame = new JFrame("#########");
myFrame.setSize(640,480);
myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
myFrame.setVisible(true);
JTextArea textArea = new JTextArea();
myFrame.add(textArea);
new JTextArea();
textArea.setEditable(false);
//System objects
Scanner in = new Scanner(System.in);
boolean running = true;
textArea.append("\t\n########################################");
textArea.append("\t\n>#############");
textArea.append("\t\n>Th#############11!");
textArea.append("\t\n>Typ#############enture!");
textArea.append("\t\n########################################");
String input = in.nextLine();
if(input.equals("start")){
{ ///beginning of story.
if(running)
textArea.append("\t\nYo#############.");
textArea.append("\t\n#############");
textArea.append("\t\n1.#############t.");
textArea.append("\t\n2.G#############t.");
String input1 = in.nextLine();
if(input1.equals("1")){
textArea.append("\n>Y#############");
textArea.append("\n>#############");
textArea.append("\n>A#############");
textArea.append("\n>1.#############");
textArea.append("\n>2.#############");
if(input.equals("1")){
textArea.append("\n>#############");
textArea.append("\n>#############");
textArea.append("\n>Ga#############d.");
}
if(input.equals("2")){
textArea.append("\n>#############");
textArea.append("\n>#############");
textArea.append("\n>Y#############ars");
textArea.append("\n>Y#############");
}
}
else if(input1.equals("2")){
textArea.append("\n>Y#############.");
textArea.append("\n>Y#############.");
}
}
}
}
}
这是我的文字冒险游戏,我被困在如何让用户输入的问题上。我读过 'action listener' 我不知道如何使用它,但我真的希望像在控制台或 terminal/cmd 程序中一样输入输入。用户只需输入 1、2 或 3 即可执行操作。
转储 Scanner
开始。使用 JTextField
。您需要了解您是在一个事件驱动的环境中操作,这意味着您需要使用 Observer Pattern 在某些事情发生变化时得到通知
首先查看 Creating a GUI With JFC/Swing, How to Use Text Fields, How to Write an Action Listeners 了解更多详情
对于初学者来说这里有漏洞:
myFrame.add(textArea);
**new JTextArea();**
textArea.setEditable(false);
(粗体字位于main的第一行,需要修改或去掉)
关于你的实际问题,我认为既然它是一个基于文本的游戏,你可以做这样的事情来接受用户输入:
(...)
public static void main(String[] args)
{
(...)
// create a scanner to read the command-line input
Scanner scanner = new Scanner(System.in);
bool running = true;
init();
//game loop
while (running)
{
if(scanner.next() == 1)
{
// do this
}else if ( == 2)
{
// do that
}//etc
//and simply change running to false, when you want the game to end
}
(...)
}
//this is simply a function to present the initial choices and general info to the user.
private void init(){System.out.println("Add instructions for the user here, e.g. telling them that they have to press 1,2, or 3");}