?? chap12-1.txt
字號:
// 程序12-1
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// 為了響應JButton事件處理,testEventButton類必須實現ActionListener接口
public class testEventButton implements ActionListener{
JFrame frame;
Container contentPane;
JButton button1,button2;
JLabel label;
// 必須實現ActionListener接口中的方法actionPerformed( )
public void actionPerformed(ActionEvent e){
label.setText("You pressed Button: "+e.getActionCommand( ));
}
public testEventButton( ){ // 定義構造函數
frame=new subJFrame("testEventButton");
contentPane=frame.getContentPane( );
contentPane.setLayout(new BorderLayout(5,5)); // 設置內容格的布局
label=new JLabel(" "); // 生成一個沒有顯示文本的標簽
contentPane.add(label,BorderLayout.CENTER);
// 創建兩個按鈕對象
button1=new JButton("A");
button2=new JButton("B");
contentPane.add(button1,BorderLayout.NORTH);
contentPane.add(button2,BorderLayout.SOUTH);
// 將當前對象作為button1和button2對象的監聽者
button1.addActionListener(this);
button2.addActionListener(this);
frame.setSize(200,150);
frame.show( );
}
public static void main(String args[ ]){
testEventButton obj=new testEventButton( );
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -