package com.test;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class aaa extends JFrame implements ActionListener
{
JButton b1, b2 ; //定义两个按钮
JLabel l1;
public aaa() //构造函数
{
b1 = new JButton("显示"); //初始化按钮
b2 = new JButton("退出");
l1 = new JLabel("This is a test");
this.setLayout(new FlowLayout()); //设置主窗体布局
this.add(b1);
this.add(b2);
this.add(l1);
l1.setVisible(false);
b1.addActionListener(this);//添加监听
b2.addActionListener(this);
pack();
setBounds(130, 50, 1000, 600); //设置窗口大小
setTitle("Test"); //标题
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //退出
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == b1 )
{
l1.setVisible(true);
}
if(e.getSource() == b2)
{
this.dispose();
}
}
/**
* 测试主函数
* @param args
*/
public static void main(String []args)
{
new aaa();
}
}
哥们我自己写的。。
绝对正确,已经测试过了