鱼C论坛

 找回密码
 立即注册
查看: 3101|回复: 0

[学习笔记] GUI-lesson3

[复制链接]
发表于 2021-9-19 14:52:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 愷龍 于 2021-9-19 15:52 编辑

|画笔 Paint
画笔是 Frame 类中的一个方法,

通过重写这个方法拿到参数可以用参数在面板或者窗口上画出自己想要的图案

不能通过 new 来创建画笔
import java.awt.*;
import javax.swing.*;
public class PaintTest {
    public static void main(String[] args) {
                new myFrame();  //调用
         }
}
class myFrame extends  Frame{  //创建一个窗口类继承 Frame类
    public myFrame(){  //构造方法
       super("我的第一个画板");  //调用父类的构造方法
       setVisible(true);             //设置窗口可见
       setBounds(200,200,500,500);  //设置初始位置  长宽
    }
    @Override   //重写paint方法
    public void paint(Graphics g) {     //通过 g. 可以实现多种画笔功能
        g.setColor(Color.red);           //设置画笔的颜色 用完后将画笔调回本色 
        g.drawOval(100,300,200,200);  //用画笔画一个空心的圆 
        g.fillOval(100,100,200,200);  //用画笔画一个实心圆
        //参数为 初始坐标 x y 和长 宽
        g.setColor(Color.BLACK);  //将画笔调为原始的黑色
    }
}

                               
登录/注册后可看大图

|监听事件:
Frame 关闭事件
WindowClosing 窗口关闭事件

WindowActvate 窗口激活事件
frame.addWindowListener(WindowListener l)   //添加监听事件 传进一个 windowsListener 对象  WindowsListener为接口
  //WindowsAdapter 抽象类 实现了windowListener接口的方法
frame.addWindowListener(new WindowsAdapter(){   //匿名内部类 实现了WindowAdapter类
     @Override                               //重写了windowclosing 方法 
           public void windowClosing(WindowEvent e) {  
               super.windowClosing(e);
               System.exit(0);          //关闭窗口  
           }
})
按钮监听事件
通过 addactionListener 来添加按钮点击事件

addactionListener (ActionListener: alr) 调用该方法需要传进 ActionListener 类型的数据 ActionListener 为一个接口 需要创建类去实现 ActionListener 接口 并重写其中方法
public static void main(String[] args) {
        Button button = new Button("按钮-1");
        button.setActionCommand("button 1-1");  //给按钮1设置别名
        Button button1 = new Button("按钮-2");
        ActionListen actionListen = new ActionListen(); //该对象实现了 ActionListener接口  
        button.addActionListener(actionListen);   //需要传入一个 ActionListener类型 的参数  
        button1.addActionListener(actionListen);   //上面创建的ActionListen对象继承实现了该接口的方法
        Frame frame1 = new Frame("按钮程序");
        frame1.add(button,BorderLayout.WEST);//将按钮添加到窗口的西边
        frame1.add(button1,"East");//将按钮添加到窗口的东边
        frame1.setVisible(true);
    }
}
 //创建一个内部类继承ActrionListener接口   实现方法共用
class ActionListen implements ActionListener {  
    @Override
    public void actionPerformed(ActionEvent e) {  //实现ActionListener 的方法   
        System.out.println("我是=>:"+e.getActionCommand());//输出 获取到的按钮信息
    }
}
//先点击按钮-1  后点击按钮-2
控制台输出:
                        我是=>:button 1-1
                    我是=>:按钮-2
//如果进行setActionCommand操作 输出为设置文本  没有则输出按钮文本
监听事件 ActionListener 接口 actionPerformed 方法之 ActionEvent 获取
//ActionListener接口的实例是需要传入addactionListener监听事件的参数
public class ActionListen implements ActionListener {  
    @Override
    public void actionPerformed(ActionEvent e) {  //可以通过ActionEvent e来获取到被监听组件的一些行为
      e.getActionCommand()  //获取控件的labal或者别名  别名需要用set方法设置
         e.getSource()//获得一些资源 返回一个Object类型的对象;
    }
}
画笔模拟画图软件圆点功能
public class PaintTest2 {
    public static void main(String[] args) {                     //思路 监听鼠标 将每次鼠标点击的点储存在集合中
            new Mypaint("简易画图");                              //创建画笔 遍历集合 在这些点的坐标上画圆⚪
    }                                                            //每点击一次就遍历一次画一次
}
class Mypaint extends Frame{   //建一个内部类继承Frame  重写paint方法
              ArrayList list=null;      //建立一个集合用来存储鼠标点击时的坐标\
             public Mypaint(String title){//有参构造方法
                super(title);           //调用父类构造方法创建一个窗口
                setVisible(true);
                setBounds(300,300,500,500);
                list= new ArrayList();   //集合初始化
                addMouseListener(new myListen());//添加鼠标监听事件
                 addWindowListener(new WindowAdapter() {
                     @Override
                     public void windowClosing(WindowEvent e) {//关闭事件
                         super.windowClosing(e);
                     }
                 });
             }
                @Override
                public void paint(Graphics g) {   //画笔方法
                    Iterator it=list.iterator();   //迭代集合 将鼠标点击的坐标遍历出来 在坐标上画⚪
                   while (it.hasNext()){
                      Point point=(Point)it.next();
                      g.setColor(Color.blue);
                      g.fillOval(point.x,point.y,10,10);
                   }
                }
    private class myListen extends MouseAdapter{   //建立内部类继承鼠标点击事件适配器
         //鼠标按压产生的事件
        @Override
        public void mousePressed(MouseEvent e) {
            list.add(e.getPoint());  //获取鼠标点击时的点 添加到集合中
            repaint();           //重画  每点击一次就重画一次
        }
    }
}

                               
登录/注册后可看大图
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-8 03:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表