import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class FlaySnow extends JFrame{
//实例出一个按钮
JButton buttonOK=new JButton("确定");
//标签控件
JLabel label=new JLabel("hello world");JButton buttoncancel=new JButton("取消");
FlaySnow(){
//设置窗体的标题
this.setTitle("java");
//设置窗体的大小 x,y,width,height
this.setBounds(10, 10, 240, 200);
this.getContentPane().setLayout(null);
//设置确定按钮的大小 x,y,width,height
buttonOK.setBounds(20, 20, 80, 30);
//把按钮增加到窗体中去
this.getContentPane().add(buttonOK);
//设置确定按钮的大小 x,y,width,height
buttoncancel.setBounds(120, 20, 80, 30);this.getContentPane().add(buttoncancel);
//设置label的大小 x,y,width,height
label.setBounds(5,50,180,30);
//把label增加到窗体中去
this.getContentPane().add(label);
//增加按钮事件监听
buttonOK.addActionListener(new AbstractAction(){@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//设置触发buttonOK时显示的文本内容
label.setText("你点击的是确定按钮");}
});
buttoncancel.addActionListener(new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
label.setText("取消");
}
});
}
public static void main(String[] args){
//控制台输出this is buttontest
System.out.println("this is buttontest");
//实例化出一个窗体
FlaySnow myframe=new FlaySnow();
//设置窗体可见
myframe.setVisible(true);}
}