java学习--GUI4

布局管理器

1、概念:组件在容器(比如JFrame)中的位置和大小是由布局管理器来决定的。所有的容器都会使用一个布局管理器,通过它来自动进行组件的布局管理。

2、种类

  • 流式布局管理器(FlowLayout)
  • 边界布局管理器(BorderLayout)
  • 网格布局管理器(GridLayout)
  • 卡片布局管理器(CardLayout)
  • 网格包布局管理器(GridBagLayout)

 流式布局

  按照组件的添加次序讲组件从左到右放置在容器中,当到达容器边界的时候,自动换行;可以左、中、右对齐。

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

public class TestFlowLayout {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Welcome to leaf's blog");
        JButton but1 = new JButton("按钮1");    
        JButton but2 = new JButton("按钮2");    
        JButton but3 = new JButton("按钮3");    
        JButton but4 = new JButton("按钮4");    
        JButton but5 = new JButton("按钮5");    
        JButton but6 = new JButton("按钮6");    
        frame.add(but1);
        frame.add(but2);
        frame.add(but3);
        frame.add(but4);
        frame.add(but5);
        frame.add(but6);
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.setSize(100, 100);
        frame.setLocation(200, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

使用循环产生多个按钮

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

public class TestFlowLayout2 {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Welcome to leaf's blog");
        for (int i = 0; i < 100; i++) {
            JButton but1 = new JButton();
            but1.setText("按钮" + i);
            frame.add(but1);
        }
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.setSize(100, 100);
        frame.setLocation(200, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

边界布局

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

public class TestBorderLayout {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Welcome to leaf's blog");
        frame.setLayout(new BorderLayout());
        
        JButton but1 = new JButton("中");
        JButton but2 = new JButton("东");
        JButton but3 = new JButton("南");
        JButton but4 = new JButton("西");
        JButton but5 = new JButton("北");
     
        frame.add(but1,BorderLayout.CENTER); 
        frame.add(but2, BorderLayout.EAST);
        frame.add(but3, BorderLayout.SOUTH);
        frame.add(but4, BorderLayout.WEST);
        frame.add(but5, BorderLayout.NORTH);
  
        frame.setSize(100, 100);
        frame.setLocation(200, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

网格布局

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

public class TestGridLayout {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Welcome to leaf's blog");
        frame.setLayout(new GridLayout(10,10));
        for (int i = 0; i < 100; i++) {
            JButton but1 = new JButton();
            but1.setText("按钮" + i);
            frame.add(but1); 
        }
        frame.setSize(100, 100);
        frame.setLocation(200, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

卡片布局

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

public class TestCardLayout {
    public static void main(String[] args) {
        final JFrame frame = new JFrame("Welcome to leaf's blog");
        final CardLayout G = new CardLayout();
        frame.getContentPane().setLayout(G);
        for (int i = 0; i < 100; i++) {
            JButton but1 = new JButton();
            but1.setText("按钮" + i);
            frame.getContentPane().add(but1, "按钮" + i); 
        }

        new Thread() {
            public void run() {
                while(true) {
                    G.next(frame.getContentPane());
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }     
                }
            };
        }.start();

        frame.setSize(100, 100);
        frame.setLocation(200, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
原文地址:https://www.cnblogs.com/lea-fu/p/3254692.html