雖然事件接口和事件源繁多復雜, 但是處理事件的基本原理卻很簡單:
即想要接受某個事件的類實現該事件監聽器的接口,然后在事件源中注冊自己,這樣它就可以接收所要的事件,并通過監聽器接口中的方法進行處理。此文中使用了三個監聽器對象,一個是隨機顏色按鈕的監聽器ColorActionListener,還有另外兩個new出來的分別實現了顏色變暗和顏色變亮的監聽器對象。
事件源分別是隨機顏色Button的ActionEvent對象,顏色變暗和顏色變亮的ActionEvent對象。
當事件源對象發生ActionEvent事件的時候,會產生ActionEvent對象,此時編譯器會把這個事件對象傳個注冊在該事件源的接口,由相應接口執行操作。充分體現了用戶為主的操作體驗,根據用戶的動作來做出響應。這是事件驅動機制很好的地方,個人拙見。呵呵
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonFrame extends SimpleFrame{
public ButtonFrame(int width,int height){
super(width,height);
setTitle("這是個按鈕事件?。?);
ColorPanel panel=new ColorPanel();
Container contentPane=getContentPane();//返回此窗體的contentPane 對象
contentPane.add(panel);
}
/*注釋用:getContentPane
public Container getContentPane()
返回此窗體的 contentPane 對象
指定者:
接口 RootPaneContainer 中的 getContentPane
返回:
contentPane 屬性*/
//定義主函數,用來驅動ButtonFrame類
public static void main(String[] args) {
ButtonFrame frame=new ButtonFrame(400,300);
frame.setVisible(true);
}
}
class ColorPanel extends JPanel{
public ColorPanel(){
//創建組件
colorButton =new JButton("隨機顏色");
darkerButton=new JButton("顏色變暗");
brighterButton=new JButton("顏色變亮");
colorText=new JTextArea(10,30);
// 添加組件
add(new JScrollPane(colorText));
add(colorButton);
add(darkerButton);
add(brighterButton);
setBackground(backgroundColor);
//注冊組件所監聽的事件
colorButton.addActionListener(new ColorActionListener());
brighterButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
setBrighter();
}
});
darkerButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
setDarker();
}
});
}
private void setBrighter(){
backgroundColor=backgroundColor.brighter();
changeColor();
}
private void setDarker(){
backgroundColor=backgroundColor.darker();
changeColor();
}
private class ColorActionListener implements ActionListener{
public void actionPerformed(ActionEvent event){
setColor();//設置面板的顏色
}
}
private void changeColor(){
int r=backgroundColor.getRed();
int g=backgroundColor.getGreen();
int b=backgroundColor.getBlue();
colorText.append("顏色值:\tR="+r+"\t G="+g+"\t B="+b+"\n");
this.setBackground(backgroundColor);
}
private void setColor(){
//產生隨機顏色
int r=(int)(Math.random()*255);
int g=(int)(Math.random()*255);
int b=(int)(Math.random()*255);
colorText.append("顏色值:\tR="+r+"\t G="+g+"\t B="+b+"\n");
backgroundColor=new Color(r,g,b);//非常的重要, 我這句話少寫了,弄了很長時間才弄好
this.setBackground(backgroundColor);
}
private Color backgroundColor;
private JButton colorButton;
private JButton darkerButton;
private JButton brighterButton;
private JTextArea colorText;
}
Tags - 課本習題
文章來源:http://www.tt-shopping.com/kevinlau/read.php/113.htm
即想要接受某個事件的類實現該事件監聽器的接口,然后在事件源中注冊自己,這樣它就可以接收所要的事件,并通過監聽器接口中的方法進行處理。此文中使用了三個監聽器對象,一個是隨機顏色按鈕的監聽器ColorActionListener,還有另外兩個new出來的分別實現了顏色變暗和顏色變亮的監聽器對象。
事件源分別是隨機顏色Button的ActionEvent對象,顏色變暗和顏色變亮的ActionEvent對象。
當事件源對象發生ActionEvent事件的時候,會產生ActionEvent對象,此時編譯器會把這個事件對象傳個注冊在該事件源的接口,由相應接口執行操作。充分體現了用戶為主的操作體驗,根據用戶的動作來做出響應。這是事件驅動機制很好的地方,個人拙見。呵呵
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonFrame extends SimpleFrame{
public ButtonFrame(int width,int height){
super(width,height);
setTitle("這是個按鈕事件?。?);
ColorPanel panel=new ColorPanel();
Container contentPane=getContentPane();//返回此窗體的contentPane 對象
contentPane.add(panel);
}
/*注釋用:getContentPane
public Container getContentPane()
返回此窗體的 contentPane 對象
指定者:
接口 RootPaneContainer 中的 getContentPane
返回:
contentPane 屬性*/
//定義主函數,用來驅動ButtonFrame類
public static void main(String[] args) {
ButtonFrame frame=new ButtonFrame(400,300);
frame.setVisible(true);
}
}
class ColorPanel extends JPanel{
public ColorPanel(){
//創建組件
colorButton =new JButton("隨機顏色");
darkerButton=new JButton("顏色變暗");
brighterButton=new JButton("顏色變亮");
colorText=new JTextArea(10,30);
// 添加組件
add(new JScrollPane(colorText));
add(colorButton);
add(darkerButton);
add(brighterButton);
setBackground(backgroundColor);
//注冊組件所監聽的事件
colorButton.addActionListener(new ColorActionListener());
brighterButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
setBrighter();
}
});
darkerButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
setDarker();
}
});
}
private void setBrighter(){
backgroundColor=backgroundColor.brighter();
changeColor();
}
private void setDarker(){
backgroundColor=backgroundColor.darker();
changeColor();
}
private class ColorActionListener implements ActionListener{
public void actionPerformed(ActionEvent event){
setColor();//設置面板的顏色
}
}
private void changeColor(){
int r=backgroundColor.getRed();
int g=backgroundColor.getGreen();
int b=backgroundColor.getBlue();
colorText.append("顏色值:\tR="+r+"\t G="+g+"\t B="+b+"\n");
this.setBackground(backgroundColor);
}
private void setColor(){
//產生隨機顏色
int r=(int)(Math.random()*255);
int g=(int)(Math.random()*255);
int b=(int)(Math.random()*255);
colorText.append("顏色值:\tR="+r+"\t G="+g+"\t B="+b+"\n");
backgroundColor=new Color(r,g,b);//非常的重要, 我這句話少寫了,弄了很長時間才弄好
this.setBackground(backgroundColor);
}
private Color backgroundColor;
private JButton colorButton;
private JButton darkerButton;
private JButton brighterButton;
private JTextArea colorText;
}
Tags - 課本習題
文章來源:http://www.tt-shopping.com/kevinlau/read.php/113.htm