Frame,Panel和三种布局管理器

窗体Frame

举例:
单个窗体

 //frame窗体存在在内存中,看不见
        Frame frame = new Frame("我的JAVA窗体");

        //设置窗体的可见性
        frame.setVisible(true);

        //设置窗体的尺寸
        frame.setSize(400,400);

        //设置窗体颜色
        frame.setBackground(Color.BLACK);
        //自定义颜色
        //frame.setBackground(new Color(1,1,1));

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

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

运行结果为:
窗体结果
多个窗体,运用封装,MyFrame这个类继承Frame

public static void main(String[] args) {
        //展示多个窗口
        MYFrame myFrame1 = new MYFrame(100, 100, 200, 200,Color.WHITE);
        MYFrame myFrame2 = new MYFrame(300, 100, 200, 200,Color.BLUE);
        MYFrame myFrame3 = new MYFrame(100, 300, 200, 200,Color.CYAN);
        MYFrame myFrame4 = new MYFrame(300, 300, 200, 200,Color.darkGray);

    }
}
class MYFrame extends Frame{
    //可能存在多个窗体,加上一个计数器
    static int id=0;

    //int x,int y表示初始化位置,int w,int h表示窗体大小
    public MYFrame(int x,int y,int w,int h,Color color){

        //调用父类,表示窗体的title,每调用一次ID自增一
        super("MYFrame"+(++id));
        //setBounds相当于setSize+setLocation
        setBounds(x,y,w,h);
        setBackground(color);
        setVisible(true);
    }
        }

运行结果为:
多个窗体运行结果

面板Panel

  1. Panel可以看成一个空间,但不能单独存在,panel需要放在frame中使用,Panel的坐标是相对于Frame的坐标
  2. frame.setLayout(null);设置默认布局,要不然会存放在左上角
  3. 最后添加窗体的可见性frame.setVisible(true);
    举例:
 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(101, 41, 131));

        //panel 设置坐标,相对于Frame,相对坐标
        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(181, 48, 200));

        //在窗体上添加面板
        frame.add(panel);

        frame.setVisible(true);
    }

}

运行结果:
Panel
关闭窗体:监听事件,会出现许多不必要的方法

frame.addWindowListener(new WindowListener()

这里需要适配器方法

frame.addWindowListener(new WindowAdapter() {
            //窗口点击关闭时,需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });

布局管理器

流式布局FlowLayout
举例:

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");
        Button button4 = new Button("button4");

        //设置为流式布局,默认在中间位置
        frame.setLayout(new FlowLayout());
        //4个Button靠左
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        
        frame.setSize(300,300);
        //添加到窗体上
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);

        frame.setVisible(true);
    }
}

运行结果:
Flow Layout

东西南北中
举例:

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

        Button East = new Button("East");
        Button West = new Button("West");
        Button South = new Button("South");
        Button North = new Button("North");
        Button Center = new Button("Center");

        frame.add(East,BorderLayout.EAST);
        frame.add(West,BorderLayout.WEST);
        frame.add(South,BorderLayout.SOUTH);
        frame.add(North,BorderLayout.NORTH);
        frame.add(Center,BorderLayout.CENTER);

        frame.setSize(500,500);
        frame.setVisible(true);
    }
}

运行结果为:
东西南北中
表格布局
举例:

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

        Button bnt1 = new Button("bnt1");
        Button bnt2 = new Button("bnt2");
        Button bnt3 = new Button("bnt3");
        Button bnt4 = new Button("bnt4");
        Button bnt5 = new Button("bnt5");
        Button bnt6 = new Button("bnt6");

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

        frame.add(bnt1);
        frame.add(bnt2);
        frame.add(bnt3);
        frame.add(bnt4);
        frame.add(bnt5);
        frame.add(bnt6);

        frame.pack();//按照窗体大小自动填充
        frame.setVisible(true);

    }
}

运行结果:
表格布局
嵌套布局

思路:总体是一个两行一列的表格布局,在两个表格上加两个面板分别为P1和p3,P1的面板上利用BorderLayout添加两个Button放在左右两侧,C位在放一个面板p2,p2上在放上两个Button。同理在p3面板上利用BorderLayout放上两个Button,分别在左右两侧,C位放一个p4面板,利用for循环添加四个Button

举例:

public static void main(String[] args) {
		//窗体总体布局
        Frame frame = new Frame();
        frame.setVisible(true);
        frame.setSize(400,300);
        frame.setLocation(100,100);
        frame.setBackground(Color.darkGray);
        //总体的两行一列
        frame.setLayout(new GridLayout(2,1));

        //4个面板
        Panel p1 = new Panel(new BorderLayout());//上半部分东西南北中的部分
        Panel p2 = new Panel(new GridLayout(2,1));//上半部分中间的部分
        Panel p3 = new Panel(new BorderLayout());//下半部分
        Panel p4 = new Panel(new GridLayout(2,2));//下半部分

        //放置上半部分按钮位置
        p1.add(new Button("east-1"),BorderLayout.EAST);
        p1.add(new Button("wast-1"),BorderLayout.WEST);
        //表格布局,直接填充了
        p2.add(new Button("button1"));
        p2.add(new Button("button2"));
        //将面板p2放在面板p1上
        p1.add(p2,BorderLayout.CENTER);

        //放置下半部分
        p3.add(new Button("east-2"),BorderLayout.EAST);
        p3.add(new Button("wast-2"),BorderLayout.WEST);
        for (int i = 3; i <7 ; i++) {
            p4.add(new Button("button"+i));
        }

        p3.add(p4,BorderLayout.CENTER);

        frame.add(p1);
        frame.add(p3);


    }

}

运行结果:
practice结果

源代码参考GUI-Study—>lesson1->testPanel

发布了24 篇原创文章 · 获赞 5 · 访问量 532

猜你喜欢

转载自blog.csdn.net/weixin_46178009/article/details/105317552