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

请教:如何添加一个button到frame中,并捕获其点击事件??比如,点击该按钮则关闭frame!!!

|

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();

}

}

|

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();

}

|

void jButton1_actionPerformed(ActionEvent e) {

this.dispose();

}改为

void jButton1_actionPerformed(ActionEvent e) {

System.exit(0);

}

|

给你我改的一个例子,里面包含这个功能的

点“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);

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值