gpt4 book ai didi

Java - 来自按钮的响应

转载 作者:行者123 更新时间:2023-12-02 04:33:36 25 4
gpt4 key购买 nike

我已经尝试了很多方法来获取此按钮bb或继续输出“Hey Buddy”,但它仍然不起作用。它已显示,但当我按下它时什么也没有发生。该代码同时使用了 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.您必须更改 actionPerformed1actionPerfromed当您实现ActionListener时并分配this到按钮的 ActionListener

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
}

关于Java - 来自按钮的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31125173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com