点击组件按钮,作出反应
package two;
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.JOptionPane;
public class HH extends JFrame{
private JButton button1=new JButton("不许打我!");
public HH()
{
setTitle("传说中的豆豆");
setSize(250,150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口默认关闭
setLocationRelativeTo(null);
setLayout(new FlowLayout());//设置布局管理器
//添加事件
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(null,"你打我了,我要咬你!");
}
});
add(button1);
}
public static void main(String[] args)
{
HH A=new HH();
A.setVisible(true);
}
}