吃飯睡覺不打豆豆
阿新 • • 發佈:2020-12-17
技術標籤:筆記
點選元件按鈕,作出反應
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);
}
}