千余千与

GUI第一个界面

第一个GUI界面

import java.awt.*;
//GUI第一个界面
public class TestFrame {
    public static void main(String[] args) {
        Frame frame = new Frame("我的第一个java图形界面");
        //设置可见性
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(400, 400);
        //设置背景颜色
        frame.setBackground(new Color(5, 116, 86, 0) );
        //弹出框的初始位置
        frame.setLocation(400, 400);
        //设置大小固定
        frame.setResizable(false);
    }
}

GUI展示第一个界面

展示多个界面

import java.awt.*;

public class TestFrame2 {
    public static void main(String[] args) {
        //展示多个窗口
        MyFrame myFrame = new MyFrame(400, 300, 200, 200, Color.cyan);
        MyFrame myFrame2 = new MyFrame(600, 300, 200, 200, Color.blue);
        MyFrame myFrame3 = new MyFrame(400, 500, 200, 200, Color.yellow);
        MyFrame myFrame4 = new MyFrame(600, 500, 200, 200, Color.green);
    }
}

class MyFrame extends Frame{//继承父类

    static int id=0;//存在多个窗口 我们需要一个计数器
    public  MyFrame(int x, int y,int w,int h,Color color){
        //super调用父类
        super("MyFrame+"+(++id));
        //设置背景颜色
        setBackground(color);
        //设置坐标
        setBounds(x, y, w, h);
        //设置可见性
        setVisible(true);

    }
}

GUI展示多个界面

面板Panel

监听窗口关闭事件,点击关闭窗口结束程序

代码:
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

//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(new Color(0xEE5C86));
        //Panel设置坐标,相对于Frame
        panel.setBounds(50, 50, 400, 400);
        panel.setBackground(new Color(0x83C997));
        //
        frame.add(panel);
        frame.setVisible(true);
        //监听事件,监听窗口关闭事件 System.exit(0)
        frame.addWindowListener(new WindowAdapter() {
            //窗口点击关闭需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);

            }
        });
    }
}
效果图

GUI3

posted on 2021-02-06 11:30  千余千与  阅读(58)  评论(0编辑  收藏  举报

导航