【问题标题】:My Java Actionlistener won't change a JTextArea when a JButton is pressed按下 JButton 时,我的 Java Actionlistener 不会更改 JTextArea
【发布时间】:2013-05-01 16:37:28
【问题描述】:

这是我几乎完成的选择你自己的冒险游戏。 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);
                        };

                    };

【问题讨论】:

  • 我的天啊,我喜欢阅读对问题的糟糕描述,并在大量的 sn-p 代码中滚动查看有问题的部分。谢谢。
  • 那是一个巨大的sn-p代码没有行号:-(
  • 像这个例子这样好的演示文稿,真的很有帮助。哈哈
  • 有时,我喜欢向后靠,想象这些“开发人员”与他们的同学/同事的对话。
  • 呃。对十亿个 if-else-if 的每个按钮的操作侦听器使用匿名或内部类。这将为您和其他阅读您的代码的人简化它。

标签: java swing jbutton actionlistener jtextarea


【解决方案1】:

你的问题是:

你有两个主要检查:

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
   }

【讨论】:

  • 你的前几段我没看懂。你能解释一下吗?
  • 我的意思是,如果您更改选项 2 按钮上的文本,它仍然是选项 2 按钮,因此当您在更改其上的文本后再次单击它时,您将转到主要条件之一有,我不知道怎么解释,但是一旦你点击按钮,actionlistner就会运行,即使你更改文本并再次点击按钮,它也会从头开始调用action方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-11
  • 2013-12-22
  • 1970-01-01
  • 2012-02-07
  • 1970-01-01
  • 2020-08-24
  • 2011-07-09
相关资源
最近更新 更多