2021.4.1_JAVA_GUI学习

GUI编程

1.组件和容器

1.Frame

package com.kuang.lesson01;

import java.awt.*;

//GUI 第一个界面
public class TestFrame {
   public static void main(String[] args) {
       //Frame,JDK,查看源码
       Frame frame = new Frame("我的第一个JAVA图像界面窗口");
       //设置可见性
       frame.setVisible(true);
       //设置窗口大小
       frame.setSize(400,500);
       //设置背景颜色  Color
//        new Color()
       frame.setBackground(new Color(160, 35, 35));
       //初始位置
       frame.setLocation(200,200);
       //设置大小固定
       frame.setResizable(false);


   }
}

2.Frame+面向对象

package com.kuang.lesson01;

import java.awt.*;

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.white);
        MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.yellow);
        MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.pink);
    }
}
class MyFrame extends Frame {
    static int id = 0;
    public MyFrame(int x,int y,int w,int h,Color color){
        super("MyFrame+"+(++id));
        setBounds(x,y,w,h);
        setBackground(color);
        setVisible(true);
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nMALgncQ-1617284494865)(C:\Users\yy\Desktop\test.png)]

2.面板Panel

解决关闭事件

package com.kuang.lesson01;

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

//Panel 可以看成空间,不能单独存在
public class TestPanel {
    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(Color.blue);
        //设置坐标,相对于frame
        panel.setBounds(50,50,400,400);
        panel.setBackground(Color.yellow);
        frame.add(panel);
        frame.setVisible(true);

        //监听事件
        //适配器模式
        frame.addWindowListener(new WindowAdapter() {
            //窗口关闭需要做的事
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });
    }
}

3.布局管理器

  • 流式布局

    package com.kuang.lesson01;
    
    import java.awt.*;
    
    public class TestFlowLayout {
        public static void main(String[] args) {
            Frame frame = new Frame();
            //组件-按钮
            Button button1 = new Button("button_1");
            Button button2 = new Button("button_2");
            Button button3 = new Button("button_3");
    
            //设置为流式布局
            frame.setLayout(new FlowLayout(FlowLayout.LEFT));
            frame.setSize(500,500);
            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 East = new Button("East");
            Button West = new Button("West");
            Button South = new Button("South");
            Button North = new Button("North");
            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(200,200);
            frame.setVisible(true);
        }
    }
    
  • 表格布局

    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);
        }
    }
    
  • 练习

    package com.kuang.lesson01;
    
    import java.awt.*;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    public class ExDemo {
        public static void main(String[] args) {
            Frame frame = new Frame("ExDemo");
            frame.setVisible(true);
            frame.setSize(500,400);
            frame.setLayout(new GridLayout(2,1));
    
            //总
            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("btn"),BorderLayout.EAST);
            p1.add(new Button("btn"),BorderLayout.WEST);
            p2.add(new Button("btn"));
            p2.add(new Button("btn"));
            p1.add(p2,BorderLayout.CENTER);
            //下
            p3.add(new Button("btn"),BorderLayout.EAST);
            p3.add(new Button("btn"),BorderLayout.WEST);
            for (int i = 0; i < 4; i++) {
                p4.add(new Button("btn"));
            }
            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-1DTXnREc-1617284494869)(C:\Users\yy\Desktop\微信截图_20210401151320.png)]

  • 总结

    1.Frame是个顶级窗口

    2.Panel无法单独现实,必须添加到某个容器

    3.布局管理器

    ​ 1.流式

    ​ 2.东西南北中

    ​ 3.表格

    4.大小、定位、背景颜色、可见性、监听

4.事件监听

public class ActiveEvent {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button btn = new Button();
        //因为,addActionListener需要ActionListener,所以才写的
        MyActionListener myActionListener = new MyActionListener();
        btn.addActionListener(myActionListener);
        frame.add(btn,BorderLayout.CENTER);
        frame.pack();
        windowClose(frame);
        frame.setVisible(true);

    }
    //关闭窗口
    private static void windowClose(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("aaa");
    }
}
  • 多个按钮只写一个监听

    public class TestActionEvent {
        public static void main(String[] args) {
            //两个按钮,实现同一个监听
            //开始  停止
            Frame frame = new Frame();
            Button button1 = new Button("START");
            Button button2 = new Button("STOP");
            MyMonitor mymonitor = new MyMonitor();
            //可以显示定义返回的命令,如果不显示定义,则走默认的返回
            //可以多个按钮只写一个监听类
            button2.setActionCommand("button2-stop");
    
            button1.addActionListener(mymonitor);
            button2.addActionListener(mymonitor);
            frame.add(button1,BorderLayout.NORTH);
            frame.add(button2,BorderLayout.SOUTH);
    
            frame.pack();
            frame.setVisible(true);
        }
    }
    
    class MyMonitor implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            //e.getActionCommand()获得按钮信息
            System.out.println("按钮被点击了:msg:"+e.getActionCommand());
        }
    }
    

5.输入框TextField监听

  • 练习

    public class TestText01 {
        public static void main(String[] args) {
            //启动
            Frame frame = new MyFrame();
        }
    }
    class MyFrame extends Frame{
        public MyFrame(){
            TextField textField = new TextField();
            add(textField);
            //监听文本框输入的文字
            MyActionListener2 myActionListener2 = new MyActionListener2();
            //按下回车就会触发这个输入框的事件
            textField.addActionListener(myActionListener2);
            //设置替换编码
            textField.setEchoChar('*');
            setVisible(true);
            pack();
    
        }
    }
    
    class MyActionListener2 implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            TextField field = (TextField) e.getSource();//获得一些资源
            System.out.println(field.getText());//获得输入框中的文本
            field.setText("");
        }
    }
    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值