GUI基础

GUI编程

组件(Component)

  • 窗口(Frame)

  • 弹窗(Dialog)

  • 面板(Panel)

  • 文本框(TextArea)

  • 列表框

  • 按钮(button)

  • 图片

  • 监听事件

  • 鼠标

  • 键盘事件

  • 破解工具

简介

Gui的核心技术:Swing AWT

1.因为界面不美观

2.需要jre环境

MVC基础

可以写出自己心中想要的一些小工具

可能要维护到swing界面,概率极小

了解MVC架构,了解监听

AWT

包含了很多的类和接口! GUI:图型用户界面

元素:窗口,按钮,文本框

java.awt

// GUI的第一个界面
public class Gui {
    public static void main(String[] args) {
        //Frame
        Frame frame =new Frame("我的第一个java图像界面窗口");
        //需要设置可见性
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(400,400);
        //设置背景颜色 Color
        frame.setBackground(new Color(86, 229, 197));
        //弹出的初始位置
        frame.setLocation(200,200);
        //设置大小固定
        frame.setResizable(false);
    }

}

//开启多个窗口
public class GUI01 {
    public static void main(String[] args) {
        //展示多个窗口new
        MyFrame frame1 = new MyFrame(100,100,200,200,Color.blue);
        MyFrame frame2 = new MyFrame(300,100,200,200,Color.blue);
        MyFrame frame3 = new MyFrame(100,300,200,200,Color.blue);
        MyFrame frame4 = new MyFrame(300,300,200,200,Color.blue);
    }
    static 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);
            setVisible(true);
            setBounds(x,y,w,h);
        }
    }
}

监视器

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

//Panel 可以看成是一个空间,但是不能单独存在
public class TestPanle {
    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(124, 250, 0));
        //panel设置,相对于Frame
        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(0xFF0016));
        //frame.add()
        frame.add(panel);
        frame.setVisible(true);

        //监听事件,窗口关闭 System.exit(0)
        //适配器模式:
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        }

        );

    }
}

布局管理器

import java.awt.*;

public class GUI02 {
    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(FlowLayout.LEFT));
        frame.setSize(200,200);
        //把按钮添加上去
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.setVisible(true);
    }
}

东西南北中

import java.awt.*;

public class TestBorderlayout {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button button1= new Button("East");
        Button button2= new Button("West");
        Button button3= new Button("South");
        Button button4= new Button("North");
        Button button5= new Button("Center");
        frame.add(button1,BorderLayout.EAST);
        frame.add(button2,BorderLayout.WEST);
        frame.add(button3,BorderLayout.SOUTH);
        frame.add(button4,BorderLayout.NORTH);
        frame.add(button5,BorderLayout.CENTER);
        frame.setSize(200,200);
        frame.setVisible(true);

    }
}

表格布局 Grid

frame.setLayout(new GridLayout(3,2)); 三行两列

构思80% 代码20%

import java.awt.*;
import java.lang.management.BufferPoolMXBean;

public class work01 {
    public static void main(String[] args) {
        Frame frame = new Frame("work01");
        frame.setLayout((new GridLayout(2,1)));
        Button button1 = new Button("Button1");
        Button button2 = new Button("Button2");
        Button button3 = new Button("Button3");
        Button button4 = new Button("Button4");
        Button button5 = new Button("Button5");
        Button button6 = new Button("Button6");
        Button button7 = new Button("Button7");
        Button button8 = new Button("Button8");
        Button button9 = new Button("Button9");
        Button button10 = new Button("Button10");
        Panel panel1 = new Panel(new BorderLayout());
        Panel panel2 = new Panel(new GridLayout(2,2));
        Panel panel3 = new Panel(new BorderLayout());
        Panel panel4 = new Panel(new GridLayout(2,2));
        panel1.add(button1,BorderLayout.WEST);
        panel1.add(button2,BorderLayout.EAST);
        panel2.add(button3);
        panel2.add(button4);
        panel1.add(panel2,BorderLayout.CENTER);
        panel3.add(button5,BorderLayout.EAST);
        panel3.add(button6,BorderLayout.WEST);
        panel4.add(button7);
        panel4.add(button8);
        panel4.add(button9);
        panel4.add(button10);
        panel3.add(panel4,BorderLayout.CENTER);
        frame.add(panel1);
        frame.add(panel3);
        frame.setSize(500,500);
        frame.setVisible(true);
        

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值