• JAVA学习day05_GUI编程


    前言

    这个专题学习的是JAVA有关GUI的编程,包括Swing函数的一些使用,因为在学校的工程实践已经学过并写过,所以本专题不会很详尽地介绍,只是记录个人觉得需要重视的地方

    一、AWT

    1、Awt介绍

    基本结构

    Frame:窗口
    Dialog:弹窗
    Applet:小程序

    2、组件和容器

    窗口Frame

    1. 单个窗口
    //GUI的第一个界面
    public class TestFrame {
        public static void main(String[] args) {
    
            //Frame
            Frame frame = new Frame("我的第四个Java图像界面窗口");
    
            //设置可见性  w h
            frame.setVisible(true);
    
            //设置窗口大小
            frame.setSize(400,400);
    
            //设置背景颜色
            frame.setBackground(new Color(0,0,0));
    
            //弹出的初始位置
            frame.setLocation(200,200);
    
            //设置大小固定
            frame.setResizable(false);
        }
    }
    
    1. 多个窗口
    public class TestFrame2 {
        public static void main(String[] args) {
            MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.blue);
            MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.green);
            MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.yellow);
            MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.red);
        }
    }
    
    class MyFrame extends Frame {
        static int id = 0;//多个窗口需要计数器
    
        public MyFrame(int x,int y,int w,int h,Color color){
            super("MyFrame"+(++id));
            setBackground(color);
            setBounds(x,y,w,h);//等于location和size一起设置
            setVisible(true);
        }
    }
    
    

    继承Frame,用super构造Frame

    面板Panel

    关闭事件

    public class TestPenal {
        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(40,161,35));
    
            //panel设置坐标,相对与frame
            panel.setBounds(50,50,400,400);
            panel.setBackground(new Color(193,15,60));
    
            //frame.add(panel)
            frame.add(panel);
            frame.setVisible(true);
    
            //监听事件,监听窗口关闭时间 System.exit(0)
            //适配器模式(23种模式)
            frame.addWindowListener(new WindowAdapter() {
                //窗口点击关闭时要做的事情
                @Override
                public void windowClosing(WindowEvent e) {
                    //结束程序
                    System.exit(0);
                }
            });
        }
    }
    

    3、布局管理器

    • 流式布局

    分成中间左边和右边,CENTER,LEFT,RIGHT

    public class TestFlowLayout {
        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");
    
            //设置为流式布局
            //CENTER是中间,还有L和R
            frame.setLayout(new FlowLayout(FlowLayout.LEFT));
    
            frame.setSize(200,200);
    
            //添加按钮
            frame.add(button1);
            frame.add(button2);
            frame.add(button3);
    
            frame.setVisible(true);
    
        }
    }
    
    

    流式布局

    • 东西南北中
    public class TestBorderLayout {
        public static void main(String[] args) {
            Frame frame = new Frame("TestBorderLayout");
    
            Button e = new Button("East");
            Button w = new Button("West");
            Button s = new Button("South");
            Button n = new Button("North");
            Button c = new Button("Center");
    
            frame.add(e,BorderLayout.EAST);
            frame.add(w,BorderLayout.WEST);
            frame.add(s,BorderLayout.SOUTH);
            frame.add(n,BorderLayout.NORTH);
            frame.add(c,BorderLayout.CENTER);
    
            frame.setSize(200,200);
            frame.setVisible(true);
    
            frame.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        }
    }
    
    

    东西南北中

    • 表格布局
    public class TestGridLayout {
        public static void main(String[] args) {
            Frame frame = new Frame("TestGridLayout");
    
            Button btn1 = new Button("btn1");
            Button btn2 = new Button("btn2");
            Button btn3 = new Button("btn3");
            Button btn4 = new Button("btn4");
            Button btn5 = new Button("btn5");
            Button btn6 = new Button("btn6");
    
            frame.setLayout(new GridLayout(3,2));
    
            frame.add(btn1);
            frame.add(btn2);
            frame.add(btn3);
            frame.add(btn4);
            frame.add(btn5);
            frame.add(btn6);
    
            frame.pack();//java函数,自动选择最优布局
            frame.setVisible(true);
        }
    }
    

    小作业

    public class TestHomeWork {
        public static void main(String[] args) {
            //懒得添加btn
            //another one
            Frame frame2 = new Frame("another one");
    
            Panel panel1 = new Panel(new GridLayout(2, 1));
            panel1.add(new Button(),0);
            panel1.add(new Button(),1);
    
            Panel panel4 = new Panel(new GridLayout(2, 2));
            panel4.add(new Button(),0);
            panel4.add(new Button(),1);
            panel4.add(new Button(),2);
            panel4.add(new Button(),3);
    
            Panel panel0 = new Panel(new GridLayout(2, 3));
            panel0.add(new Button(),0);
            panel0.add(panel1,1);
            panel0.add(new Button(),2);
            panel0.add(new Button(),3);
            panel0.add(panel4,4);
            panel0.add(new Button(),5);
    
            frame2.add(panel0);
            frame2.pack();
            frame2.setVisible(true);
    
            frame2.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        }
    }
    
    

    总共三个panel对象,都采用表格布局,第一个两行三列总布局;第二个两行一列;第三个两行两列。流动布局只能选中一种,类似对齐方式,靠左靠右居中。
    写前仔细想想写得就又快又好,开始想错了,用了7个panel属于是笨比了属于是。

  • 相关阅读:
    c#配置文件
    C#预处理指令
    C#面向对象详解
    231. Power of Two
    226. Invert Binary Tree
    C语言函数入参压栈顺序为什么是从右向左?
    对C++ 虚函数的理解
    悲观锁和乐观锁
    什么是索引
    CHAR 和VARCHAR的区别
  • 原文地址:https://www.cnblogs.com/ebym/p/15668566.html
Copyright © 2020-2023  润新知