请教:如何添加一个button到frame中,并捕获其点击事件??

lsj_smile 2002-09-18 07:05:50
请教:如何添加一个button到frame中,并捕获其点击事件??比如,点击该按钮则关闭frame!!!
...全文
296 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
nirvana_hg 2002-09-28
  • 打赏
  • 举报
回复
如果你指点右上方那个小X关闭。在mian方法里加下列代码即可

cw.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);}});
nirvana_hg 2002-09-28
  • 打赏
  • 举报
回复
litthree(litthree)写的代码没有任何错误。我在1.4下测试完全正常。点击按钮正常退出。楼主应该是你的JDK的问题吧。
litthree 2002-09-28
  • 打赏
  • 举报
回复
我的这段程序实现对ActionEvent的响应是完全没问题的,lsj_smile(我笑):你说的没反应是否是指按了frame的关闭按钮呢?如果是的话,想实现它的功能要通过WindowEvents
sunlinux 2002-09-19
  • 打赏
  • 举报
回复

void jButton1_actionPerformed(ActionEvent e) {
this.dispose();
}改为
void jButton1_actionPerformed(ActionEvent e) {
System.exit(0);
}

aiur 2002-09-19
  • 打赏
  • 举报
回复
jbutton1.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseClicked(MouseEvent evt)
{
}
});
lsj_smile 2002-09-19
  • 打赏
  • 举报
回复
litthree(litthree) ( ) :为什么我按照你这段代码写上去,运行是可以的,可是点击按钮,它并没有关闭,什么反应都没有??
hlw1013 2002-09-19
  • 打赏
  • 举报
回复
给你我改的一个例子,里面包含这个功能的
点“0”的时候就关闭了frame
import java.awt.*;
import java.awt.event.*;
public class MyCalculator implements ActionListener{
Frame f;
Panel p;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;
TextField tf;
public static void main(String args[]){
MyCalculator mc=new MyCalculator();
mc.go();
}
public void go(){
f=new Frame("My Calculator");
p=new Panel();
p.setLayout(new GridLayout(4,4));
b1=new Button("7");
b2=new Button("8");
b3=new Button("9");
b4=new Button("+");
b5=new Button("4");
b6=new Button("5");
b7=new Button("6");
b8=new Button("-");
b9=new Button("1");
b10=new Button("2");
b11=new Button("3");
b12=new Button("*");
b13=new Button("0");
b14=new Button(".");
b15=new Button("=");
b16=new Button("/");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(b7);
p.add(b8);
p.add(b9);
p.add(b10);
p.add(b11);
p.add(b12);
p.add(b13);
p.add(b14);
p.add(b15);
p.add(b16);
f.add(p,"Center");
tf=new TextField();
f.add(tf,"North");
f.setSize(200,150);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
tf.setText(e.getActionCommand());
if(e.getActionCommand()=="0"){
System.exit(0);
}
}
}
lsj_smile 2002-09-19
  • 打赏
  • 举报
回复
我对litthree(litthree) 程序中的System.exit(0);改为b.setLable("关闭"),一切正常,可是用了System.exit(0);它并不关闭该frame。到底是为什么???我一定给高分。
namowen 2002-09-18
  • 打赏
  • 举报
回复
JButton jButton1 = new JButton();
this.add(jButton1);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});

void jButton1_actionPerformed(ActionEvent e) {
this.dispose();
}
litthree 2002-09-18
  • 打赏
  • 举报
回复
import java.awt.*;
import java.awt.event.*;

class ClosingWindow extends Frame{
private Button b;
public ClosingWindow(){
super("Closing Window");
b=new Button("Close Window");
}


public void lanchFrame(){
this.setLayout(new FlowLayout());
this.setSize(300,300);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
this.add(b);
this.setVisible(true);
}

public static void main(String[] args){
ClosingWindow cw = new ClosingWindow();
cw.lanchFrame();
}
}

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧