java界面编程(下)
一、事件监听
package com.lost.ActionEvent; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; class Monitor2 implements ActionListener{ public void actionPerformed(ActionEvent e){ System.out.println("a button has been pressed,"+"the relative info is:\n"+e.getActionCommand()); /*使用返回的监听对象e调用getActionCommand()方法获取两个按钮执行单击命令后的返回信息 根据返回信息的不同区分开当前操作的是哪一个按钮,btn1没有使用setActionCommand()方法设置 则btn1返回的信息就是按钮上显示的文本*/ } } public class ActionEventDemo2 { public static void main(String[] args) { // TODO Auto-generated method stub Frame f = new Frame("ActionEventDemo2"); Button btn1 = new Button("start"); Button btn2 = new Button("stop"); Monitor2 m2 = new Monitor2(); btn1.addActionListener(m2); btn1.setActionCommand("Game Start()"); btn2.addActionListener(m2); f.add(btn1,BorderLayout.NORTH); f.add(btn2,BorderLayout.CENTER); f.pack(); f.setVisible(true); addWindowClosingEvent(f); } private static void addWindowClosingEvent(Frame f) { f.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub super.windowClosing(e); System.exit(0); } }); } }
为Componet添加监听对象,设置监听动作。
二、TextField事件监听
TextField对象可能发生Action(光标在文本框内敲回车)事件,与该事件对应的事件类是java.awt.event.ActionEvent。
用来处理ActionEvent事件是实现了java.awt.event.ActionListener接口的类对象。ActionListener接口定义有方法:
public void actionPerformed(ActionEvent e)
实现该接口的类要在该方法中添加处理事件(Action)语句
使用addActionListener(ActonListener l)方法为TextField对象注册一个ActionListener对象,当TextField对象发生Action事件时,会生成一个ActionEvent对象,该对象作为参数传递给ActionListener对象的actionPerformer方法在方法中可以获取该对象的信息,并作相应的处理。
package com.lost.ActionEvent; import java.awt.Button; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Label; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestMath1 { private static void addWindowClosingEvent(Frame f) { f.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub super.windowClosing(e); System.exit(0); } }); } public static void main(String[] args) { // TODO Auto-generated method stub addWindowClosingEvent(new MyFrame()); } } class MyFrame extends MyFrameField { TextField num1,num2,num3; public void launchFrame() { num1 = new TextField(10); num2 = new TextField(15); num3 = new TextField(15); Label lblPlus = new Label("+"); Button btnEqual = new Button("="); btnEqual.addActionListener(new MyMonitorbtnEqual(this)); setLayout(new FlowLayout()); add(num1); add(lblPlus); add(num2); add(btnEqual); add(num3); pack(); setVisible(true); } } class MyMonitorbtnEqual implements ActionListener { MyFrame myFrame = null; MyMonitorbtnEqual(MyFrame myFrame) { this.myFrame = myFrame; } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub int n1 = Integer.parseInt(myFrame.num1.getText()); int n2 = Integer.parseInt(myFrame.num2.getText()); myFrame.num3.setText(""+(n1+n2)); myFrame.num1.setText(""); myFrame.num2.setText(""); } }
三、内部类
可以方便的访问包装类的成员。
可以更清楚的组织逻辑,防止不应该被其它类访问的类进行访问。
使用情况:
当该类不允许或不需要其他类进行访问时。
内部类可以畅通无阻的访问外部类(内部类的包装类)的所有成员变量和方法。
结:
1、可以很方便的访问外部类定义的成员变量和方法。
2、当某一个类不需要其它类访问的时候就把这个类声明为内部类。
四、Graphics类
每个Componnet都有一个paint(Graphic g)用于实现绘图目的,每次重画该Component时都自动调用paint方法。
graphics类中提供了许多绘图方法,如:
drawRect(int x,int y,int witdh,int height);
fillRoundRect(int x,int y, int width,int height,int arcWidth,int arcHeight)
package com.lost.ActionEvent; import java.awt.Color; import java.awt.Frame; import java.awt.Graphics; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; class MyPaint extends Frame{ public void launchFrame() { setBounds(200,200,640,480); setVisible(true); } public void paint(Graphics g) { //g 要做一副画 g.setColor(Color.RED); g.fillOval(100, 100, 100, 100); g.setColor(Color.green); g.fillRect(150, 200, 200, 200); Color c = g.getColor(); g.setColor(c); } } public class TestPaint { private static void addWindowClosingEvent(Frame f) { f.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub super.windowClosing(e); System.exit(0); } }); } public static void main(String[] args) { // TODO Auto-generated method stub MyPaint paint = new MyPaint(); paint.launchFrame(); addWindowClosingEvent(paint); } }
五、鼠标事件适配器
抽象类java.awt.event.MouseAdapter实现了MouseListener接口,可以使用其子类作为MouseEvnet的监听器,只要重写其相应方法即可。
对于其他的监听器,也有对应的适配器。
使用适配器可以避免监听器类定义没有必要的空方法。
package com.lost.event; import java.awt.*; import java.awt.event.*; import java.util.*; public class MyMouseAdapter{ private static void addWindowClosingEvent(Frame f) { f.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub super.windowClosing(e); System.exit(0); } }); } public static void main(String args[]) { MyFrame f = new MyFrame("drawing..."); addWindowClosingEvent(f); } } class MyFrame extends Frame { ArrayList points = null; MyFrame(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 Monitor()); } 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 Monitor extends MouseAdapter { public void mousePressed(MouseEvent e) { MyFrame f = (MyFrame)e.getSource(); f.addPoint(new Point(e.getX(),e.getY())); f.repaint(); } }
六、windows事件-----窗口事件
window事件所对应的事件类为WindowEvent,所对应的事件监听接口为WindowListener
windowListener定义的方法有
public void windowOpened(WindowEvent e)
public void windowClosing(WindowEvent e)
public void windowClosed(WindowEvent e)
public void windowconified(WindowEvent e)
public void windowDeconified(WindowEvent e)
public void WindowsActivated(WindowEvent e)
public void windowDeactivated(WindowEvent e)
与windowListener对应的适配器为
windowAdapter
package com.lost.event; import java.awt.Color; import java.awt.Frame; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class windowEventDemo { private static void addWindowClosingEvent(Frame f) { f.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub super.windowClosing(e); System.exit(0); } }); } public static void main(String[] args) { // TODO Auto-generated method stub new WindowFrame(""); } } class WindowFrame extends Frame { public WindowFrame(String s) { super(s); setBounds(200,200,400,300); setLayout(null); setBackground(new Color(204,204,255)); setVisible(true); this.addWindowListener(new WindowMoniter()); this.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub setVisible(false); System.exit(-1); } }); } class WindowMoniter extends WindowAdapter { @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub //super.windowClosing(e); setVisible(false); System.exit(0); } } }
七、键盘响应事件
package com.lost.event; import java.awt.Frame; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.lang.model.type.NullType; public class TestKeyEvent { public static void main(String[] args) { // TODO Auto-generated method stub new KeyFrame("键盘响应事件"); } } class KeyFrame extends Frame { KeyFrame(String s) { super(s); setBounds(200,200,400,300); setLayout(null); setVisible(true); addKeyListener(new KeyMonitor()); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub setVisible(false); System.exit(0); } }); } class KeyMonitor extends KeyAdapter { @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub int keycode = e.getKeyCode(); if(keycode == KeyEvent.VK_UP) { System.out.println("你按的是up键"); } } } }