?? app16_3.java
字號:
// app16_3, 加入可關閉窗口的按鈕
import java.awt.*;
import java.awt.event.*;
public class app16_3 extends Frame implements ActionListener
{
static app16_3 frm=new app16_3();
static Button btn1=new Button("Yellow");
static Button btn2=new Button("Green");
static Button btn3=new Button("Exit");
public static void main(String args[])
{
btn1.addActionListener(frm); // 把事件聆聽者frm向btn1注冊
btn2.addActionListener(frm); // 把事件聆聽者frm向btn2注冊
btn3.addActionListener(frm); // 把事件聆聽者frm向btn3注冊
frm.setTitle("Action Event");
frm.setLayout(new FlowLayout(FlowLayout.CENTER));
frm.setSize(200,150);
frm.add(btn1);
frm.add(btn2);
frm.add(btn3);
frm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Button btn=(Button) e.getSource(); // 取得事件源對象
if(btn==btn1) // 如果是按下btn1按鈕
frm.setBackground(Color.yellow);
else if(btn==btn2) // 如果是按下btn2按鈕
frm.setBackground(Color.green);
else // 如果是按下btn3按鈕
System.exit(0);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -