锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

JavaGUI编程

时间:2023-09-04 05:07:01 hdw306微型拉压传感器

JavaGUI编程

组件:

1. 简介

GUI的核心技术:Swing AWT(现在GUI不常用,但有些思想还是可以学的)

  1. 因为界面不美观
  2. 需要jre环境

为什么要学习?

  1. 你可以在心里写下你想要的小工具
  2. 了解MVC结构,了解监听!

2. AWT

2.1 AWT介绍

new 类!

  1. 包含多种类和接口!GUI:图像用户界面
  2. 元素:窗口、按钮、文本框
  3. java.awt包下

[外链图片转存失败,源站可能有防盗链机制,建议保存图片并直接上传(img-jBNPsUDD-1649143567131)(C:\Users\lu’hai’peng\AppData\Roaming\Typora\typora-user-images\image-20220329212103824.png)]

2.2 组件和容器

2.2.1 Frame

import java.awt.*;  public class testGUI { 
             public static void main(String[] args) { 
                 //Frame ,jdk,看源码         Frame frame = new Frame("我的第一个窗口");          //需要设置可见性         frame.setVisible(true);          //需要设置窗口大小         frame.setSize(400,400);          //设置背景色 Color         frame.setBackground(new Color(85,150,68));         //或者frame。setBackground(Color.black)          ///弹出的初始位置         frame.setLocation(200,200);          //固定设置尺寸         frame.setResizable(false);      } } 

///发现窗口无法关闭,需要终止程序

[外链图片转存失败,源站可能有防盗链机制,建议保存图片并直接上传(img-PSZS9HdW-1649143567136)(C:\Users\lu’hai’peng\AppData\Roaming\Typora\typora-user-images\image-20220329213611639.png)]

展示窗口strog>:

public class testGUI2 { 
         
    public static void main(String[] args) { 
         
        //展示多个窗口,需要自己实现一个类,new出来
        Frame m1=new MYFrame(100,100,300,300,Color.black);
        Frame m2=new MYFrame(100,400,300,300,Color.blue);
        Frame m3=new MYFrame(100,700,300,300,Color.green);

    }
}
class MYFrame extends Frame{ 
         
    static int cnt=0;//可能存在多个窗口,许雅有一个计数器

    public MYFrame(int x,int y,int w,int h,Color color){ 
         //位置坐标x,y,宽高w,h,颜色color(构造函数)
        super("MyFrame"+(++cnt));//重写父方法
        //由于是继承,可以直接调用父类的函数
        setBackground(color);
        setBounds(x,y,w,h);
        /* 相当于setLocation(x,y) + setSize(w,h) */
        setVisible(true);

    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9Y15T54g-1649143567139)(C:\Users\lu’hai’peng\AppData\Roaming\Typora\typora-user-images\image-20220331162711441.png)]

2.2.2 面板 Panel

Panel可以看成是一个空间,但是不能单独存在,内嵌在Frame里面

//这里解决了关闭事件

public class testGUI { 
         
    //Panel可以看成是一个空间,但不能单独存在,需要嵌套在Frame里面
    public static void main(String[] args) { 
         
        Frame frame = new Frame();
        Panel panel = new Panel();
        //设置布局
        frame.setLayout(null);

        //坐标
        frame.setBounds(300,300,500,500);
        frame.setBackground(new Color(153, 34, 19));


        //panel设置坐标,相对于frame
        panel.setBounds(50,50,400,400);
        panel.setBackground(Color.green);

        //frame.add(panle)
        frame.add(panel);//将面板加入frame里面

        frame.setVisible(true);

        //监听事件,监听窗口关闭,System.exit(0)
        //适配器模式,这样就只要重写一种方法了
        frame.addWindowListener(new WindowAdapter() { 
         
            @Override
            public void windowClosing(WindowEvent e) { 
         
                System.exit(0);
            }
        });

    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-P2NoRbEg-1649143567142)(C:\Users\lu’hai’peng\AppData\Roaming\Typora\typora-user-images\image-20220331165602254.png)]

2.3 布局管理器

  • 流式布局
  • 东西南北中
  • 表格布局Grid

流式布局

public class testGUI { 
         
    //Panel可以看成是一个空间,但不能单独存在,需要嵌套在Frame里面
    public static void main(String[] args) { 
         
        Frame frame = new Frame();

        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");

        //设置流式布局
        frame.setLayout(new FlowLayout());//默认为中间
// frame.setLayout(new FlowLayout(FlowLayout.LEFT));//从左边开始,一排满了就下一排
        
        frame.setSize(400,400);
        //把按钮添加上去
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

        frame.setVisible(true);
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dydHXRyJ-1649143567145)(C:\Users\lu’hai’peng\AppData\Roaming\Typora\typora-user-images\image-20220401161958641.png)]

东西南北中

public class testGUI { 
         
    //Panel可以看成是一个空间,但不能单独存在,需要嵌套在Frame里面
    public static void main(String[] args) { 
         
        Frame frame = new Frame();

        Button west = new Button("west");
        Button east = new Button("east");
        Button north = new Button("north");
        Button south = new Button("south");
        Button center = new Button("center");

        frame.add(east,BorderLayout.EAST);
        frame.add(west,BorderLayout.WEST);
        frame.add(south,BorderLayout.SOUTH);
        frame.add(north,BorderLayout.NORTH);
        frame.add(center,BorderLayout.CENTER);

        frame.setSize(400,400);
        frame.setVisible(true);
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kGunlnwR-1649143567147)(C:\Users\lu’hai’peng\AppData\Roaming\Typora\typora-user-images\image-20220401165934743.png)]

表格布局Grid

public class testGUI { 
         
    //Panel可以看成是一个空间,但不能单独存在,需要嵌套在Frame里面
    public static void main(String[] args) { 
         
        Frame frame = new Frame();

        Button bnt1 = new Button("bnt1");
        Button bnt2 = new Button("bnt2");
        Button bnt3 = new Button("bnt3");
        Button bnt4 = new Button("bnt4");
        Button bnt5 = new Button("bnt5");
        Button bnt6 = new Button("bnt6");

        frame.setLayout(new GridLayout(2,3));//两行三列的报个布局

        frame.add(bnt1);
        frame.add(bnt2);
        frame.add(bnt3);
        frame.add(bnt4);
        frame.add(bnt5);
        frame.add(bnt6);

        frame.pack();//Java函数,用于自动布局,自动写大小等
        frame.setVisible(true);
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8LLWQWvm-1649143567149)(C:\Users\lu’hai’peng\AppData\Roaming\Typora\typora-user-images\image-20220401170750673.png)]

练习

public class testGUI { 
         
    //Panel可以看成是一个空间,但不能单独存在,需要嵌套在Frame里面
    public static void main(String[] args) { 
         
        Frame frame = new Frame();

        frame.setVisible(true);
        frame.setSize(400,300);
        frame.setLocation(500,500);
        frame.setLayout(new GridLayout(2,1));
        frame.setBackground(Color.black);



        //四个面版
        Panel p1 = new Panel(new BorderLayout());
        Panel p2 = new Panel(new GridLayout(2,1));
        Panel p3 = new Panel(new BorderLayout());
        Panel p4 = new Panel(new GridLayout(2,2));


        p1.add(new Button("east1"),BorderLayout.EAST);
        p1.add(new Button("west1"),BorderLayout.WEST);

        p2.add(new Button("b1"));
        p2.add(new Button("b2"));

        p1.add(p2,BorderLayout.CENTER);

        p3.add(new Button("east2"),BorderLayout.EAST);
        p3.add(new Button("west2"),BorderLayout.WEST);

        p4.add(new Button("1"));
        p4.add(new Button("2"));
        p4.add(new Button("3"));
        p4.add(new Button("4"));

        p3.add(p4,BorderLayout.CENTER);


        frame.add(p1);
        frame.add(p3);
        
        frame.addWindowListener(new WindowAdapter() { 
         
            @Override
            public void windowClosing(WindowEvent e) { 
         
                System.exit(0);
            }
        });

    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oAR1s6tF-1649143567151)(C:\Users\lu’hai’peng\AppData\Roaming\Typora\typora-user-images\image-20220401202456249.png)]

*总结

  1. Frame是一个顶级窗口
  2. Panel无法单独显示,必须添加到某一个容器中
  3. 布局管理器
    1. 流式
    2. 东西南北中
    3. 表格
  4. 大小,定位,背景颜色,可见性,监听!

2.4 事件监听

事件监听:当某个事情发生的时候,干什么?

例1

public class testGUI { 
         
    public static void main(String[] args) { 
         
        //按下按钮,触发一些事件
        Frame frame = new Frame();
        Button button = new Button();

        //因为addActionListener()需要一个AddActionListener,所以我们需要构造一个AddActionListener
        myactionListener mal = new myactionListener();
        button.addActionListener(mal);

        frame.add(button,BorderLayout.CENTER);
        frame.setVisible(true);
        frame.pack();
        myClos(frame);


    }
    public static void myClos(Frame frame){ 
         
        frame.addWindowListener(new WindowAdapter() { 
         
            @Override
            public void windowClosing(WindowEvent e) { 
         
                System.exit(0);
            }
        });
    }
}

//事件监听
class myactionListener implements ActionListener{ 
         
    @Override
    public void actionPerformed(ActionEvent e) { 
         
        System.out.println("aaaaa");
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xMrYWddB-1649143567152)(C:\Users\lu’hai’peng\AppData\Roaming\Typora\typora-user-images\image-20220401204546556.png)]

例2 多个按钮共享一个事件

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.BatchUpdateException;

public class testGUI { 
         
    public static void main(String[] args) { 
         
       //写一个开始和结束的button
        Frame frame = new Frame("start-end");

        Button b1 = new Button("start");
        Button b2 = new Button("end");

        //可以显示地定义触发时会返回的命令信息,如果不显示定义,则会走默认值
        //可以多个按钮只写一个监听类
        //setActionCommand()显示设置按钮信息
        b2.setActionCommand("b2-is-click");

        myListener ml = new myListener();

        b1.addActionListener(ml);
        b2.addActionListener(ml);

        frame.setLayout(new GridLayout(2,1));
        frame.add(b1);
        frame.add(b2);

        frame.pack();
        frame.setVisible(true);

    }
}

//事件监听
class myListener implements ActionListener{ 
         
    @Override
    public void actionPerformed(ActionEvent e) { 
         
        //getActionCommand()获取按钮信息
        System.out.println("it is cliscked "+e.getActionCommand());
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bXEwp9zJ-1649143567154)(C:\Users\lu’hai’peng\AppData\Roaming\Typora\typora-user-images\image-20220401205632879.png)]

2.5 输入框TextField监听

public class testGUI { 
         
    public static void main(String[] args) { 
         
        MyFrame myFrame = new MyFrame();
    }
}
class MyFrame extends Frame{ 
         


    public MyFrame(){ 
         
        TextField textfield = new TextField();
        add(textfield);//在frame中添加textfiled

        //监听这个文本框输入的文字
        MyActionLisener myActionLisener = new MyActionLisener();
        //按下enter,就会触发这个输入框的事件
        textfield.addActionListener(myActionLisener);

        //设置替换编码,比如输入密码显示的是*
        textfield.setEchoChar('*');

       setVisible(true);
       pack();

    }
}
class MyActionLisener implements ActionListener{ 
         
    @Override
    public void actionPerfor

相关文章