Java开发:GUI编程

 本文针对GUI编程进行部分简述,实际开发工作中并不常用。

目录

一、GUI简介

二、AWT

A、AWT介绍

B、Frame

C、Panel面板

D、布局管理器


一、GUI简介

GUI(Graphical User Interface)即图形用户界面,它能够使应用程序看上去更加友好。

GUI的核心技术:Swing、AWT

GUI不流行的原因:1、因为界面不美观;2、需要jre环境;

二、AWT

A、AWT介绍

AWT(Abstract Window Tool)即抽象的窗口工具,包含了很多类和接口。

B、Frame

直接上代码

public class Application {
    public static void main(String[] args) {
        createWindow();
    }

    private static void createWindow() {
        Frame frame = new Frame("Java图形界面-王硕磊");
        //需要设置可见性
        frame.setVisible(true);
        //设置窗口的宽高
        frame.setSize(900, 800);
        //设置窗口背景颜色
        frame.setBackground(Color.LIGHT_GRAY);
        //设置窗口默认显示的位置
        frame.setLocation(800, 200);
        //设置窗口不可改变大小
        frame.setResizable(false);
    }
}

C、Panel面板

public class Application {
    public static void main(String[] args) {
        createWindow();
    }

    private static void createWindow() {
        Frame frame = new Frame();
        Panel panel = new Panel();

        //设置布局
        frame.setLayout(null);
        //设置坐标
        frame.setBounds(300, 300, 500, 500);
        frame.setBackground(Color.LIGHT_GRAY);

        //panel设置坐标,相对于frame的相对位置
        panel.setBounds(50, 50, 400, 400);
        panel.setBackground(new Color(115, 156, 50));

        frame.add(panel);
        frame.setVisible(true);

        //监听窗口关闭事件
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                //窗口点击关闭的时候要做的事情
                super.windowClosing(e);
                System.exit(0);
            }
        });
    }
}

D、布局管理器

1、流式布局

public class Application {
    public static void main(String[] args) {
        createWindow();
    }

    private static void createWindow() {
        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(400, 400);
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

        frame.setVisible(true);
    }
}

2、东西南北中

3、表格布局

end

猜你喜欢

转载自blog.csdn.net/android157/article/details/127934561