0

这是我几乎完成的选择你自己的冒险游戏。Actionlistener 坏了。它适用于“互联网”部分,但 Actionlistener 的其余部分不会使按钮更改文本。

Actionlistener 的其余部分与第一部分非常相似。

请帮忙。

编辑:取出不相关的部分

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.Border;

public class TheGame extends JPanel implements ActionListener{

/**
 * @param args
 */

String gameText = "You wake up in the morning feeling like a sir. As is tradition, you need electronics of any kind to keep your mind running (going outside is out of the question. Nothing exciting in the real world). There are many options available to you, but the internet and games are the ones that appeal the most. Do you want to surf the web or use an app?";
private final JTextArea adventureArea;
private static final long serialVersionUID = 1L;
 JButton option1;
 JButton option2;

    public TheGame(){

        option1 = new JButton("Click here for teh interwebs");
         option1.addActionListener(this);
        option1.setPreferredSize(new Dimension(300, 50));
         option1.setVisible(true);

        option2 = new JButton("Click here for teh entertainments");
        option2.addActionListener(this);
         option2.setPreferredSize(new Dimension(300, 50));
         option2.setVisible(true);


        this.adventureArea = new JTextArea(24, 80);
        adventureArea.setFont(new Font("Serif", Font.PLAIN, 16));
        adventureArea.setLineWrap(true);
        adventureArea.setWrapStyleWord(true);
        adventureArea.setEditable(true);
        adventureArea.setText(gameText);





    }

动作监听器:

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==option1){//internets

            String gameText="";
            adventureArea.setText(gameText);
            option1.setText("Dark!");
            option2.setText("Light!");
            if(e.getSource()==option1){//Dark

                //String gameText="";
                //adventureArea.setText(gameText);

            }if (e.getSource()==option2){//Light
                //gameText="");
                option1.setText("Ponies!");
                option2.setText("Videos!");
                if(e.getSource()==option1){//Ponies

                    gameText="";
                    adventureArea.setText(gameText);
                    option1.setText("Read!");
                    option2.setText("Socialize!");
                    if(e.getSource()==option1){//Read
                        gameText="";
                        adventureArea.setText(gameText);
                        option1.setText("Leave!");
                        option2.setText("Leave!");

                        if(e.getSource()==option1){//leave

                            System.exit(0);
                        }else if (e.getSource()==option2){//leave

                            System.exit(0);
                        };
                    }else if (e.getSource()==option2){//socialize
                        gameText="";
                                adventureArea.setText(gameText);
                                option1.setText("Leave!");
                                option2.setText("Leave!");
                        if(e.getSource()==option1){//leave

                            System.exit(0);
                        }else if (e.getSource()==option2){//leave

                            System.exit(0);
                        };

                    };
4

1 回答 1

0

你的问题是:

您有两个主要检查:

if (e.getSource() == option1) {//internets
...
}

 else if (e.getSource() == option2) {//Entertainments
           ...      
      }

您认为如果用户单击第一个按钮并更改其上的信息,那么您再次单击同一个按钮它将运行第一个if条件内的代码

当你点击按钮时,你会做一个动作,首先要做的是检查你点击了哪个按钮,即使你改变了按钮上的文字,这个按钮也是一样的,

说明:如果您单击,option1 = new JButton("Click here for teh interwebs");则第一个 if 条件将匹配,因此它将将此按钮上的文本更改为option1.setText("Dark!");,然后单击“Dark”(选项2),然后您将再次进入该操作,该操作将检查该按钮(选项2)所以第二个if条件将匹配


而不是嵌套if条件,将条件更改为使用按钮的文本进行检查,就像这样: if (e.getSource() == option1 && option1.getText().equals("Dark!"))


例如:为了改变Click here for teh interwebs

   if (e.getSource() == option1 && option1.getText().equals("Click here for teh interwebs")) {//internets
   } else if (e.getSource() == option1 && option1.getText().equals("Dark!")) {//Dark         
   } else if (e.getSource() == option2 && option2.getText().equals("Light!")) {//Light
   } else if (e.getSource() == option1 && option1.getText().equals("Ponies!")) {//Ponies
   } else if (e.getSource() == option1 && option1.getText().equals("Read!")) {//Read
   } else if (e.getSource() == option2 && option2.getText().equals("Socialize!")) {//socialize
   } else if (e.getSource() == option2 && option2.getText().equals("Videos!")) {//Videos
   } else if (e.getSource() == option1 && option1.getText().equals("Youtube!")) {//Youtbe
   } else if (e.getSource() == option1 && option1.getText().equals("Dubstep videos!")) {//Dubstep
   } else if (e.getSource() == option2 && option2.getText().equals("Gaming videos!")) {//Gaming
   } else if (e.getSource() == option2 && option2.getText().equals("Netflix!")) {//Netflix
   } else if (e.getSource() == option1 && option1.getText().equals("MLP:FIM!")) {//MLP
   } else if (e.getSource() == option2 && option2.getText().equals("Whatever the third recommendation on your ‘Suggested’ list is!!")) {//Random
   }
于 2013-05-01T16:55:00.180 回答