现在的位置: 首页 > 综合 > 正文

Layout Managers(布局管理器)

2018年01月11日 ⁄ 综合 ⁄ 共 2387字 ⁄ 字号 评论关闭



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);
  }
 }

抱歉!评论已关闭.