最最最实用的5种Java布局方式

java的布局方式非常常用,以下介绍5种常用的布局方式。
1.FlowLayout(流式布局)
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
/**

  • 将组件已加入顺序从左到右排序,一行
  • 排满就会去第二行。
  • 每行组件剧中排列

*/
public class FlowLayoutDemo {

private JFrame frame;
private JButton button1,button2,button3,button4,button5;

public static void main(String[] args) {
    FlowLayoutDemo fd = new FlowLayoutDemo();
    fd.go();
}

private void go() {
    frame = new JFrame("flowlayout");
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new FlowLayout());
    button1 = new JButton("OK");
    button2 = new JButton("OPEN");
    button3 = new JButton("CLOSE");
    button4 = new JButton("aaa");
    button5 = new JButton("CLOSEccc");
    contentPane.add(button1);
    contentPane.add(button2);
    contentPane.add(button3);
    contentPane.add(button4);
    contentPane.add(button5);
    frame.setSize(300,200);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}12345678910111213141516171819202122232425262728293031323334353637383940运行结果如下 在这里插入图片描述

2.BorderLayout(边界布局)
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

/**

  • 由BorderLayout管理的容器被划分为5个区域
  • North south west east center

*/
public class BorderLayoutDemo {
private JFrame frame;
private JButton be,bw,bn,bs,bc;

public static void main(String[] args) {
    BorderLayoutDemo bd = new BorderLayoutDemo();
    bd.go();
}

private void go() {

    frame = new JFrame("BorderLayoutDemo");
    be = new JButton("east");
    bw = new JButton("west");
    bn = new JButton("north");
    bs = new JButton("south");
    bc = new JButton("center");
    frame.getContentPane().add(be,BorderLayout.EAST);
    frame.getContentPane().add(bw,BorderLayout.WEST);
    frame.getContentPane().add(bn,BorderLayout.NORTH);
    frame.getContentPane().add(bs,BorderLayout.SOUTH);
    frame.getContentPane().add(bc,BorderLayout.CENTER);

    frame.setSize(300, 200);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
}

}
12345678910111213141516171819202122232425262728293031323334353637383940运行结果如下
在这里插入图片描述

3.GridLayout(网格布局)
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

/**

  • 将容器分为若干行和列,组件依次放入其中
  • 每个组件占据一格。

*/
public class GridLayoutDemo {
public static void main(String[] args) {
MyWindow that = new MyWindow();
that.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

@SuppressWarnings(“serial”)
class MyWindow extends JFrame {
private JButton b1,b2,b3,b4,b5,b6;
public MyWindow() {
setTitle(“grid”);
Container contentPane = getContentPane();
contentPane.setPreferredSize(new Dimension(400,300));
contentPane.setLayout(new GridLayout(3,2));//3行2列
b1 =new JButton(“b1”);
b2 =new JButton(“b2”);
b3 =new JButton(“b3”);
b4 =new JButton(“b4”);
b5 =new JButton(“b5”);
b6 =new JButton(“b6”);
contentPane.add(b1);
contentPane.add(b2);
contentPane.add(b3);
contentPane.add(b4);
contentPane.add(b5);
contentPane.add(b6);
pack();
setVisible(true);
}
}1234567891011121314151617181920212223242526272829303132333435363738394041424344运行结果如下
在这里插入图片描述

4.BoxLayout(盒式布局)
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Label;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**

  • 将容器中的组件水平或者竖直排成一行或一列

*/
public class BoxLayoutDemo {

private JFrame jFrame;
private JPanel jPanel,jPanel2;
public static void main(String[] args) {
    BoxLayoutDemo boxLayoutDemo = new BoxLayoutDemo();
    boxLayoutDemo.go();

}
private void go () {
    jFrame = new JFrame("box lay");
    Container contentPane = jFrame.getContentPane();
    jPanel = new JPanel();
    jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.Y_AXIS));
    jPanel.add(new Label("1"));
    jPanel.add(new Label("2"));
    jPanel.add(new Label("3"));
    contentPane.add(jPanel, BoxLayout.X_AXIS);
    jPanel2 = new JPanel();
    jPanel2.setLayout(new BoxLayout(jPanel2, BoxLayout.X_AXIS));
    jPanel2.add(new Label("yes"));
    jPanel2.add(new Label("no"));
    jPanel2.add(new Label("cancel"));
    //contentPane.add(jPanel2,BorderLayout.SOUTH);
    //contentPane.add(jPanel2,FlowLayout.CENTER);
    jFrame.pack();
    jFrame.setVisible(true);
    jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}
123456789101112131415161718192021222324252627282930313233343536373839404142434445运行结果如下
在这里插入图片描述

5.CradLayout(卡片布局)
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**

  • 卡片布局 每时每刻只显示一张

*/
@SuppressWarnings(“serial”)
public class CardlayoutDemo extends JFrame {

public static void main(String[] args) {
    new CardlayoutDemo();
}

public CardlayoutDemo() {
      this.setTitle("Cardlayout");
      this.setSize(400, 400);
      this.setLocationRelativeTo(null);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Container c = this.getContentPane();
      JButton jbtn1 = new JButton("上一个");
      c.add(jbtn1, BorderLayout.NORTH);
      final JPanel mainJPanel = new JPanel(); 
      //匿名内部类只能访问final变量
      final CardLayout cardLayout = new CardLayout(); 
      mainJPanel.setLayout(cardLayout);
      mainJPanel.add(new JLabel("第1张"), "one"); 
      mainJPanel.add(new JLabel("第2张"), "two");
      mainJPanel.add(new JLabel("第3张"), "three");
      mainJPanel.add(new JLabel("第4张"), "four");
      c.add(mainJPanel, BorderLayout.CENTER); 
      JButton jbtn2 = new JButton("下一个"); 
      c.add(jbtn2, BorderLayout.SOUTH); 

      jbtn1.addActionListener(new ActionListener() {  
       public void actionPerformed(ActionEvent e) {  
        cardLayout.previous(mainJPanel);
       }  
      });  
      jbtn2.addActionListener(new ActionListener() {  
       public void actionPerformed(ActionEvent e) {  
        cardLayout.next(mainJPanel);
       }  
      });  
      //cardLayout.show(mainJPanel, "three");
      this.setVisible(true);
}

}
运行结果如下
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值