布局模式

布局模式

  • 流式布局

            Frame frame = new Frame("这是我的第一个frame窗口!");
            frame.setVisible(true);
            frame.setSize(400, 400);
            frame.setBackground(Color.LIGHT_GRAY);
            frame.setLayout(new FlowLayout(FlowLayout.CENTER));//流式布局
    
            Button btn1 = new Button("按钮1");
            btn1.setBackground(Color.BLUE);
            Button btn2 = new Button("按钮2");
            btn2.setBackground(Color.CYAN);
            Button btn3 = new Button("按钮3");
            btn3.setBackground(Color.DARK_GRAY);
    
            frame.add(btn1);
            frame.add(btn2);
            frame.add(btn3);
    

在这里插入图片描述

  • 东西南北中布局

            Frame frame = new Frame("这是我的第一个frame窗口!");
            frame.setVisible(true);
            frame.setBounds(50,50,500,500);
            frame.setBackground(Color.DARK_GRAY);
            frame.setLayout(new BorderLayout()); //东西南北中布局
    
            frame.add(new Button("East"),BorderLayout.EAST);
            frame.add(new Button("Wast"),BorderLayout.WEST);
            frame.add(new Button("South"),BorderLayout.SOUTH);
            frame.add(new Button("North"),BorderLayout.NORTH);
            frame.add(new Button("Center"),BorderLayout.CENTER);
    
            frame.setVisible(true);
    

在这里插入图片描述

  • 表格布局

            Frame frame = new Frame("这是我的第一个frame窗口!");
            frame.setBounds(50,50,500,500);
            frame.setBackground(Color.DARK_GRAY);
            frame.setLayout(new GridLayout(3,2)); //表格布局
    
            frame.add(new Button("b1"));
            frame.add(new Button("b2"));
            frame.add(new Button("b3"));
            frame.add(new Button("b4"));
            frame.add(new Button("b5"));
            frame.add(new Button("b6"));
    
            frame.pack();
            frame.setVisible(true);
    

在这里插入图片描述

  • 小练习

            Frame frame = new Frame("这是我的第一个frame窗口!");
            frame.setBounds(50,50,800,800);
            frame.setBackground(Color.DARK_GRAY);
            frame.setLayout(new GridLayout(2,1));
    
            //上面板
            Panel p1 = new Panel(new BorderLayout());
            p1.setBackground(Color.GREEN);
    
            Panel p12 = new Panel(new GridLayout(2,1));
            p12.add(new Button("上中上"));
            p12.add(new Button("上中下"));
            p1.add(new Button("上左"),BorderLayout.EAST);
            p1.add(new Button("上右"),BorderLayout.WEST);
            p1.add(p12,BorderLayout.CENTER);
    
            //下面板
            Panel p2 = new Panel(new BorderLayout());
            p2.setBackground(Color.BLUE);
    
            Panel p22 = new Panel(new GridLayout(2,2));
            p22.add(new Button("下上左"));
            p22.add(new Button("下上右"));
            p22.add(new Button("下下左"));
            p22.add(new Button("下下右"));
            p2.add(new Button("下左"),BorderLayout.EAST);
            p2.add(new Button("下右"),BorderLayout.WEST);
            p2.add(p22,BorderLayout.CENTER);
    
            frame.add(p1);
            frame.add(p2);
    
            frame.setVisible(true);
    

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值