?? multilistener.java
字號:
import java.awt.*;
import java.awt.event.*;
public class MultiListener extends Frame implements ActionListener {
TextArea topTextArea;
TextArea bottomTextArea;
Button button1, button2;
public MultiListener(String s) {
super( s);
Label l = null;
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setLayout(gridbag); //Frame設置為GridBagLayout布局管理器。
c.fill = GridBagConstraints.BOTH;
c.gridwidth = GridBagConstraints.REMAINDER;
l = new Label("監聽器聽到的:");
gridbag.setConstraints(l, c);
add(l);
c.weighty = 1.0;
topTextArea = new TextArea(5, 20);
topTextArea.setEditable(false);
gridbag.setConstraints(topTextArea, c);
add(topTextArea);
c.weightx = 0.0;
c.weighty = 0.0;
l = new Label("偷聽者聽到的:");
gridbag.setConstraints(l, c);
add(l);
c.weighty = 1.0;
bottomTextArea = new TextArea(5, 20);
bottomTextArea.setEditable(false);
gridbag.setConstraints(bottomTextArea, c);
add(bottomTextArea);
c.weightx = 1.0;
c.weighty = 0.0;
c.gridwidth = 1;
c.insets = new Insets(10, 10, 0, 10);
button1 = new Button("啦 啦 啦");
gridbag.setConstraints(button1, c);
add(button1);
c.gridwidth = GridBagConstraints.REMAINDER;
button2 = new Button("你別說話!");
gridbag.setConstraints(button2, c);
add(button2);
//當前MultiListener對象同時監聽兩個Button的事件。
button1.addActionListener(this);
button2.addActionListener(this);
//為第二個Button再注冊一個監聽器。
button2.addActionListener(new Eavesdropper());
//向窗口注冊響應關閉窗口操作的監聽器。
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
topTextArea.append(e.getActionCommand() + "\n");
}
//第二個Button的監聽器類。
class Eavesdropper implements ActionListener {
public void actionPerformed(ActionEvent e) {
bottomTextArea.append("OK,"+e.getActionCommand() + "\n");
}
}
public static void main(String[] args){
MultiListener m=new MultiListener("Multilistener example" );
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -