在 Java Swing 中主要的布局管理器包括以下几种
BorderLayout(边界布局)
- 它将容器划分为五个区域:北(BorderLayout.NORTH)、南(BorderLayout.SOUTH)、东(BorderLayout.EAST)、西(BorderLayout.WEST)和中(BorderLayout.CENTER)。例如:
import javax.swing.*;
public class BorderLayoutExample {
public static void main(String[] args) {
JFrame frame = new JFrame("BorderLayout Example");
JPanel panel = new JPanel(new BorderLayout());
JButton northButton = new JButton("North");
JButton southButton = new JButton("South");
JButton eastButton = new JButton("East");
JButton westButton = new JButton("West");
JButton centerButton = new JButton("Center");
panel.add(northButton, BorderLayout.NORTH);
panel.add(southButton, BorderLayout.SOUTH);
panel.add(eastButton, BorderLayout.EAST);
panel.add(westButton, BorderLayout.WEST);
panel.add(centerButton, BorderLayout.CENTER);
frame.add(panel);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
- 适用于有明显主次区域划分的布局,比如一个窗口中有一个主要的显示区域(中心),周围是操作按钮(东西南北)。
FlowLayout(流式布局)
- 按照组件添加的顺序,从左到右依次排列,排满一行后自动换行。如:
import javax.swing.*;
public class FlowLayoutExample {
public static void main(String[] args) {
JFrame frame = new JFrame("FlowLayout Example");
JPanel panel = new JPanel(new FlowLayout());
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");
JButton button4 = new JButton("Button 4");
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
frame.add(panel);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
- 常用于简单罗列多个组件,比如一组按钮或者标签,这种布局比较自然,不会强制组件的位置。
GridLayout(网格布局)
- 以表格形式布局组件,需要指定行数和列数,组件会均匀分布在表格单元格内。例如:
import javax.swing.*;
public class GridLayoutExample {
public static void main(String[] args) {
JFrame frame = new JFrame("GridLayout Example");
JPanel panel = new JPanel(new GridLayout(3, 2));
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
JButton button6 = new JButton("6");
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(button5);
panel.add(button6);
frame.add(panel);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
- 适合整齐排列多个等大的组件,例如棋盘格样式的布局或者矩阵形式的组件排列。
BoxLayout(箱式布局)
- 可以在一个轴向上(水平或垂直)排列组件。它可以使用 Box 类来创建,例如:
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
public class BoxLayoutExample {
public static void main(String[] args) {
JFrame frame = new JFrame("BoxLayout Example");
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");
panel.add(button1);
panel.add(button2);
panel.add(button3);
frame.add(panel);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
- 常用于创建垂直或水平排列的组件组,并且可以通过一些辅助类(如 Box.createHorizontalStrut 用于创建水平间距)来调整组件之间的间隔等。
CardLayout(卡片布局)
- 它把容器中的每个组件看作一张卡片,在某一时刻只有一张卡片是可见的。例如:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CardLayoutExample {
public static void main(String[] args) {
JFrame frame = new JFrame("CardLayout Example");
JPanel panel = new JPanel();
CardLayout cardLayout = new CardLayout();
panel.setLayout(cardLayout);
JButton button1 = new JButton("Show Card 1");
JButton button2 = new JButton("Show Card 2");
JPanel card1 = new JPanel();
JPanel card2 = new JPanel();
card1.add(new JLabel("This is Card 1"));
card2.add(new JLabel("This is Card 2"));
panel.add(card1, "card1");
panel.add(card2, "card2");
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cardLayout.show(panel, "card1");
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cardLayout.show(panel, "card2");
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(button1);
buttonPanel.add(button2);
frame.add(panel);
frame.add(buttonPanel, BorderLayout.SOUTH);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
- 适合用于实现多页面切换的场景,比如向导式的界面,用户可以在不同的 “卡片”(页面)之间切换。
GridBagLayout(网格袋布局)
- 它是 GridLayout 的更灵活的版本。可以指定每个组件在网格中的位置、跨越的行数和列数等,通过 GridBagConstraints 类来设置组件的布局约束。例如:
import javax.swing.*;
import java.awt.*;
public class GridBagLayoutExample {
public static void main(String[] args) {
JFrame frame = new JFrame("GridBagLayout Example");
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1.0;
constraints.weighty = 1.0;
constraints.gridx = 0;
constraints.gridy = 0;
panel.add(button1, constraints);
constraints.gridx = 1;
panel.add(button2, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
panel.add(button3, constraints);
frame.add(panel);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
- 用于复杂的、非规则的网格布局需求,当组件大小不一、需要占据不同的行 / 列范围等情况时非常有用。
在 JavaFX 中也有多种布局方式
- HBox(水平布局)和 VBox(垂直布局)
- HBox 用于将组件在水平方向上排列,VBox 用于在垂直方向上排列。例如:
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class JavaFXLayoutExample extends Application {
@Override
public void start(Stage primaryStage) {
Button button1 = new Button("Button 1");
Button button2 = new Button("Button 2");
HBox hbox = new HBox(button1, button2);
Label label1 = new Label("Label 1");
Label label2 = new Label("Label 2");
VBox vbox = new VBox(label1, label2);
ObservableList<String> list = FXCollections.observableArrayList("Item 1", "Item 2", "Item 3");
ListView<String> listView = new ListView<>(list);
VBox root = new VBox(hbox, vbox, listView);
Scene scene = new Scene(root, 300, 300);
primaryStage.setTitle("JavaFX Layout Example");
primaryStage.setScene(scene);
primaryStage.show();
}
}
BorderPane(边界布局)
- 与 Java Swing 中的 BorderLayout 类似,将场景分为五个区域:上、下、左、右和中心。例如:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class JavaFXBorderPaneExample extends Application {
@Override
public void start(Stage primaryStage) {
Button topButton = new Button("Top");
Button bottomButton = new Button("Bottom");
Button leftButton = new Button("Left");
Button rightButton = new Button("Right");
Button centerButton = new Button("Center");
BorderPane borderPane = new BorderPane();
borderPane.setTop(topButton);
borderPane.setBottom(bottomButton);
borderPane.setLeft(leftButton);
borderPane.setRight(rightButton);
borderPane.setCenter(centerButton);
Scene scene = new Scene(borderPane, 300, 300);
primaryStage.setTitle("JavaFX BorderPane Example");
primaryStage.setScene(scene);
primaryStage.show();
}
}
GridPane(网格布局)
- 以网格形式排列组件,可以指定组件所在的行和列位置,还可以设置组件跨越多行或多列。例如:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class JavaFXGridPaneExample extends Application {
@Override
public void start(Stage primaryStage) {
Button button1 = new Button("1");
Button button2 = new Button("2");
Button button3 = new Button("3");
Button button4 = new Button("4");
GridPane gridPane = new GridPane();
gridPane.add(button1, 0, 0);
gridPane.add(button2, 1, 0);
gridPane.add(button3, 0, 1);
gridPane.add(button4, 1, 1);
Scene scene = new Scene(gridPane, 300, 300);
primaryStage.setTitle("JavaFX GridPane Example");
primaryStage.setScene(scene);
primaryStage.show();
}
}
StackPane(堆叠布局)
- 所有组件堆叠在一起,位于同一位置,最后添加的组件在最上面。例如:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class JavaFXStackPaneExample extends Application {
@Override
public void start(Stage primaryStage) {
Button button1 = new Button("Button 1");
Button button2 = new Button("Button 2");
StackPane stackPane = new StackPane(button1, button2);
Scene scene = new Scene(stackPane, 300, 300);
primaryStage.setTitle("JavaFX StackPane Example");
primaryStage.setScene(scene);
primaryStage.show();
}
}
TilePane(平铺布局)
- 以平铺的方式排列组件,组件大小可以相同也可以不同,它会自动调整布局以适应组件的大小。例如:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
public class JavaFXTilePaneExample extends Application {
@Override
public void start(Stage primaryStage) {
Button button1 = new Button("Button 1");
Button button2 = new Button("Button 2");
Button button3 = new Button("Button 3");
TilePane tilePane = new TilePane(button1, button2, button3);
Scene scene = new Scene(tilePane, 300, 300);
primaryStage.setTitle("JavaFX TilePane Example");
primaryStage.setScene(scene);
primaryStage.show();
}
}
结尾
这些只是比较常见的布局管理器,不同的布局管理器适用于不同的场景,开发者可以根据具体的 UI 设计需求来选择合适的布局方式。
2281

被折叠的 条评论
为什么被折叠?



