AWT/Swing

作者: 帅哥_刷哥 | 来源:发表于2017-10-10 23:44 被阅读8次

    1.AWT包

    awt包

    2.AWT体系

    awt体系
    awt体系

    3. 创建Frame容器

    3.1创建Frame基础容器

    //第一种写法
    public static void main(String[] args) {
        //1.创建一个容器
        Frame frame = new Frame();
        //2.设置容器的宽度/高度
        frame.setSize(300, 300);
        //3.设置容器在屏幕中的位置(x,y)
        frame.setLocation(100, 100);
        //4.设置容器展示
        frame.setVisible(true);
    }
    
    //第二种写法
    public static void main(String[] args) {
        //1.创建一个容器
        Frame frame = new Frame();
        //2.设置容器的宽度/高度和容器在屏幕中的位置
        frame.setBounds(100, 100, 300, 300);
        //3.设置容器展示
        frame.setVisible(true);
    }
    
    上述代码展示的界面

    3.2设置Frame标题

    //第一种写法
    public static void main(String[] args) {
        //1.创建一个容器-并且添加标题
        Frame frame = new Frame("这个一个窗口");
        //2.设置容器的宽度/高度
        frame.setSize(300, 300);
        //3.设置容器在屏幕中的位置(x,y)
        frame.setLocation(100, 100);
        //4.设置容器展示
        frame.setVisible(true);
    }
    
    //第二种写法
    public static void main(String[] args) {
        //1.创建一个容器
        Frame frame = new Frame();
        //2.设置标题
        frame.setTitle("这个一个窗口");
        //3.设置容器的宽度/高度
        frame.setSize(300, 300);
        //4.设置容器在屏幕中的位置(x,y)
        frame.setLocation(100, 100);
        //5.设置容器展示
        frame.setVisible(true);
    }
    
    上述代码展示

    3.3设置Frame的关闭按钮

    public static void main(String[] args) {
        //1.创建一个容器
        final Frame frame = new Frame();
        //2.设置容器的宽度/高度和容器在屏幕中的位置
        frame.setBounds(100, 100, 300, 300);
        //3.设置容器展示
        frame.setVisible(true);
        //4.设置窗口关闭
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                //这种方式不可取
                //System.exit(0);
                //调用此方法来关闭窗口
                frame.dispose();
            }
        });
    }
    

    4.创建Dialog容器

    4.1创建Dialog基础容器

    public static void main(String[] args) {
        //1.创建一个Frame容器
        Frame frame = createFrame();
        //创建一个Dialog容器
        final Dialog dialog = new Dialog(frame);
        dialog.setTitle("这个是一个弹出框");
        dialog.setBounds(100, 100, 100, 100);
        dialog.setVisible(true);
        dialog.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                dialog.dispose();
            }
        });
    }
    

    相关文章

      网友评论

          本文标题:AWT/Swing

          本文链接:https://www.haomeiwen.com/subject/gbsssxtx.html