Layout Managers(布局管理器)

 

import java.awt.*;
import javax.swing.*;

public class LayoutManagers {
 public static void main(String[] args) {
  JFrame frame=new JFrame(“Layout Managers”);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  JTabbedPane tp=new JTabbedPane();   //创建一个选项卡tp
  tp.addTab(“Intro”, new IntroPanel());
  tp.addTab(“Flow”,new FlowPanel());
  tp.addTab(“Border”,new BorderPanel());
  tp.addTab(“Grid”, new GridPanel()); 
  tp.addTab(“Box”, new BoxPanel());
  
  frame.getContentPane().add(tp);
  frame.pack();
  frame.setVisible(true);
 }
}

 class IntroPanel extends JPanel{
 public IntroPanel(){
  setBackground(Color.green);   //设置背景色
  
  JLabel l1=new JLabel(“Layout Manager Demonstration “);
  JLabel l2=new JLabel(“Choose a tab to see an example of “+”a layout manager.”);
  
  add(l1);
  add(l2);
 }
}
 
 class FlowPanel extends JPanel{
  public FlowPanel(){
   setLayout(new FlowLayout());         //FlowLayout(流布局管理器)
   setBackground(Color.green);
             //创建按钮
   JButton b1=new JButton(“BUTTON 1″);
   JButton b2=new JButton(“BUTTON 2″);
   JButton b3=new JButton(“BUTTON 3″);
   JButton b4=new JButton(“BUTTON 4″);
   JButton b5=new JButton(“BUTTON 5″);
   JButton b6=new JButton(“BUTTON 6″);
             //把按钮添加到容器中
   add(b1);
   add(b2);
   add(b3);
   add(b4);
   add(b5);
   add(b6);
  }
 }

 class BorderPanel extends JPanel{
  public BorderPanel(){
   setLayout(new BorderLayout());   //BorderLayout(边布局管理器)
   setBackground(Color.green);
             //创建按钮
   JButton b1=new JButton(“BUTTON 1″);
   JButton b2=new JButton(“BUTTON 2″);
   JButton b3=new JButton(“BUTTON 3″);
   JButton b4=new JButton(“BUTTON 4″);
   JButton b5=new JButton(“BUTTON 5″);
             //把按钮添加到容器中
   add(b1,BorderLayout.CENTER);
   add(b2,BorderLayout.NORTH);
   add(b3,BorderLayout.SOUTH);
   add(b4,BorderLayout.EAST);
   add(b5,BorderLayout.WEST);
  }
 }
 
 class GridPanel extends JPanel{
  public GridPanel(){
   setLayout(new GridLayout(2,3)); //GridLayout(网格布局管理器)
   setBackground(Color.green);
             //创建按钮
   JButton b1=new JButton(“BUTTON 1″);
   JButton b2=new JButton(“BUTTON 2″);
   JButton b3=new JButton(“BUTTON 3″);
   JButton b4=new JButton(“BUTTON 4″);
   JButton b5=new JButton(“BUTTON 5″);
   JButton b6=new JButton(“BUTTON 6″);
             //把按钮添加到容器中
   add(b1);
   add(b2);
   add(b3);
   add(b4);
   add(b5);
   add(b6);
  }
 }
 
 class BoxPanel extends JPanel{
  public BoxPanel(){
   setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
   setBackground(Color.green);
              //创建按钮
   JButton b1=new JButton(“BUTTON 1″);
   JButton b2=new JButton(“BUTTON 2″);
   JButton b3=new JButton(“BUTTON 3″);
   JButton b4=new JButton(“BUTTON 4″);
   JButton b5=new JButton(“BUTTON 5″);
   JButton b6=new JButton(“BUTTON 6″);
              //把按钮添加到容器中
   add(b1);
   add(Box.createRigidArea(new Dimension(0,10)));
   add(b2);
   add(Box.createVerticalGlue());
   add(b3);
   add(b4);
   add(Box.createRigidArea(new Dimension(0,20)));
   add(b5);
   add(b6);
  }
 }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xcbeyond

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值