Boxlayout布局管理器的简单使用

时间:2023-01-28 20:28:34
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;


import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;


public class BoxLayoutDemo {
JPanel panel, topPanel, middlePanel, bottomPanel;


public BoxLayoutDemo() {
// 创建 topPanel
createTopJPanel();
// 创建 middlePanel
createMiddleJpanel();
// 创建 bottomPanel
createBottomJpanel();
// 创建包含 topPanel,middlePanel 和 bottomPanel 的 panelContainer
JPanel panelContainer = new JPanel();
// panelContainer 的布局为 GridBagLayout
panelContainer.setLayout(new GridBagLayout());
GridBagConstraints c1 = new GridBagConstraints();
c1.gridx = 0;
c1.gridy = 0;
c1.weightx = 1.0;
c1.weighty = 1.0;
c1.fill = GridBagConstraints.BOTH;
// 加入 topPanel
panelContainer.add(topPanel, c1);
GridBagConstraints c2 = new GridBagConstraints();
c2.gridx = 0;
c2.gridy = 1;
c2.weightx = 1.0;
c2.weighty = 0;
c2.fill = GridBagConstraints.HORIZONTAL;
// 加入 middlePanel
panelContainer.add(middlePanel, c2);
GridBagConstraints c3 = new GridBagConstraints();
c3.gridx = 0;
c3.gridy = 2;
c3.weightx = 1.0;
c3.weighty = 0;
c3.fill = GridBagConstraints.HORIZONTAL;
// 加入 bottomPanel
panelContainer.add(bottomPanel, c3);


// 创建窗体
JFrame frame = new JFrame("Boxlayout 演示");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panelContainer.setOpaque(true);
frame.setSize(new Dimension(480, 320));
frame.setContentPane(panelContainer);
frame.setVisible(true);
}


protected void createTopJPanel() {
topPanel = new JPanel();
String[] columnName = { "姓名", "性别", "单位", "参加项目", "备注" };
String[][] rowData = { { "张三", "男", "计算机系", "100 米 ,200 米", "" },
{ "李四", "男", "化学系", "100 米,铅球", "" }, };
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
JTable table = new JTable(new DefaultTableModel(rowData, columnName));
JScrollPane scrollPane = new JScrollPane(table);
scrollPane
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
topPanel.add(Box.createHorizontalStrut(10));
topPanel.add(scrollPane);
topPanel.add(Box.createHorizontalStrut(10));
}


protected void createMiddleJpanel() {
JPanel panelLeft, panelMiddle, panelRight;
middlePanel = new JPanel();
middlePanel.setLayout(new BoxLayout(middlePanel, BoxLayout.X_AXIS));
panelLeft = new JPanel();
panelLeft.setLayout(new BoxLayout(panelLeft, BoxLayout.X_AXIS));
JLabel sourceLabel = new JLabel("运动会项目:");
sourceLabel.setAlignmentY(Component.TOP_ALIGNMENT);
sourceLabel.setBorder(BorderFactory.createEmptyBorder(4, 5, 0, 5));


DefaultListModel dlm = new DefaultListModel();
dlm.addElement("100 米");
dlm.addElement("200 米");
dlm.addElement("400 米");
dlm.addElement("跳远");
dlm.addElement("跳高");
dlm.addElement("铅球");
dlm.addElement("篮球");
JList list = new JList(dlm);
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
list.setVisibleRowCount(3);
JScrollPane scroll = new JScrollPane(list);
scroll.setPreferredSize(new Dimension(120, 80));
scroll
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setAlignmentY(Component.TOP_ALIGNMENT);
scroll.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 30));
panelLeft.add(sourceLabel);
panelLeft.add(scroll);
middlePanel.add(panelLeft);


// 定义中间的两个按钮
JButton toTargetButton = new JButton(">>");
JButton toSourceButton = new JButton("<<");


panelMiddle = new JPanel();
panelMiddle.setLayout(new BoxLayout(panelMiddle, BoxLayout.Y_AXIS));
panelMiddle.add(toTargetButton);
// 两个按钮之间加入一个不可见的 rigidArea
panelMiddle.add(Box.createRigidArea(new Dimension(15, 15)));
// 将按钮 << 加入到中间的 Panel
panelMiddle.add(toSourceButton);
panelMiddle.setAlignmentY(Component.TOP_ALIGNMENT);
panelMiddle.setBorder(BorderFactory.createEmptyBorder(15, 5, 15, 5));
// 将中间的 Panel 加入到 middlePanel
middlePanel.add(panelMiddle);


JLabel targetLabel = new JLabel("查询项目:");
targetLabel.setAlignmentY(Component.TOP_ALIGNMENT);
targetLabel.setBorder(BorderFactory.createEmptyBorder(4, 5, 0, 5));
// 创建列表查询项目
DefaultListModel targetListModel = new DefaultListModel();
targetListModel.addElement("100 米");
JList targetList = new JList(targetListModel);
targetList
.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
targetList.setVisibleRowCount(5);
JScrollPane targetListScroller = new JScrollPane(targetList);
targetListScroller.setPreferredSize(new Dimension(120, 80));
targetListScroller
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
targetListScroller.setAlignmentY(Component.TOP_ALIGNMENT);
// 创建最右边的 Panel
panelRight = new JPanel();
// 设置最右边的 Panel 为水平布局
panelRight.setLayout(new BoxLayout(panelRight, BoxLayout.X_AXIS));
// 将标签查询项目加到最右边的 Panel
panelRight.add(targetLabel);
// 将列表查询项目加到最右边的 Panel
panelRight.add(targetListScroller);
panelRight.setAlignmentY(Component.TOP_ALIGNMENT);
panelRight.setBorder(BorderFactory.createEmptyBorder(0, 30, 0, 0));
// 最后将最右边的 Panel 加入到 middlePanel
middlePanel.add(panelRight);
}


protected void createBottomJpanel() {
bottomPanel = new JPanel();
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.X_AXIS));
JButton buttonOK = new JButton("确定");
JButton buttonCancel = new JButton("取消");
bottomPanel.add(buttonOK);
bottomPanel.add(Box.createHorizontalGlue());
bottomPanel.add(buttonCancel);
}


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