2013-06-19 27 views
0

我想使用图形用户界面进行总结性项目的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);$ 

我试图得到它使每一个我按下按钮,将更新到下一 段时间,目前我一直无法找到如何做这种事。

+0

btw你的构造函数必须像大写的类名一样,问题是你注册了两次sameActionListener到按钮本身,你想做什么我did not understand – nachokk

+0

我想要做的很多事情是所以当你按下optionA时,它会执行第一个if语句,然后会出现A或B,如果再次按A,它会进入下一个,所以它会分支出来。我希望他们澄清一下?而对于班级名称,我在这里更改了它的名称,因为它涉及我的名字在实际版本上,因为它是总结性的。还有一种方法可以将它从optionA更改为新的,这样我可以在我的代码中工作吗?或没有? – user2501851

+0

源代码中的单个空白行是* always *就足够了。 –

回答

1

你正在将两个动作命令设置在同一个类中......那么,根据上下文,动作命令将有不同的命令,对吧?然后,您需要为这些情况设置不同的听众。在课堂上创建一个对象(以我愚蠢的观点)是冗长和堆积的。你可以试试这个:

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所说的那样,重写你的构造函数来与类名配对,否则你会得到一个错误。

另一件事......将这个游戏分成不同的对象,以保持其私人状态并使扩展和维护变得容易,这将是很好的。

+0

谢谢你们,我一直在与你们说什么搞混,但现在我只是困惑于一件事。我有行动命令if语句。这是一种很好的方式吗,还是更容易用更好的东西取代它们?因为我不确定action语句如果语句以及它如何完全工作。谢谢您的帮助! – user2501851

+0

嗯,我相信用某些书写得很好的东西比较好。我对动作命令知之甚少,但设置动作侦听器似乎更确定该元素将执行某个代码块,而不依赖于if语句。 – thepanuto

+0

问题是,当我得到它执行,它跳到最后一个选项,或根本没有改变。我似乎无法让它一步一步地走下去 – user2501851

相关问题