Java 如何通过按下按钮进行游戏对话?
我正在尝试制作一个基于文本的RPG,但对如何进行故事对话有一个疑问(我对Java还是很陌生) 我想通过按下一个按钮来更改文本区域的内容,这样每次按下按钮时对话框都会继续 例如,如果有这样一个对话框 你好。我以前从未见过你的脸 你看起来像个冒险家 你也是来杀那个巫师的 我想一个接一个地显示这些文本,而不是一次显示 这是我的密码:Java 如何通过按下按钮进行游戏对话?,java,jtextarea,Java,Jtextarea,我正在尝试制作一个基于文本的RPG,但对如何进行故事对话有一个疑问(我对Java还是很陌生) 我想通过按下一个按钮来更改文本区域的内容,这样每次按下按钮时对话框都会继续 例如,如果有这样一个对话框 你好。我以前从未见过你的脸 你看起来像个冒险家 你也是来杀那个巫师的 我想一个接一个地显示这些文本,而不是一次显示 这是我的密码: package test; import java.awt.*; import java.awt.event.*; import javax.swing.JFrame;
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import java.awt.Container;
import javax.swing.*;
public class Dialogue extends JFrame implements ActionListener
{
//Window
JFrame window;
Container con;
//Font
Font basicfont;
Font bigfont;
Font timesnewroman;
//Main game screen
JPanel storyP;
JPanel inputP;
JPanel enterP;
//Main game screen Panel 3
JTextArea storyT;
//Main game screen Panel 5
JLabel inputA;
JTextField input;
JButton enterB;
public static void main(String[]args)
{
Dialogue game = new Dialogue();
game.setup();
}
public void setup()
{
window = new JFrame();
window.setBounds(0,0,1200,950);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setBackground(Color.black);
window.setLayout(null); //Disabling the default layout.
window.setVisible(true);
basicfont = new Font("MS Gothic", Font.PLAIN, 24);
bigfont = new Font("MS Gothic", Font.BOLD, 28);
con = window.getContentPane();
// Panel Setup
storyP = new JPanel(); //This is where story text is displayed.
storyP.setBounds(50, 500, 800, 320);
storyP.setBackground(Color.white);
storyP.setLayout(new GridLayout());
inputP = new JPanel();
inputP.setBounds(50, 830, 500, 50);
inputP.setBackground(Color.black);
inputP.setLayout(new BorderLayout());
//p5.setLayout(new GridLayout());
enterP = new JPanel();
enterP.setBounds(600, 830, 120, 50);
enterP.setBackground(Color.black);
//STORY TEXT SETUP
storyT = new JTextArea();
storyT.setFont(basicfont);
storyT.setBackground(Color.black);
storyT.setForeground(Color.white);
storyT.setEditable(false);
//INPUT BOX SETUP
inputA = new JLabel(">");
inputA.setBackground(Color.black);
inputA.setForeground(Color.white);
inputA.setFont(bigfont);
input = new JTextField(15);
input.setBackground(Color.black);
input.setForeground(Color.white);
input.setBorder(null);
input.setFont(bigfont);
input.addActionListener(this);
enterB = new JButton("ENTER");
enterB.setFont(timesnewroman);
enterB.addActionListener(this);
//ADDING
storyP.add(storyT);
inputP.add("West",inputA);
inputP.add(input);
enterP.add(enterB);
//VISIBILITY
con.add(storyP);
con.add(inputP);
con.add(enterP);
Opening();
}
public void Opening()
{
storyT.setText("Hello. I have never seen your face before.");
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==enterB)
{
storyT.setText("You look like an adventurer.");
}
}
}
我可以通过按我创建的回车键将文本从“你好,我以前从未见过你的脸”改为“你看起来像个冒险家”
但我不知道如何从这一点进一步(显示“那么你也是来杀死那个向导的?”),因为我的ENTER按钮的操作被指定显示一个特定的文本(“你看起来像个冒险家”)。我甚至不知道在哪里写第三行,所以它还没有写在这段代码中。可能有一个计数器和一个looong开关:
counter++;
switch(counter){
case 0: storyT.setText("You look like an adventurer.");
case 1: storyT.setText("So you also came to kill that wizard?");
case 2: ....
}
但是如果你有成百上千的文本,那就太糟糕了。
为此,我将使用字符串数组或ArrayList。然后,您可以使用计数器获取要打印的文本索引
如果你能把这些文本归档,那就更好了。然后你就可以用Scanner类逐行读取了
Scanner sc = new Scanner(new File("texts.txt"));
ArrayList<String> texts = new ArrayList<String>();
while(sc.hasNextLine())
text.add(sc.nextLine());
sc.close();
变量文本应声明为实例变量,而不是在方法体中:
private ArrayList<String> texts;
....
....
....
texts=new ArrayList<String>();
私有数组列表文本;
....
....
....
text=新的ArrayList();
在你的情况下,我会这样做
声明一个堆栈
Stack<String> texts = new Stack<String>();
然后,在您的actionlistener中,您只需这样做,而不是您所拥有的:
storyT.setText(texts.pop());
如果您需要一些安全措施,可以这样做:
if (texts.hasnext())
storyT.setText(texts.pop());
这将检查堆栈中是否仍存储有文本。否则,堆栈将不会弹出,在这种情况下,您将得到null,这是不好的。您可以使用包含整个会话的字符串数组。而不是在用户按enter键时简单地迭代。你可以使用一个计数器来切换到对话的下一个部分,或者其他一些聪明的方法。是的,这似乎是一种方法。谢谢你提供的信息:-)谢谢!我试过你的方法,似乎效果不错。但我有一个问题。按Enter按钮时,“我的当前代码”首先显示第三行。然后显示第2行,然后显示第1行。所以顺序颠倒了。我可以通过以相反的顺序编写对话来纠正顺序,但这会使代码混乱,所以希望我能避免这种情况。有办法解决这个问题吗?没有,堆栈就是这样工作的。使用push()输入的第一件事就是使用pop()输出的第一件事。因此,只需按适当的顺序添加字符串。在您的情况下,只需颠倒推字符串的顺序即可。我可以建议用链表代替吗。读一读吧!我用计数器和开关试过你的方法,效果很好!关于ArrayList和Scanner的方法…听起来很有希望,实际上我有一种感觉,我迟早会采用这种方法。考虑到我目前的Java水平,扫描另一个文件中的数据听起来仍然有点高级,但我会尝试。非常感谢您提供的所有有用信息!我知道你的意思,扫描仪有点先进,不管怎样,我很高兴它对@Ryi有帮助
storyT.setText(texts.pop());
if (texts.hasnext())
storyT.setText(texts.pop());