绊夏微凉

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

GUI-窗口

窗口的显示

public static void main(String[] args) {
        // 定义窗口
        Frame frame = new Frame();
        // 设置窗口大小
        frame.setSize(400, 400);
        // 设置窗口坐标
        frame.setLocation(200, 200);
        // 设置窗口颜色
        frame.setBackground(Color.BLACK);
        // 设置窗口显示
        frame.setVisible(true);

    }

窗口大小和窗口坐标一起设置

public static void main(String[] args) {
        // 定义窗口
        Frame frame = new Frame();
        // 设置窗口大小,坐标
        frame.setBounds(200, 200, 400, 400);
        // 设置窗口颜色
        frame.setBackground(Color.BLACK);
        // 设置窗口显示
        frame.setVisible(true);
    }

窗口的关闭

​ 点击窗口的X无法关闭,得自定义窗口监听按钮,对窗口进行关闭

public static void main(String[] args) {
        // 定义窗口
        Frame frame = new Frame();
        // 设置窗口大小,坐标
        frame.setBounds(200, 200, 400, 400);
        // 设置窗口颜色
        frame.setBackground(Color.BLACK);
        // 关闭窗口
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                // 终止程序
                System.exit(0);
            }
        });
        // 设置窗口显示
        frame.setVisible(true);
    }
posted on 2021-04-13 11:39  绊夏微凉  阅读(72)  评论(0)    收藏  举报