首页 > 代码库 > 2014/10/5
2014/10/5
1
Paint()
1 package hello; 2 import java.awt.*; 3 public class helloworld 4 { 5 public static void main(String[] args)throws Exception 6 { 7 new F().launchFrame(); 8 } 9 }10 11 class F extends Frame12 {13 public void launchFrame()14 {15 setBounds(200, 300, 600, 700);16 setVisible(true);17 }18 public void paint(Graphics g) //Frame出现自动调用19 {20 Color c = g.getColor(); //获得原来的颜色21 g.setColor(Color.green); //22 g.fillOval(50, 50, 30, 30);23 g.setColor(Color.ORANGE);24 g.fillRect(90, 90, 30, 30);25 g.setColor(c); //恢复原来的颜色26 }27 }
2
MouseListener, MouseAdapter(鼠标适配器,适配器可以防止定义无意义方法,每个监听器都有Adapter)子类,实现了所有方法,都是空的
Point类
152
1 package hello; 2 import java.awt.*; 3 import java.awt.event.*; 4 import java.util.*; 5 public class helloworld 6 { 7 public static void main(String[] args)throws Exception 8 { 9 new F("wori");10 }11 }12 13 class F extends Frame 14 {15 ArrayList al = new ArrayList(); //存放鼠标点的点16 public F(String s)17 {18 super(s);19 setLayout(null);20 setBounds(200, 300, 600, 700);21 setVisible(true);22 this.addMouseListener(new M()); //监听鼠标23 24 }25 public void paint(Graphics g)26 {27 Iterator i = al.iterator(); //28 Color c = g.getColor();29 g.setColor(Color.CYAN);30 while(i.hasNext())31 {32 Point p = (Point)i.next();33 g.fillOval(p.x, p.y, 10, 10);34 }35 36 }37 void addPoint(int a, int b)38 {39 al.add(new Point(a, b));40 }41 class M extends MouseAdapter //MouseAdapter是MouseListener的子类42 {43 public void mousePressed(MouseEvent e)44 {45 F f = (F)e.getSource(); //getSource()是获得监听的对象46 addPoint(e.getX(), e.getY()); //47 f.repaint(); //刷新图像48 49 }50 }51 }
3
WindowEvent WindowListener
1 package hello; 2 import java.awt.*; 3 import java.awt.event.*; 4 import java.util.*; 5 public class helloworld 6 { 7 public static void main(String[] args)throws Exception 8 { 9 Frame f = new Frame("wocao");10 11 f.setLayout(null);12 f.setBounds(300, 300, 300, 300);13 f.addWindowListener(new WL());14 f.setVisible(true);15 }16 17 }18 19 class WL extends WindowAdapter20 {21 public void windowClosing(WindowEvent e)22 {23 24 ((Frame)e.getSource()).setVisible(false); //注意()一定要套(Frame)e.getSource().setVisible(false);不行 ***25 System.exit(0); //0是正常退出26 }27 }
4
KeyListener KeyListener
1 package hello; 2 import java.awt.*; 3 import java.awt.event.*; 4 import java.util.*; 5 public class helloworld 6 { 7 public static void main(String[] args)throws Exception 8 { 9 Frame f = new Frame("wocao");10 11 f.addKeyListener(new Kl()); //不能加TextField,加了输入进文本框,就不是Frame的事件了12 f.addWindowListener(new Wl());13 f.pack();14 f.setVisible(true);15 }16 17 }18 19 20 class Kl extends KeyAdapter21 {22 public void keyTyped(KeyEvent e)23 {24 int c = e.getKeyCode();25 26 System.out.println(c + " is typed");27 28 }29 }30 31 32 class Wl extends WindowAdapter33 {34 public void windowClosing(WindowEvent e)35 {36 ((Frame)e.getSource()).setVisible(false);37 System.exit(0);38 }39 }
2014/10/5
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。