java swing事件_第四节 Java Swing事件处理
第四節(jié):事件處理
一個(gè)圖形界面完的成只是程序開(kāi)發(fā)中起步的工作,因?yàn)橐胱屆恳粋€(gè)組件都發(fā)揮其作用,就必須對(duì)所有的組件進(jìn)行事件處理。那么什么是事件處理,所謂事件就表示一個(gè)對(duì)象發(fā)生狀態(tài)變化。例如,每當(dāng)按下一個(gè)按鈕時(shí),實(shí)際上按鈕的狀態(tài)就發(fā)生了變化,而如果要想處理此事件,就需要監(jiān)聽(tīng)者不斷地進(jìn)行監(jiān)聽(tīng)事件的變化,并根據(jù)時(shí)間進(jìn)行相應(yīng)的處理。事件要想被處理,必須使用事件監(jiān)聽(tīng)器,所有的事件監(jiān)聽(tīng)器都是以接口的形式出現(xiàn)的,處理時(shí)只要實(shí)現(xiàn)此接口就行。整個(gè)事件處理流程如下圖所示:
下面通過(guò)幾個(gè)事件來(lái)進(jìn)一步說(shuō)明事件的處理流程。
窗體事件
WindowsListener是專(zhuān)門(mén)處理窗體的事件監(jiān)聽(tīng)接口,一個(gè)窗體的所有變化,如窗體的打開(kāi),關(guān)閉等都可以使用這個(gè)接口進(jìn)行監(jiān)聽(tīng)。此接口定義的方法如下:
public?void?windowOpened(WindowEvent?e)//窗口打開(kāi)時(shí)觸發(fā)
public?void?windowClosing(WindowEvent?e)//窗口正在關(guān)閉時(shí)觸發(fā)
public?void?windowIconified(WindowEvent?e)//窗口最小化時(shí)觸發(fā)
public?void?windowDeiconified(WindowEvent?e)//窗口從最小化恢復(fù)到正常狀態(tài)時(shí)觸發(fā)
public?void?windowActivated(WindowEvent?e)//將窗口變?yōu)榛顒?dòng)窗口時(shí)觸發(fā)
public?void?windowDeactivated(WindowEvent?e)//將窗口變?yōu)椴换顒?dòng)窗口時(shí)觸發(fā)
建立一個(gè)監(jiān)聽(tīng)器
import?java.awt.event.WindowEvent;
import?java.awt.event.WindowListener;
public?class?MyWindowEventHandle?implements?WindowListener{
@Override
public?void?windowOpened(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
System.out.println("windowOpened-->窗口被打開(kāi)");
}
@Override
public?void?windowClosing(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
System.out.println("windowClosing-->窗口關(guān)閉");
}
@Override
public?void?windowClosed(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
System.out.println("windowClosed-->窗口被關(guān)閉");
System.exit(1);//系統(tǒng)退出
}
@Override
public?void?windowIconified(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
System.out.println("windowIconifed-->窗口最小化");
}
@Override
public?void?windowDeiconified(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
System.out.println("windowDeiconified-->窗口最小化恢復(fù)");
}
@Override
public?void?windowActivated(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
System.out.println("windowActivated-->窗口被選中");
}
@Override
public?void?windowDeactivated(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
System.out.println("windowDeactivated-->窗口被選中");
}
}
單單只有一個(gè)監(jiān)聽(tīng)器是不夠的,還需要在組件上注冊(cè)監(jiān)聽(tīng),這樣才可以處理,直接使用窗體的addWindowListener(監(jiān)聽(tīng)對(duì)象)方法即可注冊(cè)。
import?java.awt.Color;
import?javax.swing.JFrame;
public?class?MyWindowEventJFrame01?{
public?static?void?main(String?args[]){
JFrame?jFrame=new?JFrame("Welcome?to?MichaelLee!");
//將此窗口加入到一個(gè)窗口監(jiān)聽(tīng)器中,這樣監(jiān)聽(tīng)器就可以根據(jù)時(shí)間進(jìn)行處理
jFrame.addWindowListener(new?MyWindowEventHandle());
jFrame.setSize(400,300);
jFrame.setBackground(Color.black);
jFrame.setLocation(500,300);
jFrame.setVisible(true);
}
}
程序運(yùn)行結(jié)果:
windowActivated-->窗口被選中
windowOpened-->窗口被打開(kāi)
windowIconifed-->窗口最小化
windowDeactivated-->窗口被選中
windowDeiconified-->窗口最小化恢復(fù)
windowActivated-->窗口被選中
windowClosing-->窗口關(guān)閉
windowDeactivated-->窗口被選中
程序運(yùn)行后會(huì)顯示一個(gè)窗體,此時(shí)對(duì)窗體進(jìn)行相應(yīng)的狀態(tài)變化,則在后臺(tái)會(huì)打印出以上的信息。
監(jiān)聽(tīng)適配器:
大致了解事件處理的基本流程后,大家可能會(huì)有這樣一個(gè)疑問(wèn),如果現(xiàn)在只需對(duì)關(guān)閉窗口的事件進(jìn)行監(jiān)聽(tīng),其他的操作根本就不關(guān)心,那末還有必要覆寫(xiě)那么多的方法嗎?能不能只根據(jù)個(gè)人的需要來(lái)進(jìn)行覆寫(xiě)?答案是肯定的。要想解決這個(gè)問(wèn)題,可以使用Adapter(適配器)類(lèi)。以WindowAdapter為例,用戶只要繼承了此類(lèi),就可以根據(jù)自己的需要覆寫(xiě)方法。比如現(xiàn)在我們只關(guān)心關(guān)閉窗口和打開(kāi)窗口。
通過(guò)WindowAdapter實(shí)現(xiàn)監(jiān)聽(tīng)
import?java.awt.event.WindowAdapter;
import?java.awt.event.WindowEvent;
public?class?MyWindowAdapterHandler?extends?WindowAdapter{
@Override
public?void?windowOpened(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
super.windowOpened(e);
System.out.println("窗口被打開(kāi)!");
}
@Override
public?void?windowClosing(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
super.windowClosed(e);
System.out.println("窗口關(guān)閉!");
}
}
注冊(cè)事件監(jiān)聽(tīng)
import?java.awt.Color;
import?javax.swing.JFrame;
public?class?MyWindowEventJFrame02?{
public?static?void?main(String?args[]){
JFrame?jFrame=new?JFrame("Welcome?to?MichaelLee!");
jFrame.addWindowListener(new?MyWindowAdapterHandler());
jFrame.setSize(500,400);
jFrame.setLocation(300,400);
jFrame.setBackground(Color.BLUE);
jFrame.setVisible(true);
}
}
程序運(yùn)行結(jié)果:
窗口被打開(kāi)!
窗口關(guān)閉!
此時(shí)只監(jiān)聽(tīng)窗口被打開(kāi)以及被關(guān)閉事件,但是這樣一來(lái)又會(huì)出現(xiàn)一個(gè)新的問(wèn)題,如果此監(jiān)聽(tīng)處理只需操作一次,那末就沒(méi)必要將其設(shè)置成一個(gè)單獨(dú)的類(lèi),此時(shí)就可以利用匿名內(nèi)部類(lèi)來(lái)完成。
使用匿名內(nèi)部類(lèi):
import?java.awt.Color;
import?java.awt.event.WindowAdapter;
import?java.awt.event.WindowEvent;
import?java.awt.event.WindowListener;
import?javax.swing.JFrame;
public?class?MyWindowEventJframe03?{
public?static?void?main(String?args[]){
JFrame?jFrame=new?JFrame("Welcome?to?MichaelLee!");
//此時(shí)直接使用WindowAdapter的子類(lèi)完成監(jiān)聽(tīng)的處理
jFrame.addWindowListener(new?WindowAdapter()?{
//覆寫(xiě)窗口的關(guān)閉方法
@Override
public?void?windowOpened(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
super.windowOpened(e);
System.out.println("窗口被打開(kāi)!");
}
@Override
public?void?windowClosing(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
super.windowClosed(e);
System.out.println("窗口關(guān)閉!");
}
});
jFrame.setSize(500,400);
jFrame.setLocation(300,400);
jFrame.setBackground(Color.BLUE);
jFrame.setVisible(true);
}
}
效果與上面的一樣,可以看出直接編寫(xiě)匿名內(nèi)部類(lèi)可以減少監(jiān)聽(tīng)類(lèi)的定義,這在開(kāi)發(fā)中是較為常見(jiàn)的一種做法。
動(dòng)作事件及監(jiān)聽(tīng)處理:
要想讓一個(gè)按鈕變得有意義,就必須使用事件處理。在Swing事件處理中可以使用ActionListener接口處理按鈕的動(dòng)作事件,ActionListener接口只定義了一個(gè)方法
Void?actiongPerformed(ActionEvent?e)//發(fā)生操作時(shí)調(diào)用
是用以上接口監(jiān)聽(tīng)按鈕的單擊事件
import?java.awt.Color;
import?java.awt.Font;
import?java.awt.Frame;
import?java.awt.GridLayout;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?java.awt.event.WindowAdapter;
import?java.awt.event.WindowEvent;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JLabel;
import?javax.swing.JPanel;
import?javax.swing.JTextField;
class?ActionHandler{
private?JFrame?jframe=new?JFrame("Welcome?to?MichaelLee!");
private?JButton?btn=new?JButton("顯示");
private?JLabel??lab=new?JLabel();
private?JTextField?text=new?JTextField(10);//定義一個(gè)文本域
private?JPanel?pan=new?JPanel();//定義一個(gè)版面
public?ActionHandler(){
Font?font=new?Font("Serif",Font.ITALIC+Font.BOLD,28);
lab.setFont(font);
lab.setText("等待用戶輸入!");
btn.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
//?TODO?Auto-generated?method?stub
if(e.getSource()==btn){//判斷觸發(fā)源是否是按鈕
lab.setText(text.getText());//將文本文字設(shè)置到標(biāo)簽
}
}
});
//此時(shí)直接使用WindowAdapter的子類(lèi)完成監(jiān)聽(tīng)的處理
jframe.addWindowListener(new?WindowAdapter()?{
//覆寫(xiě)窗口的關(guān)閉方法
@Override
public?void?windowOpened(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
super.windowOpened(e);
System.out.println("窗口被打開(kāi)!");
}
@Override
public?void?windowClosing(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
super.windowClosed(e);
System.out.println("窗口關(guān)閉!");
}
});
jframe.setLayout(new?GridLayout(2,1));
pan.setLayout(new?GridLayout(1,2));
pan.add(text);
pan.add(btn);
jframe.add(pan);
jframe.add(lab);
jframe.add(lab);
jframe.pack();//根據(jù)組件自動(dòng)調(diào)整窗口
jframe.setLocation(500,400);
jframe.setBackground(Color.BLUE);
jframe.setVisible(true);
}
}
public?class?MyActionEventDemon01?{
public?static?void?main(String?args[]){
new?ActionHandler();
}
}
運(yùn)行程序首先會(huì)出現(xiàn)這樣一個(gè)界面
在文本框中輸入內(nèi)容后,單擊顯示,界面會(huì)變成這樣
此時(shí)只要單擊按鈕就會(huì)觸發(fā)監(jiān)聽(tīng)器
了解了動(dòng)作事件之后,實(shí)際上就可以使用此事件完成一個(gè)簡(jiǎn)單的用戶登錄操作。例如在程序中輸入的用戶名為MichaelLee密碼為2014/7/9
則認(rèn)為是合法用戶,提示登錄成功的信息。反之,則提示登陸失敗的信息。代碼如下:
先建一個(gè)類(lèi)
class?logincheck?{
private?String?name;
private?String?password;
public?logincheck(String?name,String?password){
this.name=name;
this.password=password;
}
public?boolean?validate(){
if("MichaelLee".equals(name)&&"2014/7/9".equals(password)){
return?true;
}else{
return?false;
}
}
}
登錄操作實(shí)現(xiàn):
import?java.awt.Font;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?java.awt.event.WindowAdapter;
import?java.awt.event.WindowEvent;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JLabel;
import?javax.swing.JPasswordField;
import?javax.swing.JTextField;
class?ActionHandle{//監(jiān)聽(tīng)事件一旦形成,就一直處于監(jiān)聽(tīng)狀態(tài),除非窗口關(guān)閉
private?JFrame?jFrame=new?JFrame("Welcome?to?MichaelLee!");
private?JButton?submit=new?JButton("登錄");
private?JButton?reset=new?JButton("重置");
private?JLabel?nameLabel=new?JLabel("用戶名");
private?JLabel?passJLabel=new?JLabel("密碼");
private?JLabel?infoLabel=new?JLabel("用戶登錄系統(tǒng)");
private?JTextField?nameField=new?JTextField();
private?JPasswordField?passField=new?JPasswordField();
public?ActionHandle(){
Font?font=new?Font("Serif",Font.BOLD,12);
infoLabel.setFont(font);
submit.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
//?TODO?Auto-generated?method?stub
if(e.getSource()==submit){//判斷觸發(fā)源是否是提交按鈕
//System.out.println("NO!");
String?tname=nameField.getText();
String?tpass=passField.getText();
logincheck?log=new?logincheck(tname,tpass);
if(log.validate()){//對(duì)用戶名和密碼進(jìn)行驗(yàn)證
infoLabel.setText("登陸成功,歡迎光臨!");
}else{
infoLabel.setText("登錄失敗,錯(cuò)誤的用戶名或密碼");
}
}
}});
reset.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
//?TODO?Auto-generated?method?stub
if(e.getSource()==reset){//判斷觸發(fā)源是否是重置按鈕
nameField.setText("");//清空文本框內(nèi)容
passField.setText("");//清空密碼框內(nèi)容
infoLabel.setText("用戶登錄");
}
}
});
jFrame.addWindowListener(new?WindowAdapter()?{
@Override
public?void?windowOpened(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
super.windowOpened(e);
System.out.println("窗口打開(kāi)");
}
@Override
public?void?windowClosing(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
super.windowClosing(e);
System.out.println("窗口關(guān)閉");
}
@Override
public?void?windowIconified(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
super.windowIconified(e);
System.out.println("窗口最小化");
}
@Override
public?void?windowDeiconified(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
super.windowDeiconified(e);
System.out.println("取消窗口最小化");
}
@Override
public?void?windowActivated(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
super.windowActivated(e);
System.out.println("選中窗口");
}
@Override
public?void?windowDeactivated(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
super.windowDeactivated(e);
System.out.println("取消窗口選中");
}
});
jFrame.setLayout(null);
nameLabel.setBounds(5,5,60,20);
passJLabel.setBounds(5,30,60,?20);
infoLabel.setBounds(5,65,220,30);
nameField.setBounds(65,5,100,20);
passField.setBounds(65,30,100,20);
submit.setBounds(165,5,60,20);
reset.setBounds(165,30,60,20);
jFrame.add(infoLabel);
jFrame.add(nameField);
jFrame.add(nameLabel);
jFrame.add(passField);
jFrame.add(passJLabel);
jFrame.add(reset);
jFrame.add(submit);
jFrame.setSize(380,130);
jFrame.setVisible(true);
}
}
public?class?MyActionExentDemon03?{
public?static?void?main(String?args[]){
new?ActionHandle();
}
}
程序運(yùn)行結(jié)果
輸入正確的用戶名和密碼則會(huì)出現(xiàn)提示登陸成功,否則提示登錄失敗,單擊重置按鈕,內(nèi)容被清空。
鍵盤(pán)事件及監(jiān)聽(tīng)處理
在Swing中可以直接使用KeyListener接口對(duì)鍵盤(pán)的操作進(jìn)行監(jiān)聽(tīng)
KeyListener接口的方法
public?void?keyTyped(KeyEvent?e)//輸入某個(gè)鍵時(shí)調(diào)用
public?void?keyPressed(KeyEvent?e)//鍵盤(pán)按下時(shí)調(diào)用
public?void?keyReleased(KeyEvent?e)//鍵盤(pán)松開(kāi)時(shí)調(diào)用
public?char?getKeyChar()//返回輸入的字符,只針對(duì)與keyTyped有意義
public?int?getKeyCode()//返回輸入字符的鍵碼
public?static?String?getKeyText(int?keyCode)//返回此鍵的信息,如‘HOME’‘F1’或‘A’等
實(shí)現(xiàn)鍵盤(pán)監(jiān)聽(tīng)
import?java.awt.event.KeyEvent;
import?java.awt.event.KeyListener;
import?java.awt.event.WindowAdapter;
import?java.awt.event.WindowEvent;
import?javax.swing.JFrame;
import?javax.swing.JScrollPane;
import?javax.swing.JTextArea;
class?MykeyHandle?extends?JFrame?implements?KeyListener{
public?JTextArea?textArea=new?JTextArea();
public?MykeyHandle(){
super.setTitle("Welcome?to?MichaelLee!");
JScrollPane?src=new?JScrollPane(textArea);
src.setBounds(5,?5,?300,?200);
super.add(src);
textArea.addKeyListener(this);
super.setSize(310,?210);
super.setVisible(true);
super.addWindowListener(new?WindowAdapter()?{
@Override
public?void?windowOpened(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
super.windowOpened(e);
System.out.println("窗口打開(kāi)");
}
@Override
public?void?windowClosing(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
super.windowClosing(e);
System.out.println("窗口關(guān)閉");
}
});
}
@Override
public?void?keyTyped(KeyEvent?e)?{
//?TODO?Auto-generated?method?stub
System.out.println("輸入的內(nèi)容是:"+e.getKeyChar()+"\n");//輸出到后臺(tái)
textArea.append("輸入的內(nèi)容是:"+e.getKeyChar()+"\n");//輸入到多行文本框中
}
@Override
public?void?keyPressed(KeyEvent?e)?{
//?TODO?Auto-generated?method?stub
System.out.println("鍵盤(pán)"+KeyEvent.getKeyText(e.getKeyCode())+"鍵按下\n");//輸出到后臺(tái)
textArea.append("鍵盤(pán)"+KeyEvent.getKeyText(e.getKeyCode())+"鍵按下\n");//輸入到多行文本框中
}
@Override
public?void?keyReleased(KeyEvent?e)?{
//?TODO?Auto-generated?method?stub
System.out.println("鍵盤(pán)"+KeyEvent.getKeyText(e.getKeyCode())+"鍵松開(kāi)\n");//輸出到后臺(tái)
textArea.append("鍵盤(pán)"+KeyEvent.getKeyText(e.getKeyCode())+"鍵松開(kāi)\n");//輸入到多行文本框中
}
}
public?class?MyKeyEventDemon01?{
public?static?void?main(String?args[]){
new?MykeyHandle().textArea.append("hahhahahhahah!");
}
}
運(yùn)行結(jié)果
This表示當(dāng)前對(duì)象,以上程序中,MyKeyHandle實(shí)現(xiàn)了KeyListener監(jiān)聽(tīng)接口,所以此類(lèi)也是監(jiān)聽(tīng)操作類(lèi),這樣當(dāng)JTextArea增加事件時(shí)直接使用This關(guān)鍵字,如下所示:
text.addKeyListener(this);
This表示當(dāng)前對(duì)象,此時(shí)將this加入到監(jiān)聽(tīng)器中,就表示將一個(gè)監(jiān)聽(tīng)處理類(lèi)加入到監(jiān)聽(tīng)器中。
在鍵盤(pán)監(jiān)聽(tīng)中也可以使用KeyAdapter適配器完成鍵盤(pán)事件的監(jiān)聽(tīng)
使用KeyAdapter
import?java.awt.event.KeyAdapter;
import?java.awt.event.KeyEvent;
import?java.awt.event.WindowAdapter;
import?java.awt.event.WindowEvent;
import?javax.swing.JFrame;
import?javax.swing.JScrollPane;
import?javax.swing.JTextArea;
class?Mykeyhandle2?extends?JFrame{
//此類(lèi)直接繼承了JFrame,以下的super可以理解為JFrame
private?JTextArea?textArea=new?JTextArea();
public?Mykeyhandle2(){
super.setTitle("Welcome?to?MichaelLee!");
JScrollPane?src=new?JScrollPane(textArea);
src.setBounds(5,?5,?300,?200);
super.add(src);
textArea.addKeyListener(new?KeyAdapter()?{//直接使用KeyAdapter完成監(jiān)聽(tīng),可以選擇需要的方法進(jìn)行覆寫(xiě)
@Override
public?void?keyTyped(KeyEvent?e)?{
//?TODO?Auto-generated?method?stub
super.keyTyped(e);
System.out.println("輸入的內(nèi)容是:"+e.getKeyChar()+"\n");
textArea.append("輸入的內(nèi)容是:"+e.getKeyChar()+"\n");
}
});
super.setSize(310,?210);
super.setVisible(true);
super.addWindowListener(new?WindowAdapter()?{
@Override
public?void?windowOpened(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
super.windowOpened(e);
System.out.println("窗口打開(kāi)");
}
@Override
public?void?windowClosing(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
super.windowClosing(e);
System.out.println("窗口關(guān)閉");
}
});
}
}
public?class?MyKeyEventDemon02?{
public?static?void?main(String?args[]){
new?Mykeyhandle2();
}
}
運(yùn)行效果
對(duì)鼠標(biāo)事件進(jìn)行監(jiān)聽(tīng)可以使用MouseListener接口,常用方法如下:
public?void?mouseClicked(MouseEvent?e)//鼠標(biāo)單擊時(shí)調(diào)用
public?void?mousePressed(MouseEvent?e)//按下時(shí)調(diào)用
public?void?mouseReleased(MouseEvent?e)//松開(kāi)時(shí)調(diào)用
public?void?mouseEntered(MouseEvent?e)//鼠標(biāo)進(jìn)入到組件時(shí)調(diào)用
public?void?mouseExited(MouseEvent?e)//鼠標(biāo)離開(kāi)組件時(shí)調(diào)用
Public?static?final?int?BUTTON1//表示鼠標(biāo)左鍵的常量
Public?static?final?int?BUTTON2//表示鼠標(biāo)滾軸的常量
Public?static?final?int?BUTTON3//表示鼠標(biāo)右鍵的常量
Public?int?getButton()//以數(shù)字形式返回按下的鼠標(biāo)鍵
Public?int?getClickCount()//返回鼠標(biāo)的單擊次數(shù)
Public?int?getX()//返回鼠標(biāo)操作的X坐標(biāo)
Public?int?getY()//返回鼠標(biāo)操作的Y坐標(biāo)
實(shí)現(xiàn)鼠標(biāo)監(jiān)聽(tīng)
import?java.awt.event.MouseEvent;
import?java.awt.event.MouseListener;
import?java.awt.event.WindowAdapter;
import?java.awt.event.WindowEvent;
import?javax.swing.JFrame;
import?javax.swing.JScrollPane;
import?javax.swing.JTextArea;
class?MyMousehandle?extends?JFrame?implements?MouseListener{
private?JTextArea?textArea=new?JTextArea();
public?MyMousehandle(){
super.setTitle("Welcome?to?MichaelLee!");
JScrollPane?src=new?JScrollPane(textArea);
src.setBounds(5,?5,?300,?200);
super.add(src);
textArea.addMouseListener(this);
super.setSize(310,210);
super.setVisible(true);
super.addWindowListener(new?WindowAdapter()?{
@Override
public?void?windowClosing(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
super.windowClosing(e);
System.exit(1);
}
});
}
@Override
public?void?mouseClicked(MouseEvent?e)?{
//?TODO?Auto-generated?method?stub
int?c=e.getButton();
String?mouseInfo=null;
if(c==MouseEvent.BUTTON1){
mouseInfo="左鍵";
}else?if(c==MouseEvent.BUTTON3){
mouseInfo="右鍵";
}else?if(c==MouseEvent.BUTTON2){
mouseInfo="滾軸";
}
textArea.append("鼠標(biāo)單擊:"+mouseInfo+"\n");
}
@Override
public?void?mousePressed(MouseEvent?e)?{
//?TODO?Auto-generated?method?stub
textArea.append("鼠標(biāo)按下。\n");
}
@Override
public?void?mouseReleased(MouseEvent?e)?{
//?TODO?Auto-generated?method?stub
textArea.append("鼠標(biāo)松開(kāi)。\n");
}
@Override
public?void?mouseEntered(MouseEvent?e)?{
//?TODO?Auto-generated?method?stub
textArea.append("鼠標(biāo)進(jìn)入組件。\n");
}
@Override
public?void?mouseExited(MouseEvent?e)?{
//?TODO?Auto-generated?method?stub
textArea.append("鼠標(biāo)離開(kāi)組件。\n");
}
}
public?class?MyMouseEventdemon01?{
public?static?void?main(String?args[]){
new?MyMousehandle();
}
}
對(duì)鼠標(biāo)事件進(jìn)行監(jiān)聽(tīng)可以使用MouseListener接口,常用方法如下:
public?void?mouseClicked(MouseEvent?e)//鼠標(biāo)單擊時(shí)調(diào)用
public?void?mousePressed(MouseEvent?e)//按下時(shí)調(diào)用
public?void?mouseReleased(MouseEvent?e)//松開(kāi)時(shí)調(diào)用
public?void?mouseEntered(MouseEvent?e)//鼠標(biāo)進(jìn)入到組件時(shí)調(diào)用
public?void?mouseExited(MouseEvent?e)//鼠標(biāo)離開(kāi)組件時(shí)調(diào)用
Public?static?final?int?BUTTON1//表示鼠標(biāo)左鍵的常量
Public?static?final?int?BUTTON2//表示鼠標(biāo)滾軸的常量
Public?static?final?int?BUTTON3//表示鼠標(biāo)右鍵的常量
Public?int?getButton()//以數(shù)字形式返回按下的鼠標(biāo)鍵
Public?int?getClickCount()//返回鼠標(biāo)的單擊次數(shù)
Public?int?getX()//返回鼠標(biāo)操作的X坐標(biāo)
Public?int?getY()//返回鼠標(biāo)操作的Y坐標(biāo)
實(shí)現(xiàn)鼠標(biāo)監(jiān)聽(tīng)
import?java.awt.event.MouseEvent;
import?java.awt.event.MouseListener;
import?java.awt.event.WindowAdapter;
import?java.awt.event.WindowEvent;
import?javax.swing.JFrame;
import?javax.swing.JScrollPane;
import?javax.swing.JTextArea;
class?MyMousehandle?extends?JFrame?implements?MouseListener{
private?JTextArea?textArea=new?JTextArea();
public?MyMousehandle(){
super.setTitle("Welcome?to?MichaelLee!");
JScrollPane?src=new?JScrollPane(textArea);
src.setBounds(5,?5,?300,?200);
super.add(src);
textArea.addMouseListener(this);
super.setSize(310,210);
super.setVisible(true);
super.addWindowListener(new?WindowAdapter()?{
@Override
public?void?windowClosing(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
super.windowClosing(e);
System.exit(1);
}
});
}
@Override
public?void?mouseClicked(MouseEvent?e)?{
//?TODO?Auto-generated?method?stub
int?c=e.getButton();
String?mouseInfo=null;
if(c==MouseEvent.BUTTON1){
mouseInfo="左鍵";
}else?if(c==MouseEvent.BUTTON3){
mouseInfo="右鍵";
}else?if(c==MouseEvent.BUTTON2){
mouseInfo="滾軸";
}
textArea.append("鼠標(biāo)單擊:"+mouseInfo+"\n");
}
@Override
public?void?mousePressed(MouseEvent?e)?{
//?TODO?Auto-generated?method?stub
textArea.append("鼠標(biāo)按下。\n");
}
@Override
public?void?mouseReleased(MouseEvent?e)?{
//?TODO?Auto-generated?method?stub
textArea.append("鼠標(biāo)松開(kāi)。\n");
}
@Override
public?void?mouseEntered(MouseEvent?e)?{
//?TODO?Auto-generated?method?stub
textArea.append("鼠標(biāo)進(jìn)入組件。\n");
}
@Override
public?void?mouseExited(MouseEvent?e)?{
//?TODO?Auto-generated?method?stub
textArea.append("鼠標(biāo)離開(kāi)組件。\n");
}
}
public?class?MyMouseEventdemon01?{
public?static?void?main(String?args[]){
new?MyMousehandle();
}
}
也可以通過(guò)MouseAdapter實(shí)現(xiàn)指定鼠標(biāo)操作的監(jiān)聽(tīng)
import?java.awt.event.MouseAdapter;
import?java.awt.event.MouseEvent;
import?java.awt.event.WindowAdapter;
import?java.awt.event.WindowEvent;
import?javax.swing.JFrame;
import?javax.swing.JScrollPane;
import?javax.swing.JTextArea;
class?MyMousehandle3?extends?JFrame{
private?JTextArea?textArea=new?JTextArea();
public?MyMousehandle3(){
super.setTitle("Welcome?to?MichaelLee!");
JScrollPane?scr=new?JScrollPane(textArea);
scr.setBounds(5,?5,?300,?200);
super.add(scr);
textArea.addMouseListener(new?MouseAdapter()?{
@Override
public?void?mouseClicked(MouseEvent?e)?{//z只覆寫(xiě)mouseClicked方法
//?TODO?Auto-generated?method?stub
super.mouseClicked(e);
int?c=e.getButton();
String?mouseInfo=null;
if(c==MouseEvent.BUTTON1){
mouseInfo="左鍵";
}else?if(c==MouseEvent.BUTTON3){
mouseInfo="右鍵";
}else{
mouseInfo="滾軸";
}
textArea.append("鼠標(biāo)單擊:"+mouseInfo+"\n");
}
});
super.setSize(310,210);
super.setVisible(true);
super.addWindowListener(new?WindowAdapter()?{
@Override
public?void?windowClosing(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
super.windowClosing(e);
System.exit(1);
}
});
}
}
public?class?MyMouseEventDemon02?{
public?static?void?main(String?args[]){
new?MyMousehandle();
}
}
鼠標(biāo)拖曳事件及監(jiān)聽(tīng)處理
在一般的圖形界面經(jīng)常可以看到鼠標(biāo)拖曳操作的處理,在Swing事件處理方法中可以使用MouseMotionListener接口完成鼠標(biāo)的拖曳操作。此接口常用方法:
Void?mouseDragged(MouseEvent?e)//在組件上按下并拖動(dòng)時(shí)調(diào)用
Void?MouseMoved(MouseEvent?e)//鼠標(biāo)移動(dòng)到組件時(shí)調(diào)用
觀察鼠標(biāo)拖曳操作
鼠標(biāo)拖曳事件及監(jiān)聽(tīng)處理
在一般的圖形界面經(jīng)常可以看到鼠標(biāo)拖曳操作的處理,在Swing事件處理方法中可以使用MouseMotionListener接口完成鼠標(biāo)的拖曳操作。此接口常用方法:
Void?mouseDragged(MouseEvent?e)//在組件上按下并拖動(dòng)時(shí)調(diào)用
Void?MouseMoved(MouseEvent?e)//鼠標(biāo)移動(dòng)到組件時(shí)調(diào)用
觀察鼠標(biāo)拖曳操作
import?java.awt.event.MouseEvent;
import?java.awt.event.MouseMotionListener;
import?java.awt.event.WindowAdapter;
import?java.awt.event.WindowEvent;
import?javax.swing.JButton;
import?javax.swing.JFrame;
class?MyMouseMotionhandle?extends?JFrame{
public?MyMouseMotionhandle(){
super.setTitle("Welcome?to?MichaelLee!");
super.addMouseMotionListener(new?MouseMotionListener()?{
@Override
public?void?mouseMoved(MouseEvent?e)?{
//?TODO?Auto-generated?method?stub
System.out.println("鼠標(biāo)移動(dòng)到窗體");
}
@Override
public?void?mouseDragged(MouseEvent?e)?{
//?TODO?Auto-generated?method?stub
System.out.println("鼠標(biāo)拖也到:X="+e.getX()+",Y="+e.getY());
}
});
/*JButton?ibt=new?JButton("按鈕");
super.add(ibt);*/
super.setSize(310,210);
super.setVisible(true);
super.addWindowListener(new?WindowAdapter()?{
@Override
public?void?windowClosing(WindowEvent?e)?{
//?TODO?Auto-generated?method?stub
super.windowClosing(e);
System.exit(1);
}
});
}
}
public?class?MyMouseMotionEventDemon01?{
public?static?void?main(String?args[]){
new?MyMouseMotionhandle();
}
}
程序運(yùn)行后發(fā)現(xiàn),只要鼠標(biāo)一向窗體移動(dòng)就會(huì)觸發(fā)mouseMoved()事件,只要是在窗體上拖曳,就會(huì)觸發(fā)mouseDragged事件。
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的java swing事件_第四节 Java Swing事件处理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Python map/reduce
- 下一篇: java美元兑换,(Java实现) 美元