0

総括プロジェクトの GUI を使用して Java でテキスト ベースのアドベンチャー ゲームを作成しようとしています。そもそもオプション B を機能させる方法がないと何の役にも立たないので、オプション B のものは追加していません。import 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);$

ボタンを押すたびに次のセクションに更新されるようにしようとしていますが、現在、これを行う方法について何も見つけることができませんでした。

4

1 に答える 1

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 が言ったように、クラス名とペアになるようにコンストラクターを書き直してください。そうしないと、エラーが発生します。

もう 1 つ... このゲームをさまざまなオブジェクトに分割して、プライベートな状態を維持し、拡張とメンテナンスを容易にするとよいでしょう。

于 2013-06-19T17:44:25.033 に答える