📜  AWT布局(1)

📅  最后修改于: 2023-12-03 15:13:35.004000             🧑  作者: Mango

AWT布局

AWT布局指的是Java AWT窗体布局管理器的使用。AWT(Abstract Windowing Toolkit)是Java的一个GUI工具包,它是Java之父James Gosling和他的小组创建的,被称为Java的第一个GUI工具包。

AWT布局管理器是用于在AWT容器中排列组件的类,它可以自动排列组件以适应不同的容器大小。布局管理器可以确保组件能够放置在相应容器中的正确位置,并且能够相对于其他组件正确响应大小调整。AWT一共有五个布局管理器,分别是BorderLayout、FlowLayout、GridLayout、CardLayout和GridBagLayout。

BorderLayout

BorderLayout布局将容器划分为五个区域:北、东、南、西、中。容器中的每个组件都可以放置在其中一个区域内。如果在一个区域内放置了多个组件,那么只有最后一个组件会被显示。

import java.awt.*;

public class BorderLayoutExample {

    public static void main(String[] args) {
        Frame frame = new Frame("BorderLayout Example");
        Button north = new Button("North");
        Button south = new Button("South");
        Button east = new Button("East");
        Button west = new Button("West");
        Button center = new Button("Center");

        frame.add(north, BorderLayout.NORTH);
        frame.add(south, BorderLayout.SOUTH);
        frame.add(east, BorderLayout.EAST);
        frame.add(west, BorderLayout.WEST);
        frame.add(center, BorderLayout.CENTER);

        frame.setSize(300, 300);
        frame.setVisible(true);
    }
}
FlowLayout

FlowLayout布局按照左到右、上到下的顺序排列组件,并根据容器大小自动换行。

import java.awt.*;

public class FlowLayoutExample {

    public static void main(String[] args) {
        Frame frame = new Frame("FlowLayout Example");
        Panel panel = new Panel(new FlowLayout());

        for (int i = 1; i <= 10; i++) {
            Button button = new Button("Button " + i);
            panel.add(button);
        }

        frame.add(panel);
        frame.setSize(300, 300);
        frame.setVisible(true);
    }
}
GridLayout

GridLayout布局将容器划分为固定的行数和列数,并将组件顺序地放置在每个单元格中。如果多余的组件存在,则它们会被忽略。

import java.awt.*;

public class GridLayoutExample {

    public static void main(String[] args) {
        Frame frame = new Frame("GridLayout Example");
        Panel panel = new Panel(new GridLayout(3, 3));

        for (int i = 1; i <= 9; i++) {
            Button button = new Button("Button " + i);
            panel.add(button);
        }

        frame.add(panel);
        frame.setSize(300, 300);
        frame.setVisible(true);
    }
}
CardLayout

CardLayout布局将多个组件作为卡片,只有一个组件可见。可以使用下一个和上一个按钮切换卡片。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CardLayoutExample {

    public static void main(String[] args) {
        Frame frame = new Frame("CardLayout Example");
        Panel panel = new Panel(new CardLayout());

        Button button1 = new Button("Card 1");
        Button button2 = new Button("Card 2");
        Button button3 = new Button("Card 3");

        panel.add(button1, "card1");
        panel.add(button2, "card2");
        panel.add(button3, "card3");

        Button nextButton = new Button("Next");
        Button prevButton = new Button("Prev");

        nextButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CardLayout cardLayout = (CardLayout) panel.getLayout();
                cardLayout.next(panel);
            }
        });

        prevButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CardLayout cardLayout = (CardLayout) panel.getLayout();
                cardLayout.previous(panel);
            }
        });

        Panel buttonPanel = new Panel();
        buttonPanel.add(prevButton);
        buttonPanel.add(nextButton);

        frame.add(panel, BorderLayout.CENTER);
        frame.add(buttonPanel, BorderLayout.SOUTH);

        frame.setSize(300, 300);
        frame.setVisible(true);
    }
}
GridBagLayout

GridBagLayout布局将组件放置在网格中,每个组件占据一个或多个单元格,并可以设置组件大小和位置。

import java.awt.*;

public class GridBagLayoutExample {

    public static void main(String[] args) {
        Frame frame = new Frame("GridBagLayout Example");
        Panel panel = new Panel(new GridBagLayout());

        Button button1 = new Button("Button 1");
        Button button2 = new Button("Button 2");
        Button button3 = new Button("Button 3");
        Button button4 = new Button("Button 4");

        GridBagConstraints constraints1 = new GridBagConstraints();
        constraints1.gridx = 0;
        constraints1.gridy = 0;
        constraints1.insets = new Insets(5, 5, 5, 5);
        panel.add(button1, constraints1);

        GridBagConstraints constraints2 = new GridBagConstraints();
        constraints2.gridx = 1;
        constraints2.gridy = 0;
        constraints2.insets = new Insets(5, 5, 5, 5);
        panel.add(button2, constraints2);

        GridBagConstraints constraints3 = new GridBagConstraints();
        constraints3.gridx = 0;
        constraints3.gridy = 1;
        constraints3.gridwidth = 2;
        constraints3.insets = new Insets(5, 5, 5, 5);
        panel.add(button3, constraints3);

        GridBagConstraints constraints4 = new GridBagConstraints();
        constraints4.gridx = 2;
        constraints4.gridy = 0;
        constraints4.gridheight = 2;
        constraints4.insets = new Insets(5, 5, 5, 5);
        panel.add(button4, constraints4);

        frame.add(panel);
        frame.setSize(300, 300);
        frame.setVisible(true);
    }
}