发布网友 发布时间:2022-04-22 22:52
共1个回答
热心网友 时间:2023-10-07 07:00
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();
}
}