BUTTON事件监听
package com.zzk.cn; import java.awt.*; import java.awt.event.*; public class TestActionEvent { public static void main(String[] args) { Frame f=new Frame("Test"); Button b=new Button("Press Me!"); Monitor bh=new Monitor(); b.addActionListener(bh); f.add(b,BorderLayout.CENTER); f.pack(); f.setVisible(true); } } class Monitor implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("a button has been pressed"); } }
/* 范例名称:Java事件处理举例 * 源文件名称:TestActionEvent2.java * 要 点: * 1. 一个事件源组件上可以同时注册多个监听器 * 2. 一个监听器对象可以同时注册到多个事件源组件上 * 3. 事件源的信息可以随它所触发的事件自动传递到所有注册过的监听器 */ package com.zzk.cn; import java.awt.*; import java.awt.event.*; public class TestActionEvent2 { public static void main(String[] args) { Frame f=new Frame("test"); Button b1=new Button("Start"); Button b2=new Button("Stop"); Monitor2 bh=new Monitor2(); b1.addActionListener(bh); b2.addActionListener(bh); b2.setActionCommand("gameover"); f.add(b1,"North"); f.add(b2,"Center"); f.pack(); f.setVisible(true); } } class Monitor2 implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("a button has been pressed,"+ "the relative info is:\n"+e.getActionCommand()); } }
TEXTFIELD事件监听
输入TEXT中的字符都可以在命令行中显示出来
package com.zzk.cn; import java.awt.*; import java.awt.event.*; public class TFActionEvent { public static void main(String[] args) { new TFFrame(); } } class TFFrame extends Frame { TFFrame() { TextField tf=new TextField(); add(tf); tf.addActionListener(new TFActionListener()); pack(); setVisible(true); } } class TFActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { TextField tf=(TextField)e.getSource(); System.out.println(tf.getText()); tf.setText(""); } }
增加密码内容在输入框中显示*
package com.zzk.cn; import java.awt.*; import java.awt.event.*; public class TFPassword { public static void main(String[] args) { new TFFrame2(); } } class TFFrame2 extends Frame { TFFrame2() { TextField tf=new TextField(); add(tf); tf.addActionListener(new TFActionListener()); //注意这个!回显字符 tf.setEchoChar('*'); pack(); setVisible(true); } } class TFPasswordListener2 implements ActionListener { public void actionPerformed(ActionEvent e) { TextField tf=(TextField)e.getSource(); System.out.println(tf.getText()); tf.setText(""); } }
TEXTFIELD加法计算器 程序
package com.zzk.cn; import java.awt.*; import java.awt.event.*; public class TFMath { public static void main(String[] args) { new TFFrame3().launchFrame(); } } class TFFrame3 extends Frame { TextField num1,num2,num3; public void launchFrame() { num1=new TextField(10); num2=new TextField(10); num3=new TextField(15); Label lblPlus=new Label("+"); Button btnEqual=new Button("="); btnEqual.addActionListener(new MyMonitor()); setLayout(new FlowLayout()); add(num1); add(lblPlus); add(num2); add(btnEqual); add(num3); pack(); setVisible(true); } private class MyMonitor implements ActionListener { public void actionPerformed(ActionEvent e) { int n1=Integer.parseInt(num1.getText()); int n2=Integer.parseInt(num2.getText()); num3.setText(""+(n1+n2)); } } }
PAINT的使用
package com.zzk.cn; import java.awt.*; public class TestPaint { public static void main(String[] args) { new PaintFrame().launchFrame(); } } class PaintFrame extends Frame { public void launchFrame() { setBounds(200,200,640,460); setVisible(true); } public void paint(Graphics g) { Color c=g.getColor(); g.setColor(Color.red); g.fillOval(50, 50, 30, 30);//填充椭圆 g.setColor(Color.green); g.fillRect(80, 80, 40, 40);//填充矩形 g.setColor(c); } }
画点程序
package com.zzk.cn; import java.awt.*; import java.awt.event.*; import java.util.*; public class MyMouseAdapter { public static void main(String[] args) { new MyFrame5("drawing..."); } } class MyFrame5 extends Frame { ArrayList points=null; MyFrame5(String s) { super(s); points=new ArrayList();//将点放在数组中 setLayout(null);//取消默认布局 setBounds(300,300,400,300); this.setBackground(new Color(204,204,255)); setVisible(true); this.addMouseListener(new Monitor3()); } public void paint(Graphics g) { Iterator i=points.iterator(); while(i.hasNext()) { Point p=(Point)i.next(); g.setColor(Color.blue); g.fillOval(p.x, p.y, 10, 10); } } public void addPoint(Point p) { points.add(p); } } class Monitor3 extends MouseAdapter { public void mousePressed(MouseEvent e) { MyFrame5 f=(MyFrame5)e.getSource(); f.addPoint(new Point(e.getX(),e.getY())); f.repaint(); } }
关掉窗口
package com.zzk.cn; import java.awt.*; import java.awt.event.*; public class TestWindowClose { public static void main(String args[]) { new MyFrame55("MyFrame"); } } class MyFrame55 extends Frame { MyFrame55(String s) { super(s); setLayout(null); setBounds(300,300,400,300); this.setBackground(new Color(204,204,255)); setVisible(true); this.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { setVisible(false); System.exit(-1); } } ); } }
第二种写法:
内部类写法
package com.zzk.cn; import java.awt.*; import java.awt.event.*; public class TestWindowClose { public static void main(String args[]) { new MyFrame55("MyFrame"); } } class MyFrame55 extends Frame { MyFrame55(String s) { super(s); setLayout(null); setBounds(300,300,400,300); this.setBackground(new Color(204,204,255)); setVisible(true); this.addWindowListener(new MyWindowMonitor()); // this.addWindowListener( // new WindowAdapter() { // public void windowClosing(WindowEvent e) { // setVisible(false); // System.exit(-1); // } // } // ); } class MyWindowMonitor extends WindowAdapter { public void windowClosing(WindowEvent e) { setVisible(false); System.exit(0); } } }
package com.zzk.cn; /* 范例名称:匿名类在事件处理中的使用 * 源文件名称:TestAnonymous2.java * 要 点: * 1. 匿名类只能是内部类 * 2. 匿名类的两种创建方式----既可以继承父类、也可以单重实现接口 */ import java.awt.*; import java.awt.event.*; public class TestAnonymous2 { Frame f=new Frame("Test"); TextField tf=new TextField(10); Button b1=new Button("Start"); public TestAnonymous2() { f.add(b1,"North"); f.add(tf,"South"); b1.addActionListener(new ActionListener() { private int i; public void actionPerformed(ActionEvent e) { tf.setText(e.getActionCommand()+ ++i); } } ); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); f.pack(); f.setVisible(true); } public static void main(String args[]) { new TestAnonymous2(); } }
键盘上下左右事件
package com.zzk.cn; import java.awt.*; import java.awt.event.*; public class TestKey { public static void main(String[] args) { new KeyFrame().launchFrame(); } } class KeyFrame extends Frame { public void launchFrame() { setSize(200,200); setLocation(300,300); addKeyListener(new MyKeyMonitor()); setVisible(true); } class MyKeyMonitor extends KeyAdapter { public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); if(keyCode == KeyEvent.VK_UP) { System.out.println("UP"); } if(keyCode == KeyEvent.VK_DOWN) { System.out.println("DOWN"); } if(keyCode == KeyEvent.VK_LEFT) { System.out.println("LEFT"); } if(keyCode == KeyEvent.VK_RIGHT) { System.out.println("RIGHT"); } } } }