Java-来自按钮的响应



我尝试了很多方法来获得这个按钮bb或继续输出"嘿,巴迪",但仍然不起作用。它显示了,但当我按下它时,什么也没发生。该代码同时使用java swing和awt。

package Game;
import java.awt.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.util.concurrent.TimeUnit;
import javax.swing.JButton;
public class base extends java.applet.Applet implements ActionListener, TextListener {
    //Graphics
    //Graphics
    @SuppressWarnings("deprecation")
    public static JButton bb = new JButton("Continue");
    public TextArea ta = new TextArea(30, 140);
    TextArea tb = new TextArea(3, 130);
    public int counter = 0;
    //main class
    public static void main(String[] args) {
        Frame f = new Frame("---Quest---");
        base ex = new base();
        ex.init();
        f.add("Center", ex);
        f.pack();
        f.show(true);
        bb.addActionListener(ex);
    }
    public void actionPerformed1(ActionEvent Continue) {
        bb.addActionListener(this);
        counter++;
        if (Continue.getSource() == bb && counter == 1) {
            tb.append("Hey Buddy");
        }
    }
    //graphics
    public void init() {
        bb.addActionListener(this);
        Panel p;
        setLayout(new BorderLayout());
        p = new Panel();
        ta.append("Hey");
        bb.addActionListener(this);
        p.add(bb);
        p.add(ta);
        p.add(tb);
        p.setBackground(Color.blue);
        ta.setBackground(Color.cyan);
        ta.setEditable(false);
        add("Center", p);
        p.setVisible(true);
    }
    //time class
    public static int nap(int time) {
        try {
            TimeUnit.SECONDS.sleep(time);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return 0;
    }
//end of code       
    @Override
    public void textValueChanged(TextEvent arg0) {
        // TODO Auto-generated method stub
    }
    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
    }
}

整个代码都有缺陷。在线阅读评论。

1.我知道你为什么要在按钮上添加actionListener 4次(保留一次)

2.在实现ActionListener并将this分配给按钮的ActionListener 时,您必须将actionPerformed1更改为actionPerfromed

public TextArea ta = new TextArea(30, 140);
TextArea tb = new TextArea(3, 130);
public int counter = 0;
//main class
public static void main(String[] args) {
    f.show(true);//show is deprecated use setVisible(true) instead;
    bb.addActionListener(ex);//1
}
public void actionPerformed1(ActionEvent Continue) {//have to change the actionPerformed1 to actionPerfromed
    bb.addActionListener(this);//2 What is this assigning inside actionPerformed Need to be removed
    counter++;
    if (Continue.getSource() == bb && counter == 1) {
        tb.append("Hey Buddy");
    }
}
//graphics
public void init() {
    bb.addActionListener(this);//3
    Panel p;
    setLayout(new BorderLayout());
    p = new Panel();
    ta.append("Hey");
    bb.addActionListener(this);//4
    p.add(bb);
    p.setVisible(true);//already called a show for JFrame why you want to set Visible of Panel
}

最新更新