发布时间 : 星期日 文章(完整word版)Java知识总结完整版,推荐文档更新完毕开始阅读
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(\ } } }
Graphics类
每个Component都有一个paint(Graphics g)用于实现绘图目的,每次重画该Component时都自动调用paint方法
Graphics类中提供了许多绘图方法,如:
drawRect(int x,int y,int width,int height)
fillRoundRect(int x,int y,int width,int height,int arcWidth,int arcHeight)等
Adapter&repaint import java.awt.*;
import java.awt.event.*; import java.util.*;
public class MyMouseAdapter{
public static void main(String args[]) { new MyFrame(\ } }
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(); } }
鼠标事件适配器
抽象类java.awt.event.MouseAdapter实现了MouseListener接口,可以使用其子类作为MouseEvent的监听器,只要重写其相应的方法即可 对于其他的监听器,也有对应的适配器
使用适配器可以避免监听器类定义没有必要的空方法 repaint-update()-paint()