当前位置: 技术问答>java相关
请教:如何添加一个button到frame中,并捕获其点击事件??
来源: 互联网 发布时间:2015-09-19
本文导语: 请教:如何添加一个button到frame中,并捕获其点击事件??比如,点击该按钮则关闭frame!!! | import java.awt.*; import java.awt.event.*; class ClosingWindow extends Frame{ private Button b; public ClosingW...
请教:如何添加一个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();
}
}
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();
}
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);
}
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”的时候就关闭了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);
}
}
}