gpt4 book ai didi

java - 使用 GUI 的基于文本的冒险游戏存在无法通过按钮继续操作的问题

转载 作者:行者123 更新时间:2023-12-01 14:25:42 25 4
gpt4 key购买 nike

我正在尝试使用 GUI 在 Java 中制作一个基于文本的冒险游戏作为一个总结性项目,我遇到的问题是当我按下选项 A 时,它会工作一次,然后就不再工作。我还没有添加任何选项 B 的东西,因为如果我一开始就没有办法让它发挥作用,它就没有任何好处。 导入java.awt.*;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Project extends JFrame implements ActionListener {

private static final int WIDTH = 840;

private static final int HEIGHT = 480;



private JLabel gameText;

private JButton optionA, optionB, exitB;



private ExitButtonHandler ebHandler;



public project()

{

gameText = new JLabel("<HTML><br>You wake up in a forest, there is a path which
heads north. There also seems to be a old trail that leads deeper in the woods.</br>
</html>");


optionA = new JButton("Head North");
optionA.setActionCommand("optionA");
optionA.addActionListener(this);
optionB = new JButton("Go deeper into the forest.");
optionB.setActionCommand("optionB");
optionB.addActionListener(this);
exitB = new JButton("Exit");

ebHandler = new ExitButtonHandler();

exitB.addActionListener(ebHandler);


setTitle("Adventuregame");

Container pane = getContentPane();

pane.setLayout(new GridLayout(4, 2));




pane.add(gameText);

pane.add(optionA);

pane.add(optionB);

pane.add(exitB);



setSize(WIDTH, HEIGHT);

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}
public void actionPerformed(ActionEvent e)
{

if(e.getActionCommand().equals("optionA") )
{
gameText.setText("<HTML><br>You find yourself on the pathway to a major capital
city, the walls of the kingdom head towards the sky and the gate is wide open, you can
hear the sounds of busy life from inside the castle gates. As you head in, a guard
confronts
you and asks why you are there.</br></HTML>");
optionA.setText("I'm lost and trying to find my way.");
optionB.setText("Ignore the guard");
optionA.setActionCommand("optionA2");
optionA.addActionListener(this);
if(e.getActionCommand().equals("optionA2"))
{
gameText.setText("<HTML><br>The guard checks you for weapons, finding nothing he
takes you to the tavern to find someone who may be able to help you out. In the Tavern
there are some drunkards singing in the corner and a band of mercenaries on your right.
At the bar there is a older man whom you seem to reconise</br></HTML>");
optionA.setText("Go towards the Mercenaries.");
optionB.setText("Go to the Older Man.");
optionA.setActionCommand("optionA3");
optionA.addActionListener(this);$

我正在尝试获取它,以便每次按下按钮时它都会更新到下一个部分,目前我无法找到有关如何执行此操作的任何内容。

最佳答案

您将两个操作命令设置在同一个类中...那么,操作命令将根据上下文具有不同的命令,对吗?然后,你需要针对这些情况有不同的听众。在类中创建对象(以我的拙见)是冗长且消耗堆的。你可以试试这个:

    public class Project {
//do your stuff for the class here and, in the constructor...
public Project(){
this.optionA = new JButton("First option.");
this.optionB = new JButton("Second option.");
this.optionA.setActionListener(new ActionListener(){
// enter the code for the optionA listener.
});
this.optionB.setActionListener(new ActionListener(){
// enter the code for the optionB listener.
});
}
}

如果您想更改它,只需在需要时重置操作监听器即可。

正如 nachokk 所说,重写你的构造函数以与类名配对,否则你会得到一个错误。

另一件事...将这个游戏拆分为不同的对象,以维护它们的私有(private)状态并使扩展和维护变得容易,这会很好。

关于java - 使用 GUI 的基于文本的冒险游戏存在无法通过按钮继续操作的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17195476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com