VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > Java教程 >
  • GUI学习 鼠标,窗口,键盘监听

GUI基础学习二

2.1画笔

package paint;

import java.awt.*;

public class Painting {
    public static void main(String[] args) {
        new MyPaint().loadFrame();
    }
}
class MyPaint extends Frame{
    public void loadFrame(){
        setBounds(200,200,400,500);
        setVisible(true);
    }
    @Override
    public void paint(Graphics g) {
        g.setColor(Color.red);
        //g.drawOval(100,100,100,100);
        g.fillOval(100,100,100,100);
        g.setColor(Color.GREEN);
        g.fillRect(200,100,100,100);
        //使用完画笔后最好重置画笔颜色
        g.setColor(Color.black);
    }
}

2.2鼠标监听

目的:实现鼠标绘画

package paint;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;

public class TestMouse {
    public static void main(String[] args) {
        new MyFrame("画图");
    }
}
class MyFrame extends Frame{
    ArrayList arrayList;
    //画画需要画笔,需要监听当前鼠标的位置,需要集合来储存这个点

    public MyFrame(String title) {
        super(title);
        //设置画板位置和大小
        setBounds(200,200,400,400);
        //存储鼠标点击的点
        arrayList=new ArrayList<>();

        //鼠标监听器,正对这个窗口
        this.addMouseListener(new MyMouseListener());

        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        //画画,监听鼠标的事件
        Iterator iterator=arrayList.iterator();
        while(iterator.hasNext()){
            Point point=(Point) iterator.next();
            g.setColor(Color.BLUE);
            g.fillOval(point.x,point.y,10,10);
        }
    }

    //添加一个点到界面上
    public void addPaint(Point point){
        arrayList.add(point);
    }
    private class MyMouseListener extends MouseAdapter{
        //s鼠标:按下 弹起 按住不放
        @Override
        public void mousePressed(MouseEvent e) {
            MyFrame myFrame=(MyFrame) e.getSource();
            //当我们点击产生一个点
            myFrame.addPaint(new Point(e.getX(),e.getY()));

            //每次点击鼠标都需要重新画一遍
            myFrame.repaint();
        }
    }
}

2.3窗口监听

package paint;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestWindow {
    public static void main(String[] args) {
        new WindowFrame();
    }
}
class WindowFrame extends Frame{
    public WindowFrame() {
        setVisible(true);
        setBounds(100,100,300,300);
        setBackground(Color.blue);
        //addWindowListener(new MyWindowListener());
        this.addWindowListener(
                //匿名内部类
                new WindowAdapter() {
                    
                    @Override
                    public void windowClosing(WindowEvent e) {
                        //g关闭窗口
                        System.out.println("windowClosing");
                        System.exit(0);
                    }
                    
                    @Override
                    public void windowActivated(WindowEvent e) {
                        //激活窗口
                        WindowFrame source = (WindowFrame) e.getSource();
                        source.setTitle("被激活了");
                        System.out.println("windowActivated");
                    }
                });
    }
}

//    class MyWindowListener extends WindowAdapter{
//        @Override
//        public void windowClosing(WindowEvent e) {
//            setVisible(false);//隐藏窗口
//            System.exit(0);//正常退出
//        }
//    }


2.4键盘监听

package paint;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class TestKeyListener {
    public static void main(String[] args) {
        new KeyFrame();
    }
static class KeyFrame extends Frame{
        public KeyFrame()  {
            setBounds(100,100,300,300);
            setVisible(true);

            this.addKeyListener(new KeyAdapter() {
                //键盘按下
                @Override
                public void keyPressed(KeyEvent e) {
                    //获得键盘按下的键是哪一个,获取当前建的码
                    int keycode=e.getKeyCode();
                    System.out.println(keycode);
                    if (keycode==KeyEvent.VK_UP){
                        System.out.println("你按下了上建");
                    }
                    //根据按下不同操作,产生不同的结果:
                }
            });
        }
    }
}
出处:https://www.cnblogs.com/2436986zyr/p/15389559.html

相关教程