Layout Managers(布局管理器)

作者:xcbeyond
瘋狂源自夢想,技術(shù)成就輝煌!微信公眾號:《程序猿技術(shù)大咖》號主,專注后端開發(fā)多年,擁有豐富的研發(fā)經(jīng)驗,樂于技術(shù)輸出、分享,現(xiàn)階段從事微服務(wù)架構(gòu)項目的研發(fā)工作,涉及架構(gòu)設(shè)計、技術(shù)選型、業(yè)務(wù)研發(fā)等工作。對于Java、微服務(wù)、數(shù)據(jù)庫、Docker有深入了解,并有大量的調(diào)優(yōu)經(jīng)驗。

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();   //創(chuàng)建一個選項卡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);   //設(shè)置背景色
  
  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);
             //創(chuàng)建按鈕
   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);
             //創(chuàng)建按鈕
   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(網(wǎng)格布局管理器)
   setBackground(Color.green);
             //創(chuàng)建按鈕
   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);
              //創(chuàng)建按鈕
   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);
  }
 }