在java語言中,事件監聽器有ActionListener(動作監聽器)、MouseListener(鼠標監聽器)、MousemotionListener(鼠標動作監聽器)、KeyListener(鍵盤監聽器)等等。在源代碼中,這些監聽器都是接口,因此,要實現這些事件的監聽,就必須再新建一個類來繼承要使用的接口,新建的類中又必須重寫該接口的所有方法,於是事件監聽就容易完成了。
1、ActionListener(動作監聽器):在實現此接口的類中,可以給需要關注其動作的組件(如JButton類型的)添加該監聽器,之後在事件處理方法(public void actionPerformed(ActionEvent event){})中,對每個事件進行不同處理。比如在一個登陸界面中,你需要添加一個登陸按鈕,而要使點擊該按鈕後有反應,就需要在該扭上添加該監聽器,再在事件處理方法中添加方法體就可以實現一定功能。
public class MyUI1 extends JFrame{
private JTextField jtf1;
private JPasswordField jtf2;
public static void main(String[] args) {
new MyUI1().init();
}
public void init(){
this.setTitle("MyQQ");
this.setSize(430,350);
this.getContentPane().setBackground(new Color(235,242,249));
this.setUndecorated(true);//去邊框
this.setResizable(false);
EastPanel();
NorthPanel();
WestPanel();
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void NorthPanel(){
JPanel north=new JPanel();
north.setPreferredSize(new Dimension(0,185));
north.setLayout(new FlowLayout(FlowLayout.RIGHT,0,0));
//創建一個圖標對象
ImageIcon image=new ImageIcon("image/catch.jpg");
LoginListener mouse=new LoginListener(this);
north.addMouseListener(mouse);
//將圖片添加至組件上
JLabel jla=new JLabel(image);
north.add(jla);
ImageIcon image1=new ImageIcon("image/qq11.jpg");
//JButton jbu=new JButton(image1);
//north.add(jbu);
//jbu.setPreferredSize(new Dimension(30,30));
//jbu.setBackground(Color.black);
this.add(north,BorderLayout.NORTH);
}
public void WestPanel(){
JPanel west=new JPanel();
west.setPreferredSize(new Dimension(131,0));
west.setBackground(new Color(235,242,249));
west.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
//創建一個圖標對象
ImageIcon image=new ImageIcon("image/catch2.jpg");
//將圖片添加至組件上
JLabel jla=new JLabel(image);
west.add(jla);
this.add(west,BorderLayout.WEST);
}
public void EastPanel(){
JPanel east=new JPanel();
east.setBackground(new Color(235,242,249));
east.setPreferredSize(new Dimension(300,0));
east.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
jtf1=new JTextField(17);
jtf1.setPreferredSize(new Dimension(193,28));
jtf2=new JPasswordField(17);
jtf2.setPreferredSize(new Dimension(193,28));
//jtf1.setFont(new Font("楷體",4,18));
east.add(jtf1);
ImageIcon image=new ImageIcon("image/qq4.jpg");
JLabel jl1=new JLabel(image);
east.add(jl1);
east.add(jtf2);
ImageIcon image2=new ImageIcon("image/qq3.jpg");
JLabel jl2=new JLabel(image2);
east.add(jl2);
ImageIcon image1=new ImageIcon("image/qq1.jpg");
JLabel jl3=new JLabel(image1);
east.add(jl3);
ImageIcon image3=new ImageIcon("image/qq2.jpg");
JButton jbu=new JButton(image3);
jbu.setPreferredSize(new Dimension(192,30));
east.add(jbu);
MyUI2 listener=new MyUI2(jtf1, jtf2);
jbu.addActionListener(listener);
ImageIcon image4=new ImageIcon("image/qq5.jpg");
JLabel jl4=new JLabel(image4);
east.add(jl4);
this.add(east,BorderLayout.EAST);
}
}
該代碼實現的功能就是一個登陸界面,只是按鈕上加的是圖片,登陸按鈕上加的就是該監聽器,點擊按鈕後就會執行事件監聽方法。
public class MyUI2 extends JFrame implements ActionListener{
private JTextField jtf1;
private JPasswordField jtf2;
private Mouse listener2;
ButtonListener listener1;
//private Mouse listener2;
public MyUI2(JTextField jtf1,JPasswordField jtf2){
this.jtf1=jtf1;
this.jtf2=jtf2;
}
public void actionPerformed(ActionEvent e) {
if(jtf1.getText().equals("")&&jtf2.getText().equals("")){
this.setTitle("畫圖板");
this.setSize(new Dimension(800,700));
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
this.setResizable(false);
listener1=new ButtonListener(this);
this.WestPanel();
this.NorthPanel();
this.EastPanel();
this.setVisible(true);
Graphics g=this.getGraphics();
listener2=new Mouse(g,listener1);
listener1.setLis(listener2);
this.addMouseListener(listener2);
this.addMouseMotionListener(listener2);
}else
JOptionPane.showConfirmDialog(null, "您輸入的密碼有誤!");
}
public void NorthPanel(){
JPanel north=new JPanel();
north.setPreferredSize(new Dimension(0,80));
//north.setBackground(Color.black);
north.setBackground(new Color(200,20,180));
//north.setOpaque(false);
JLabel jbl=new JLabel("畫 圖 板");
north.add(jbl);
jbl.setFont(new Font("隸書",4,60));
this.add(north,BorderLayout.NORTH);
}
public void EastPanel(){
JPanel east=new JPanel();
east.setPreferredSize(new Dimension(95,0));
east.setBackground(new Color(70,250,250));
east.setLayout(new FlowLayout(FlowLayout.CENTER,10,5));
String lis[]={"橡皮","清除","重畫"};
for(int i=0;i<lis.length;i++){
JButton jbu2=new JButton(lis[i]+"");
east.add(jbu2);
jbu2.addActionListener(listener1);
jbu2.setBackground(new Color(155,155,155));
jbu2.setForeground(new Color(255,255,255));
jbu2.setPreferredSize(new Dimension(60,30));
}
String str[]={"畫筆","直線","圓","橢圓","六角星","矩形","綵線","球","綵球"};
for(int i=0;i<str.length;i++){
JButton jbu=new JButton(str[i]+"");
east.add(jbu);
jbu.setBackground(new Color(240,20,20));
jbu.setForeground(new Color(255,255,255));
jbu.setPreferredSize(new Dimension(90,47));
jbu.addActionListener(listener1);
}
this.add(east,BorderLayout.EAST);
}
public void WestPanel(){
JPanel west=new JPanel();
west.setPreferredSize(new Dimension(150,0));
west.setLayout(new FlowLayout(FlowLayout.RIGHT,5,5));
west.add(new JLabel("顏色1:"));
west.setBackground(new Color(70,250,250));
for(int i=0;i<15;i++){
JButton jbu=new JButton();
jbu.setPreferredSize(new Dimension(30,30));
west.add(jbu);
jbu.setBackground(new Color(3*i,4*i,5*i));
}
west.add(new JLabel("顏色2:"));
for(int i=0;i<15;i++){
JButton jbu=new JButton();
jbu.setPreferredSize(new Dimension(30,30));
west.add(jbu);
jbu.setBackground(new Color(9*i,5*i+20,8*i+70));
}
west.add(new JLabel("顏色3:"));
for(int i=0;i<15;i++){
JButton jbu=new JButton();
jbu.setPreferredSize(new Dimension(30,30));
west.add(jbu);
jbu.setBackground(new Color(8*(15-i)+100,3*i+10,2*i));
}
west.add(new JLabel("顏色4:"));
for(int i=0;i<15;i++){
JButton jbu=new JButton();
jbu.setPreferredSize(new Dimension(30,30));
west.add(jbu);
jbu.setBackground(new Color(255-3*i,255-4*i,255-3*i));
}
this.add(west,BorderLayout.WEST);
}
public void paint(Graphics g){
super.paint(g);
if(listener2.array.array!=null){
for(int i=0;i<listener2.array.array.length;i++){
Shapes shape=listener2.array.array[i];
int x1=shape.x1;int x2=shape.x2;int y1=shape.y1;int y2=shape.y2;
if(shape.str.equals("直線")){
g.drawLine(x1,y1,x2,y2);
}
if(shape.str.equals("圓")){
g.drawOval(Math.min(x1, x2),Math.min(y1, y2),Math.abs((x2-x1+y2-y1)/2),Math.abs((x2-x1+y2-y1)/2));
}
if(shape.str.equals("橢圓")){
g.drawOval(Math.min(x1, x2),Math.min(y1, y2),Math.abs(x2-x1),Math.abs(y2-y1));
}
if(shape.str.equals("矩形")){
g.drawRect(Math.min(x1, x2),Math.min(y1, y2),Math.abs(x2-x1),Math.abs(y2-y1));
}
if(shape.str.equals("球")){
for(int j=0;j<100;j++){
g.setColor(new Color(j,j*2+50,j+100));
g.fillOval(x1+j/2-50,y1+j/2-50,100-j,100-j);
}
}
if(shape.str.equals("六角星")){
g.drawLine((x1+x2)/2, y1, (x2+2*x1)/3, (y2+3*y1)/4);
g.drawLine((x1+x2)/2, y1, (2*x2+x1)/3, (y2+3*y1)/4);
g.drawLine((x2+2*x1)/3, (y2+3*y1)/4, x1,(y2+3*y1)/4 );
g.drawLine(x1, (y2+3*y1)/4, (x2+5*x1)/6, (y1+y2)/2);
g.drawLine((x2+5*x1)/6, (y1+y2)/2, x1, (3*y2+y1)/4);
g.drawLine(x1, (3*y2+y1)/4, (x2+2*x1)/3, (3*y2+y1)/4);
g.drawLine( (x2+2*x1)/3,(3*y2+y1)/4, (x1+x2)/2, y2);
g.drawLine((x1+x2)/2, y2, (2*x2+x1)/3, (3*y2+y1)/4);
g.drawLine((2*x2+x1)/3, (3*y2+y1)/4, x2, (3*y2+y1)/4);
g.drawLine(x2, (3*y2+y1)/4, (5*x2+x1)/6, (y2+y1)/2);
g.drawLine((5*x2+x1)/6,(y2+y1)/2, x2, (y2+3*y1)/4);
g.drawLine(x2, (y2+3*y1)/4,(2*x2+x1)/3,(y2+3*y1)/4);
}
if(shape.str.equals("綵球")){
for(int j=0;j<50;j++){
g.setColor(new Color(2*j+155,2*j,2*j+155));
g.fillOval(x1+j-50,y1+j-50,100-2*j,100-2*j);
}
}
if(shape.str.equals("綵線")){
g.setColor(new Color(x2%255,y2%255,x2%255));
g.drawLine(x1,y1,x2,y2);
}
if(shape.str.equals("畫筆")){
g.drawLine(x1,y1, x2, y2);
x1=x2;
y1=y2;
}
if(shape.str.equals("橡皮")){
g.setColor(new Color(238,238,238));
g.fillOval(x1, y1, 25,25);
}
}
}
}
}
該代碼就是繼承該接口的類,點擊登錄按鈕後就會出現一個畫圖板窗體。
2、MouseListener(鼠標監聽器):該監聽器可以加在窗體上或Jpanel等組件上。上面一段代碼中的窗體上就加了該監聽器。
public class Mouse implements MouseListener,MouseMotionListener{
private String str;
Array array=new Array();
private int x1,x2,y1,y2;
private ButtonListener listener;
private Graphics g;
public Mouse(Graphics g,ButtonListener listener){
this.g=g;
this.listener=listener;
}
public Mouse(){
}
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
x1=e.getX();
y1=e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
x2=e.getX();
y2=e.getY();
if(listener.getSTR().equals("圓")&&x1>=150&&x2<=705&&y1>=100&&x2>=150&&y2>=100&&x1<=705){
g.drawOval(Math.min(x1, x2),Math.min(y1, y2),Math.abs((x2-x1+y2-y1)/2),Math.abs((x2-x1+y2-y1)/2));
Shapes shape=new Shapes();
shape.x1=x1;shape.x2=x2;shape.y1=y1;shape.y2=y2;shape.str="圓";
array.add(shape);
}
else if(listener.getSTR().equals("直線")&&x1>=150&&x2<=705&&y1>=100&&x2>=150&&y2>=100&&x1<=705){
Graphics g1=(Graphics)g;
g1.drawLine(x1,y1,x2,y2);
Shapes shape=new Shapes();
shape.x1=x1;shape.x2=x2;shape.y1=y1;shape.y2=y2;shape.str="直線";
array.add(shape);
}
else if(listener.getSTR().equals("橢圓")&&x1>=150&&x2<=705&&y1>=100&&x2>=150&&y2>=100&&x1<=705){
g.drawOval(Math.min(x1, x2),Math.min(y1, y2),Math.abs(x2-x1),Math.abs(y2-y1));
Shapes shape=new Shapes();
shape.x1=x1;shape.x2=x2;shape.y1=y1;shape.y2=y2;shape.str="橢圓";
array.add(shape);
}else if(listener.getSTR().equals("矩形")&&x1>=150&&x2<=705&&y1>=100&&x2>=150&&y2>=100&&x1<=705){
g.drawRect(Math.min(x1, x2),Math.min(y1, y2),Math.abs(x2-x1),Math.abs(y2-y1));
Shapes shape=new Shapes();
shape.x1=x1;shape.x2=x2;shape.y1=y1;shape.y2=y2;shape.str="矩形";
array.add(shape);
}else if(listener.getSTR().equals("球")&&x1>=150&&x2<=705&&y1>=100&&x2>=150&&y2>=100&&x1<=705){
Color co=g.getColor();
for(int i=0;i<100;i++){
g.setColor(new Color(i,i*2+50,i+100));
g.fillOval(x1+i/2-50,y1+i/2-50,100-i,100-i);
}
Shapes shape=new Shapes();
g.setColor(co);
shape.x1=x1;shape.x2=x2;shape.y1=y1;shape.y2=y2;shape.str="球";
array.add(shape);
}else if(listener.getSTR().equals("六角星")&&x1>=150&&x2<=705&&y1>=100&&x2>=150&&y2>=100&&x1<=705){
g.drawLine((x1+x2)/2, y1, (x2+2*x1)/3, (y2+3*y1)/4);
g.drawLine((x1+x2)/2, y1, (2*x2+x1)/3, (y2+3*y1)/4);
g.drawLine((x2+2*x1)/3, (y2+3*y1)/4, x1,(y2+3*y1)/4 );
g.drawLine(x1, (y2+3*y1)/4, (x2+5*x1)/6, (y1+y2)/2);
g.drawLine((x2+5*x1)/6, (y1+y2)/2, x1, (3*y2+y1)/4);
g.drawLine(x1, (3*y2+y1)/4, (x2+2*x1)/3, (3*y2+y1)/4);
g.drawLine( (x2+2*x1)/3,(3*y2+y1)/4, (x1+x2)/2, y2);
g.drawLine((x1+x2)/2, y2, (2*x2+x1)/3, (3*y2+y1)/4);
g.drawLine((2*x2+x1)/3, (3*y2+y1)/4, x2, (3*y2+y1)/4);
g.drawLine(x2, (3*y2+y1)/4, (5*x2+x1)/6, (y2+y1)/2);
g.drawLine((5*x2+x1)/6,(y2+y1)/2, x2, (y2+3*y1)/4);
g.drawLine(x2, (y2+3*y1)/4,(2*x2+x1)/3,(y2+3*y1)/4);
Shapes shape=new Shapes();
shape.x1=x1;shape.x2=x2;shape.y1=y1;shape.y2=y2;shape.str="六角星";
array.add(shape);
}else if(listener.getSTR().equals("綵球")&&x1>=150&&x2<=705&&y1>=100&&x2>=150&&y2>=100&&x1<=705){
Color co=g.getColor();
for(int i=0;i<50;i++){
g.setColor(new Color(2*i+155,2*i,2*i+155));
g.fillOval(x1+i-50,y1+i-50,100-2*i,100-2*i);
}
g.setColor(co);
Shapes shape=new Shapes();
shape.x1=x1;shape.x2=x2;shape.y1=y1;shape.y2=y2;shape.str="綵球";
array.add(shape);
}
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseDragged(MouseEvent e) {
x2=e.getX();
y2=e.getY();
if(listener.getSTR().equals("綵線")&&x1>=150&&x2<=705&&y1>=100&&x2>=150&&y2>=100&&x1<=705){
Color co=g.getColor();
g.setColor(new Color(x2%255,y2%255,x2%255));
g.drawLine(x1, y1, x2, y2);
g.setColor(co);
Shapes shape=new Shapes();
shape.x1=x1;shape.x2=x2;shape.y1=y1;shape.y2=y2;shape.str="綵線";
array.add(shape);
}else if(listener.getSTR().equals("畫筆")&&x1>=150&&x2<=705&&y1>=100&&x2>=150&&y2>=100&&x1<=705){
g.drawLine(x1, y1, x2, y2);
Shapes shape=new Shapes();
shape.x1=x1;shape.x2=x2;shape.y1=y1;shape.y2=y2;shape.str="畫筆";
array.add(shape);
x1=x2;
y1=y2;
}else if(listener.getSTR().equals("橡皮")&&x1>=150&&x2<=705&&y1>=100&&x2>=150&&y2>=100&&x1<=705){
Color co=g.getColor();
g.setColor(new Color(238,238,238));
g.fillOval(x1, y1, 25,25);
g.setColor(co);
x1=x2;
y1=y2;
Shapes shape=new Shapes();
shape.x1=x1;shape.x2=x2;shape.y1=y1;shape.y2=y2;shape.str="橡皮";
array.add(shape);
}
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
}
這就是繼承該接口的類,這樣就可以根據鼠標的動作來實現其中的方法了。
MousemotionListener(鼠標動作監聽器)以及KeyListener(鍵盤監聽器)的監聽機制基本差不多,就不一一舉例了。總之,事件監聽功能非常強大,可以根據需要來選擇要實現哪個監聽機制