画笔
package GUI.lee.test;
import java.awt.*;
public class Demo5 {
public static void main(String[] args) {
new MyPaint().LaodFrame();
}
}
class MyPaint extends Frame {
public void LaodFrame(){
setBounds(500,500,500,500);
setVisible(true);
}
//重写,画笔工具
@Override
public void paint(Graphics g) {
//super.paint(g);
//需要颜色、可以画画
g.setColor(Color.orange);
g.drawOval(100,100,200,200);
g.setColor(Color.green);
g.fillRect(250,250,200,200);
//画笔用完还原到最初颜色
}
}
鼠标监听
目的:鼠标画画(点击)
package lesson01;
import com.sun.corba.se.impl.protocol.giopmsgheaders.FragmentMessage;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
public class TestMouseListener {
public static void main(String[] args) {
new Myframe("鼠标监听事件");
}
}
class Myframe extends Frame {
//画画需要画笔,还需要监听鼠标当前的位置,需要集合来存储点
ArrayList points;
//构造器,供main函数new 对象时调用
public Myframe(String title) {
super(title);
setVisible(true);
setBounds(50,50,300,300);
//集合存放鼠标的点位置
points = new ArrayList<>();
//添加鼠标监听事件
addMouseListener(new MyMouseListener());//需要一个鼠标监听MouseListener
}
//重写Frame中画画的方法
@Override
public void paint(Graphics g) {
//迭代器将点画出来
Iterator iterator = points.iterator();
while (iterator.hasNext()) {
Point point = (Point) iterator.next();
g.setColor(Color.green);
g.fillOval(point.x,point.y,10,10);//fill设置实心
}
}
//将鼠标点添加到面板上
public void addpoint (Point point) {
points.add(point);
}
//添加鼠标内部类
//适配器模式,只需要继承实现接口的类即可,不需要实现接口,重写所以的方法
private class MyMouseListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
Myframe frame = (Myframe) e.getSource();
addpoint(new Point(e.getX(),e.getY()));
//每次点击鼠标重画一次
frame.repaint();
}
}
}
窗口监听(关闭、焦点)
package GUI.lee.test;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Demo7 {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame {
public WindowFrame() {
setVisible(true);
setBounds(200, 200, 500, 500);
//addWindowListener(new MyWindowListener());
this.addWindowListener(//匿名内部类
new WindowAdapter() {
@Override
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated");
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("windowClosed");
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
System.exit(0);
}
@Override
public void windowOpened(WindowEvent e) {
System.out.println("windowOpened");
}
}
);
}
//暂时不用
class MyWindowListener extends WindowAdapter {
@Override
public void windowClosing(WindowEvent e) {
//setVisible(false);//点击关闭后只是让窗口隐藏
System.exit(0);//点击关闭结束程序
}
}
}
键盘监听
class KFrame extends Frame{
public KFrame(){
setBounds(1,2,300,300);
setVisible(true);
this.addKeyListener(new KeyAdapter(){
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
char keyCode = e.getKeyChar();
System.out.println(e.getKeyCode());
if (keyCode == KeyEvent.VK_ESCAPE){
System.out.println("你按下了ESC");
}
}
});
}
}
迭代器Iterator
作用:将集合中的元素遍历取出
Iterator iterator = col.iterator();//得到一个集合的迭代器
while(iterator.hasNext()){//判断是否还有下一个元素
Col col = iterator.next();//next()作用:1、下移。2、将下移以后集合位置上的元素返回
}