建立一个按钮“请按这里,单击此按钮交替显示文本“您按下了奇数次按钮”和“您按下了偶数次按钮”。


package ButtonDemo;
import java.awt.*;
import java.awt.event.*;
class ButtonDemo extends WindowAdapter implements ActionListener{
Frame f;Button b;TextField tf;int flag=0;
public static void main(String args[])
{
ButtonDemo bt=new ButtonDemo();
bt.init(); }
void init(){
f=new Frame("请按例题");
b=new Button("请按这里");
b.addActionListener(this);
f.add(b,"South");
tf=new TextField();
f.add(tf,"Center");
f.addWindowListener(this);
f.setSize(300,300);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
String s1="您按下了奇次数按钮";
String s2="您按下了偶次数按钮";
if(flag==0){tf.setText(s1);flag=1;}
else{tf.setText(s2);flag=0;}
}
public void windowClosing(WindowEvent e){
System.exit(0);
}
}

 

 

posted on 2017-09-16 23:34  小星_log  阅读(445)  评论(0编辑  收藏  举报