《JAVA异常处理》

来源:互联网 发布:python简明中文教程 编辑:程序博客网 时间:2024/06/06 20:19

1.用java.awt.*;包,一般还要用java.swing.*;做GUI.

2.事件侦听器与侦听接口:

步骤:

(1)     继承implemnts ActionListener类

(2)     实现ActionListener接口的事件处理方法actionPerformed(ActionEvente)

(3)     注册事件侦听器,对象.addactrionListener(侦听器名);

3. 实例:(点击按钮设置屏背景颜色的例子)

import java.awt.*;

import java.awt.event.*;

class ColorAction implements ActionListener{//事件侦听器类

     privateActionEventFrame frame;//用主类对象作为变量,方便两类之间联系

     private Colorbackgroundcolor;

     publicColorAction(ActionEventFrame aef, Color c) {//带2个参数的构造方法

         frame =aef;

         backgroundcolor= c;

     }

     publicvoid actionPerformed(ActionEvent e) {//实现接口方法

         frame.setBackground(backgroundcolor);

     }

}

public class ActionEventFrame extends Frame {

     ActionEventFrame(){

         Panelpanel = new Panel();

         //创建三个事件源

         Buttonredbutton = new Button("红色");

         Buttongreenbutton = new Button("绿色");

         Buttonbluebutton = new Button("蓝色");

         panel.add(redbutton);

         panel.add(greenbutton);

         panel.add(bluebutton);

         add(panel,"South");

         //注册事件侦听器:同一个类的三个不同对象

         redbutton.addActionListener(newColorAction(this, Color.red));

         greenbutton.addActionListener(newColorAction(this, Color.green));

         bluebutton.addActionListener(newColorAction(this, Color.blue));

     }

     public staticvoid main(String argc[]) {

         ActionEventFramemyframe = new ActionEventFrame();

         myframe.setTitle("ActionEvent事件");

         myframe.setSize(200,150);

         myframe.setVisible(true);

     }

}

1.  窗口状态WindowEvent事件

import java.awt.*;

import java.awt.event.*;

public class WinEventTest extends Frame implementsWindowListener{

Frame owner;

Dialog dig;

public WinEventTest( ){//构造方法

setTitle("窗口事件处理");

addWindowListener(this);//注册监听者

this.owner=this;

setSize(200,200);

setVisible(true);

}

public void windowClosing(WindowEvent e){//重写窗口关闭方法

dig=new Dialog(owner,"确认退出",true);

Panel p=new Panel();

Button yes=new Button("是");

Button no=new Button("否");

p.add(yes);

p.add(no);

yes.addActionListener(new ActionListener(){//内部类形式注册及重写事件处理方法

public void actionPerformed(ActionEvent e){

owner.dispose();

}

});

no.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

dig.dispose();

}

});

dig.add(new Label("想要退出吗?"));

dig.add(p,"South");

dig.setSize(200,100);

dig.setVisible(true);

}

//实现WindowListener接口的所有方法

public void windowClosed(WindowEvent e){}

public void windowDeactivated(WindowEvent e){}

public void windowActivated(WindowEvent e){}

public void windowIconified(WindowEvent e){}

public void windowDeiconified(WindowEvent e){}

public void windowOpened(WindowEvent e){}

 

public static void main(String[] args){

WinEventTest obj=new WinEventTest();

}

}

2.  适配器(使其不用实现接口的所有方法,进行简化)

(上题更改后)

import java.awt.*;

import java.awt.event.*;

public class WinAdapterTest extends Frame {

     Frame owner;

     Dialog dig;

     public WinAdapterTest(){

         setTitle("窗口事件处理");

         this.owner=this;

         setSize(200,200);

         setVisible(true);

WindowListener winHandler=new WindowAdapter(){//内部类

public void windowClosing(WindowEvent e){

dig=new Dialog(owner,"确认退出",true);

Panel p=new Panel();

Button yes=new Button("是");

Button no=new Button("否");

p.add(yes);

p.add(no);

yes.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

owner.dispose();

}

});

no.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

dig.dispose();

}

} );

dig.add(new Label("想要退出吗?"));

dig.add(p,"South");

dig.setSize(200,100);

dig.setVisible(true);

}

};

addWindowListener(winHandler);//注册监听者

}

public static void main(String[] args){

         WinAdapterTest obj=newWinAdapterTest();

     }

}

 

3.  事件侦听器的几种方式


1.  鼠标事件处理

(实例)

import java.awt.*;

import java.awt.event.*;

public class MouseEventTest extends Frame {

Label mouseInfo1, mouseInfo2;

 

public MouseEventTest() {

     super("Mouse移动处理");

     mouseInfo1 = newLabel("");

     mouseInfo1.setBackground(Color.YELLOW);

     mouseInfo2 = newLabel("");

     mouseInfo2.setBackground(Color.YELLOW);

     add(mouseInfo1,"North");

     add(mouseInfo2,"South");

     addMouseMotionListener(newMouseMotionAdapter() {

          public voidmouseMoved(MouseEvent e) {

               mouseInfo1.setText(e.getX()+ " " + e.getY());//获取坐标

          }

     });

     addMouseListener(newMouseAdapter() {

          public voidmouseEntered(MouseEvent e) {//鼠标进入方法

               mouseInfo2.setText("Enter");

          }

          public voidmouseExited(MouseEvent e) {//鼠标离开方法

               mouseInfo2.setText("Exit");

          }

          public voidmousePressed(MouseEvent e) {//鼠标按下方法

               if(e.getButton() == e.BUTTON1)

                   mouseInfo2.setText("LeftButton");

               else if(e.getButton() == e.BUTTON2)

                   mouseInfo2.setText("MiddleButton");

               else if(e.getButton() == e.BUTTON3)

                   mouseInfo2.setText("RightButton");

          }

     });

}

public static void main(String[]args) {

     final MouseEventTest obj = newMouseEventTest();

     obj.setSize(300, 200);

     obj.setVisible(true);

     obj.addWindowListener(newWindowAdapter() {

          public voidwindowClosing(WindowEvent e) {

               obj.dispose();

          }

     });

}

}

2.  键盘事件处理

import java.awt.*;

import java.awt.event.*;

 

public class KeyEventTest extends Frame {

     Button man;

 

     publicKeyEventTest() {

         super("活用Key事件");

         setLayout(null);

         setSize(300,150);

         man = newButton("按下键盘的箭头键移动试试");

         man.setBounds(20,50, 150, 40);

         man.setBackground(Color.BLUE);

         man.setForeground(Color.WHITE);

         add(man);

         man.addKeyListener(newKeyAdapter() {

              publicvoid keyPressed(KeyEvent e) {

                   Stringdirection = e.getKeyText(e.getKeyCode());

                   intx = man.getX();

                   inty = man.getY();

                   if(direction.equals("Right"))

                       x+= 10;

                   elseif (direction.equals("Left"))

                       x-= 10;

                   elseif (direction.equals("Down"))

                       y+= 10;

                   elseif (direction.equals("Up"))

                       y-= 10;

                   man.setLocation(x,y);

              }

         });

     }

 

     public staticvoid main(String[] args) {

         finalKeyEventTest obj = new KeyEventTest();

         obj.setVisible(true);

         obj.addWindowListener(newWindowAdapter() {

              publicvoid windowClosing(WindowEvent e) {

                   obj.dispose();

              }

         });

     }

}

n  char getKeyChar(): 返回按键表示的字符

例如:直接按“a”,返回字符a,若按“Shift+a”,则返回字符A,

n   int getKeyCode():返回整数 keyCode(键盘按键的编号)

     无论是按”a”,还是”Shift+a”,均返回65。KeyEvent类定义许多常量来表示键码值,例如:VK_F1~VK_F12, VK_LEFT,VK_KP_UP,VK_HOME,VK_SHIFT,VK_ALT,,… 具体见API文档

n  String getKeyText(intkeyCode) :返回keyCode所对应的String,即取得按键表示的文字。如“HOME”、“F1”或“A” 

0 0
原创粉丝点击