到目前为止,我有一个主类、消息类和答案类。消息和答案存储在单独的数组中,因此我可以在需要时将它们取出。但是,我不确定如何进行。我需要检查用户在点击继续按钮时输入的内容以及该消息的相应答案数组。(一条消息将有自己的一系列答案要检查)。如果有更好的方法来做到这一点,即每条消息都有一个案例声明,请告诉我。这是代码:
主要的:
package com.game.main;
import java.applet.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main extends Applet {
private TextField commandInput;
private TextArea messageDisplay;
private Button button;
public Message messages;
private String currentLevel;
private boolean running = false;
private Thread thread;
private ActionListener action;
public synchronized void start() {
running = true;
thread = new Thread();
thread.start();
}
public synchronized void stop() {
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// Initialisation method
public void init() {
super.init();
messages = new Message();
// Define colours
setBackground(Color.white);
setForeground(Color.black);
Panel appletPanel = new Panel();
// Use a border layout
BorderLayout b = new BorderLayout();
appletPanel.setLayout(b);
add(appletPanel);
this.setSize(800, 400);
// Define UI items
commandInput = new TextField(20);
messageDisplay = new TextArea(null, 20, 60, TextArea.SCROLLBARS_NONE); // 20 rows x 60 chars
button = new Button("Proceed");
Panel inputPanel = new Panel();
// Add components to our layout / panels
inputPanel.add(commandInput);
inputPanel.add(button);
appletPanel.add(messageDisplay, "North");
appletPanel.add(inputPanel, "South");
currentLevel = messages.messageArray[0];
messageDisplay.append(messages.messageArray[0]);
run();
}
// game loop
public void run() {
requestFocus();
// read as while "running == true"
while (running) {
}
}
}
留言:
package com.game.main;
public class Message {
public String[] messageArray;
public Message() {
messageArray = new String[50];
messageArray[0] = "Welcome.";
}
}
答案:
package com.game.main;
public class Answers {
public String[] introAnswers;
public Answers() {
introAnswers = new String[2];
introAnswers[0] = "Go";
introAnswers[1] = "go";
}
}