我想使用图形用户界面进行总结性项目的Java基于文本的冒险游戏,我遇到的问题是当我按下选项A ,它会起作用一次,之后不起作用。我还没有添加任何选项B的东西,因为如果我没有办法让它在第一时间工作,它不会做任何好事。 import java.awt。*;基于文本的冒险游戏使用GUI有能够继续按下按钮的问题
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);$
我试图得到它使每一个我按下按钮,将更新到下一 段时间,目前我一直无法找到如何做这种事。
btw你的构造函数必须像大写的类名一样,问题是你注册了两次sameActionListener到按钮本身,你想做什么我did not understand – nachokk
我想要做的很多事情是所以当你按下optionA时,它会执行第一个if语句,然后会出现A或B,如果再次按A,它会进入下一个,所以它会分支出来。我希望他们澄清一下?而对于班级名称,我在这里更改了它的名称,因为它涉及我的名字在实际版本上,因为它是总结性的。还有一种方法可以将它从optionA更改为新的,这样我可以在我的代码中工作吗?或没有? – user2501851
源代码中的单个空白行是* always *就足够了。 –