【Java】【事件處理機制】

來源:互聯網
上載者:User

標籤:--   opera   jframe   方式   nts   cti   panel   str   orm   

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/*
栗子 事件監聽瞭解
*/

class Cat implements ActionListener{
public void actionPerformed(ActionEvent arg0){
if(arg0.getActionCommand().equals("黑色")){
System.out.println("Cat也知道你按下了黑色按鈕");
}
else if(arg0.getActionCommand().equals("紅色")){
System.out.println("Cat也知道你按下了紅色按鈕");
}
else{
System.out.println("Cat什麼也不知道");
}
}
}
public class Test_Action extends JFrame implements ActionListener {
//定義組件
JPanel mp = null;
JButton jb1,jb2;

public Test_Action(){
mp = new JPanel();
jb1 = new JButton("黑色");
jb2 = new JButton("紅色");
//設定布局管理器

//加入組件
mp.setBackground(Color.black);
this.add(mp);
this.add(jb1,BorderLayout.NORTH);
this.add(jb2,BorderLayout.SOUTH);

//貓類在監聽
Cat mycat1 = new Cat();
jb1.addActionListener(mycat1);
jb2.addActionListener(mycat1);
//註冊監聽
jb1.addActionListener(this);
jb2.addActionListener(this);

//指定action命令
jb1.setActionCommand("黑色");
jb2.setActionCommand("紅色");

//表單設定
this.setTitle("事件處理機制");
this.setSize(400,300);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

//對事件處理的方法
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("黑色")){
System.out.println("點擊了黑色按鈕");
}else if(e.getActionCommand().equals("紅色")){
System.out.println("點擊了紅色按鈕");
}else{
System.out.println("不知道");
}
}


public static void main(String[] args){
Test_Action ta = new Test_Action();
}



}








import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
/*
栗子 加深對事件處理機制的理解
1. 通過上下左右鍵,來控制一個小球的移動
*/

//定義自己的面板
class MyPanels extends JPanel implements KeyListener{
int x = 10,y = 10;
public void paint(Graphics g){
super.paint(g);
g.fillOval(x,y,10,10);
}

//按鍵被按下
public void keyPressed(KeyEvent e){
System.out.println("被按下" + ((char)e.getKeyCode()));
if(e.getKeyCode() == KeyEvent.VK_DOWN){
System.out.println("向下");
y++;
}else if(e.getKeyCode() == KeyEvent.VK_UP){
System.out.println("向上");
y--;
}else if(e.getKeyCode() == KeyEvent.VK_LEFT){
System.out.println("向左");
x--;
}else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
System.out.println("向右");
x++;
}

//調用repaint(),重繪介面
this.repaint();

}

//按鍵被彈起
public void keyReleased(KeyEvent e){

}

//keyTyped代表具體的值被輸出
public void keyTyped(KeyEvent e){

}
}
public class Test_Action extends JFrame {
//定義組件
MyPanels mp = null;

public Test_Action(){
mp = new MyPanels();

//監聽
this.addKeyListener(mp);

//加入組件
this.add(mp);
//設定表單
this.setTitle("鍵盤事件監聽");
this.setSize(400,300);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args){
Test_Action ta = new Test_Action();
}

}

按上下左右鍵可相應的上下左右

 

Java事件處理機制--深入理解

幾個重要的概念:1. 事件來源 2. 事件 3. 事件監聽器

1. 事件來源是一個產生(或觸發)事件的對象,比如前面的JButton的一個對象jb1. 當這個事件來源對象的某些狀態以某種方式發生變化時,就會產生某種類型的事件(一個事件來源可能會產生多個不同的事件)。如果某個組件(對象)希望得到事件來源產生的事件,就需要在這個事件來源上註冊。

2.事件就是承載事件來源狀態改變時的資訊對象。Java.awt.event 包和 javax.swing.event包中定義了各種事件類型,常見的事件類別有:

   ActionEvent        按下按鈕,或雙擊一個清單項目或選中某個菜單時

  AdjustmentEvent 當操作一個捲軸

 ComponentEvent  當一個組件隱藏、移動、改變大小時

 ContainerEvent    當一個組件從容器中加入或者刪除時

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 



















【Java】【事件處理機制】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.