GUI

GUI编程

## 1.简介

GUI的核心技术: Swing AWT

​ 为什么不流行? 因为不美观 . 需要JRE环境

2.AWT

2.1 AWT介绍

​ 1.包含了很多类和接口. GUI: 图形用户界面编程

​ 2.元素:窗口,按钮,文本框

​ 3.java.awt

3.组件和容器

1.Frame

Frame frame = new Frame("我的第一个Java图形窗口");

//设置可见性 w  h
frame.setVisible(true);

//设置窗口大小
frame.setSize(400,400);

//设置颜色
frame.setBackground(new Color(127, 53, 53));

//弹出的初始位置
frame.setLocation(200,200);

//设置大小固定
frame.setResizable(false);

BUG: 发现窗口关闭不掉,停止Java程序运行。

设置多个窗口

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.YELLOW);
        MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.WHITE);
        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));
        setBackground(color);
        setBounds(x, y, w, h);
        setVisible(true);

    }

}

2.面板

//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.YELLOW);

        //pannel也可以设置坐标,相对于frame
        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(0,0,0));

        //继承Container又继承component
        frame.add(panel);
        frame.setVisible(true);
        //监听事件,监听窗口关闭事件 System.exit(0)
        //适配器模式
        frame.addWindowListener(new WindowAdapter() {
            @Override   //点击关闭的时候做的事情
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
    }
}

3.布局管理器

​ 流式布局

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.RIGHT));
    frame.setSize(200,200);

    frame.add(button1);
    frame.add(button2);
    frame.add(button3);

    frame.setVisible(true);

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

​ 东西南北中 BorderLayout

public static void main(String[] args) {
    Frame frame = new Frame("TestBorderLayout");
    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);

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

}

​ 表格布局 Grid

public static void main(String[] args) {
    Frame frame = new Frame();
    Button button1 = new Button("btn1");
    Button button2 = new Button("btn2");
    Button button3 = new Button("btn3");
    Button button4 = new Button("btn4");
    Button button5 = new Button("btn5");
    Button button6 = new Button("btn6");

    frame.setLayout(new GridLayout(3,2));

    frame.add(button1);
    frame.add(button2);
    frame.add(button3);
    frame.add(button4);
    frame.add(button5);
    frame.add(button6);

    frame.pack();//Java函数,作用是自动选择一个最优秀的布局
    frame.setVisible(true);

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

}

总结:

​ 1.Frame 是一个顶级窗口

​ 2.panel 无法单独显示,必须添加到某个容器中

​ 3.布局管理器 1.流式 2.东西南北中 3.表格

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

4.事件监听

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

public class TestActionEvent {

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

    public static void main(String[] args) {
        Frame frame = new Frame();
        Button button = new Button();

        MyActionListener myActionListener = new MyActionListener();

        button.addActionListener(myActionListener);

        frame.add(button,BorderLayout.CENTER);

        frame.pack();

        windowsClose(frame);

        frame.setVisible(true);


    }
}

//事件监听
class MyActionListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("aaa");
    }
}

组合+内部类回顾复习

​ OOP原则:组合,大于继承

//简单的加法计算器
public class TestCalc {
    public static void main(String[] args) {
        new Calculator().loadFrame();
    }
}

//计算器类
class Calculator extends Frame{

    TextField num1,num2,num3;

    public void loadFrame(){
        //3个文本框
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        //1个按钮
        Button button = new Button("=");
        button.addActionListener(new MyCalculatorListener(this));
        //1个标签
        Label label = new Label("+");
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });

        pack();
        setVisible(true);
    }

    public Calculator() throws HeadlessException {
    }
}

//监听器类

class MyCalculatorListener implements ActionListener{

    Calculator calculator = null;

    //获取三个变量
    public MyCalculatorListener(Calculator calculator){
        this.calculator = calculator;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //1.获得加数和被加数
        int n1 = Integer.parseInt(calculator.num1.getText());
        int n2 = Integer.parseInt(calculator.num2.getText());
        //2.将这个值加法运算后放到第三个框
        calculator.num3.setText(""+(n1+n2));
        //3.清楚前两个框框
        calculator.num1.setText("");
        calculator.num2.setText("");
    }
}

也可以把MyCalculatorListener包装成内部类:更好的包装。畅通无阻的访问外部的属性和方法

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值