Java 在jpanel中对齐jpanel

Java 在jpanel中对齐jpanel,java,swing,user-interface,alignment,layout-manager,Java,Swing,User Interface,Alignment,Layout Manager,我想做一个像这样的对话 这就是我到目前为止所做的 我现在要处理的问题是“表属性”面板的网格布局。我目前不喜欢的是创建的空白网格布局。 我可以调整这个面板的大小到原型图片中面板的大小吗 如果没有,我想去掉“GridLayout”,而是使用带有“Y_轴”的“BoxLayout”,然后使用flowlayout创建两个面板,其中第一个面板将具有“FontStyle”和“Table Data”面板,第二个面板将具有“选择模式和选项”以及其他属性? 如果有更好的方法,请提出建议 代码如下: class

我想做一个像这样的对话

这就是我到目前为止所做的

我现在要处理的问题是“表属性”面板的网格布局。我目前不喜欢的是创建的空白网格布局。 我可以调整这个面板的大小到原型图片中面板的大小吗

如果没有,我想去掉“GridLayout”,而是使用带有“Y_轴”的“BoxLayout”,然后使用flowlayout创建两个面板,其中第一个面板将具有“FontStyle”和“Table Data”面板,第二个面板将具有“选择模式和选项”以及其他属性? 如果有更好的方法,请提出建议

代码如下:

class View extends JDialog {

    View(JFrame frame) {
        super(frame);
        this.setTitle("Settings");
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        this.setResizable(false);
        this.setModal(false);
        this.add(getPanelComponents());
        this.pack();
        this.setLocationRelativeTo(null);
    }

    JPanel getPanelComponents() {
        JPanel panel = new JPanel();
        panel.setBorder(new EmptyBorder(10, 10, 10, 10));
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        panel.add(borderPanels());
        panel.add(southPanel());

        return panel;
    }

    private JPanel borderPanels() {
        JPanel borderPanels = new JPanel(new GridLayout(2, 2, 10, 10)); 
        borderPanels.setBorder(
            new CompoundBorder(
                getTitledBorder("Table Properties", Color.GRAY), 
                new EmptyBorder(5, 10, 10, 10)
            )
        );

        JPanel fsPanel = new JPanel();
        fsPanel.setLayout(new BoxLayout(fsPanel, BoxLayout.Y_AXIS));
        fsPanel.setBorder(                
            new CompoundBorder(
                getTitledBorder("Font Style", Color.GRAY),
                new EmptyBorder(5, 10, 10, 10)
            )
        );
        JPanel fsSubPanel1 = new JPanel(new FlowLayout(FlowLayout.LEADING));
        JPanel fsSubPanel2 = new JPanel(new FlowLayout(FlowLayout.LEADING));
        JPanel fsSubPanel3 = new JPanel(new FlowLayout(FlowLayout.LEADING));
        JPanel fsSubPanel4 = new JPanel(new FlowLayout(FlowLayout.LEADING));
        fsSubPanel1.add(new JLabel("Font name: "));
        fsSubPanel1.add(new JComboBox(new Object[] {"Courier New"}));
        fsSubPanel2.add(new JCheckBox("Bold"));
        fsSubPanel2.add(Box.createHorizontalStrut(5));
        fsSubPanel2.add(new JLabel("Font size: "));
        fsSubPanel2.add(new JComboBox(new Object[] {10, 12, 14, 16}));
        fsSubPanel3.add(new JCheckBox("Italic"));
        fsSubPanel4.add(new JCheckBox("Underline"));
        fsPanel.add(fsSubPanel1);
        fsPanel.add(fsSubPanel2);   
        fsPanel.add(fsSubPanel3);
        fsPanel.add(fsSubPanel4);

        JPanel tdPanel = new JPanel();
        tdPanel.setBorder(                
            new CompoundBorder(
                getTitledBorder("Table Data", Color.GRAY),
                new EmptyBorder(5, 10, 10, 10)
            )
        );
        BoxLayout tdBoxLayout = new BoxLayout(tdPanel, BoxLayout.Y_AXIS);
        tdPanel.setLayout(tdBoxLayout);
        tdPanel.add(new JLabel("Show values as: "));
        tdPanel.add(new JRadioButton("Text"));
        tdPanel.add(new JRadioButton("Number"));
        tdPanel.add(new JRadioButton("Character"));

        JPanel smoPanel = new JPanel();
        smoPanel.setBorder(                
            new CompoundBorder(
                getTitledBorder("Selection Mode & Options", Color.GRAY),
                new EmptyBorder(5, 10, 10, 10)
            )
        );
        BoxLayout smoBoxLayout = new BoxLayout(smoPanel, BoxLayout.Y_AXIS);
        smoPanel.setLayout(smoBoxLayout);
        smoPanel.add(new JLabel("Selection Mode"));
        smoPanel.add(Box.createVerticalStrut(10));
        smoPanel.add(new JRadioButton("Single Selection"));
        smoPanel.add(new JRadioButton("Single Interval Selection"));
        smoPanel.add(new JRadioButton("Multiple Interval Selection"));
        smoPanel.add(Box.createVerticalStrut(10));
        smoPanel.add(new JLabel("Selection Options"));
        smoPanel.add(Box.createVerticalStrut(10));
        smoPanel.add(new JCheckBox("Row Selection"));
        smoPanel.add(new JCheckBox("Column Selection"));
        smoPanel.add(new JCheckBox("Cell Selection"));

        JPanel apPanel = new JPanel();
        apPanel.setLayout(new BoxLayout(apPanel, BoxLayout.Y_AXIS));
        apPanel.setPreferredSize(new Dimension(300, 100));
        apPanel.setBorder(                
            new CompoundBorder(
                getTitledBorder("Additional Properties", Color.GRAY),
                new EmptyBorder(5, 10, 10, 10)
            )
        );
        JPanel apSubPanel1 = new JPanel(new FlowLayout(FlowLayout.LEADING));
        JPanel apSubPanel2 = new JPanel(new FlowLayout(FlowLayout.LEADING));
        JPanel apSubPanel3 = new JPanel(new FlowLayout(FlowLayout.LEADING));
        JPanel apSubPanel4 = new JPanel(new FlowLayout(FlowLayout.LEADING));
        apSubPanel1.add(new JCheckBox());
        JButton colChooserTrue = new JButton();
        colChooserTrue.setPreferredSize(new Dimension(25, 25));
        colChooserTrue.setBackground(Color.red);
        apSubPanel1.add(colChooserTrue); 
        apSubPanel1.add(new JLabel("Paint cells where the value is true"));
        apSubPanel2.add(new JCheckBox());
        JButton colChooserFalse = new JButton();
        colChooserFalse.setPreferredSize(new Dimension(25, 25));
        colChooserFalse.setBackground(Color.blue);
        apSubPanel2.add(colChooserFalse); 
        apSubPanel2.add(new JLabel("Paint cells where the value is false"));
        apSubPanel3.add(new JCheckBox("Cell Editing"));
        apSubPanel4.add(new JCheckBox("Cell Tool Tip"));
        apPanel.add(apSubPanel1);
        apPanel.add(apSubPanel2);
        apPanel.add(apSubPanel3);
        apPanel.add(apSubPanel4);

        borderPanels.add(fsPanel);
        borderPanels.add(tdPanel);
        borderPanels.add(smoPanel);
        borderPanels.add(apPanel);

        return borderPanels;
    }

    private JPanel southPanel() {
        JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0));
        southPanel.setBorder(new EmptyBorder(10, 0, 0, 0));
        southPanel.add(new JButton("Save"));
        southPanel.add(Box.createHorizontalStrut(10));
        southPanel.add(new JButton("Close"));
        return southPanel;
    }

    private Border getTitledBorder(String title, Color color) {
        return BorderFactory.createTitledBorder(BorderFactory.createLineBorder(color), title);
    }

}
Spring布局的工作:

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.JFrame;
import java.awt.Container;
import javax.swing.JRadioButton;

public class DialogSettingsSpringExample {

    private static void createAndShowGUI() {

        JFrame frame = new JFrame("Settings");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container contentPane = frame.getContentPane();
        SpringLayout layout = new SpringLayout();
        contentPane.setLayout(layout);

        /* define Table Properties panel */
        JPanel pTableProperties = new JPanel();
        SpringLayout tpLayout = new SpringLayout();
        pTableProperties.setLayout(tpLayout);
        pTableProperties.setBorder(
            BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Table Properties"),
                BorderFactory.createEmptyBorder(0, 0, 0, 0)
            )
        );
        contentPane.add(pTableProperties);
        layout.putConstraint(SpringLayout.WEST, pTableProperties, 10, SpringLayout.WEST, contentPane);
        layout.putConstraint(SpringLayout.NORTH, pTableProperties, 10, SpringLayout.NORTH, contentPane);
        layout.putConstraint(SpringLayout.SOUTH, pTableProperties, -50, SpringLayout.SOUTH, contentPane);
        layout.putConstraint(SpringLayout.EAST, pTableProperties, -10, SpringLayout.EAST, contentPane);

        JButton btnClose = new JButton("Close");
        contentPane.add(btnClose);
        layout.putConstraint(SpringLayout.NORTH, btnClose, 10, SpringLayout.SOUTH, pTableProperties);
        layout.putConstraint(SpringLayout.SOUTH, btnClose, -10, SpringLayout.SOUTH, contentPane);
        layout.putConstraint(SpringLayout.EAST, btnClose, -12, SpringLayout.EAST, contentPane);

        JButton btnSave = new JButton("Save");
        contentPane.add(btnSave);
        layout.putConstraint(SpringLayout.NORTH, btnSave, 10, SpringLayout.SOUTH, pTableProperties);
        layout.putConstraint(SpringLayout.SOUTH, btnSave, -10, SpringLayout.SOUTH, contentPane);
        layout.putConstraint(SpringLayout.EAST, btnSave, -10, SpringLayout.WEST, btnClose);


        /* define Font Style panel */
        JPanel pFontStyle = new JPanel();
        SpringLayout tfsLayout = new SpringLayout();
        pFontStyle.setLayout(tfsLayout);
        pFontStyle.setBorder(
            BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Font Style"), 
                BorderFactory.createEmptyBorder(5, 5, 5, 5)
            )
        );
        pTableProperties.add(pFontStyle);
        tpLayout.putConstraint(SpringLayout.WEST, pFontStyle, 10, SpringLayout.WEST, pTableProperties);
        tpLayout.putConstraint(SpringLayout.NORTH, pFontStyle, 10, SpringLayout.NORTH, pTableProperties);
        tpLayout.putConstraint(SpringLayout.SOUTH, pFontStyle, 150, SpringLayout.NORTH, pTableProperties);
        tpLayout.putConstraint(SpringLayout.EAST, pFontStyle, 250, SpringLayout.WEST, pTableProperties);

        JLabel labelFontName = new JLabel("Font name:");
        pFontStyle.add(labelFontName);
        tfsLayout.putConstraint(SpringLayout.WEST, labelFontName, 5, SpringLayout.WEST, pFontStyle);
        tfsLayout.putConstraint(SpringLayout.NORTH, labelFontName, 5, SpringLayout.NORTH, pFontStyle);

        JComboBox cb = new JComboBox(new Object[]{"Consolas"});
        pFontStyle.add(cb);              
        tfsLayout.putConstraint(SpringLayout.WEST, cb, 10, SpringLayout.EAST, labelFontName);
        tfsLayout.putConstraint(SpringLayout.EAST, cb, -10, SpringLayout.EAST, pFontStyle);
        tfsLayout.putConstraint(SpringLayout.NORTH, cb, 0, SpringLayout.NORTH, pFontStyle);

        JCheckBox cbxBold = new JCheckBox("Bold");
        pFontStyle.add(cbxBold);
        tfsLayout.putConstraint(SpringLayout.WEST, cbxBold, -5, SpringLayout.WEST, labelFontName);
        tfsLayout.putConstraint(SpringLayout.NORTH, cbxBold, 5, SpringLayout.SOUTH, cb);

        JCheckBox cbxItalic = new JCheckBox("Italic");
        pFontStyle.add(cbxItalic);
        tfsLayout.putConstraint(SpringLayout.WEST, cbxItalic, 0, SpringLayout.WEST, cbxBold);
        tfsLayout.putConstraint(SpringLayout.NORTH, cbxItalic, 0, SpringLayout.SOUTH, cbxBold);

        JCheckBox cbxUnder = new JCheckBox("Underline");
        pFontStyle.add(cbxUnder);
        tfsLayout.putConstraint(SpringLayout.WEST, cbxUnder, 0, SpringLayout.WEST, cbxItalic);
        tfsLayout.putConstraint(SpringLayout.NORTH, cbxUnder, 0, SpringLayout.SOUTH, cbxItalic);

        JLabel labelFontSize = new JLabel("Font size:");
        pFontStyle.add(labelFontSize);
        tfsLayout.putConstraint(SpringLayout.WEST, labelFontSize, 55, SpringLayout.EAST, cbxBold);
        tfsLayout.putConstraint(SpringLayout.NORTH, labelFontSize, 10, SpringLayout.SOUTH, cb);

        JComboBox cbFontSize = new JComboBox(new Object[]{9, 10, 11, 12, 14, 16});
        pFontStyle.add(cbFontSize);
        tfsLayout.putConstraint(SpringLayout.EAST, cbFontSize, 0, SpringLayout.EAST, cb);
        tfsLayout.putConstraint(SpringLayout.WEST, cbFontSize, 10, SpringLayout.EAST, labelFontSize);
        tfsLayout.putConstraint(SpringLayout.NORTH, cbFontSize, 5, SpringLayout.SOUTH, cb);

        /* define Table Data panel */
        JPanel pTableData = new JPanel();
        SpringLayout tdLayout = new SpringLayout();
        pTableData.setLayout(tdLayout);
        pTableData.setBorder(
            BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Table Data"), 
                BorderFactory.createEmptyBorder(5, 5, 5, 5)
            )
        );
        pTableProperties.add(pTableData);
        tpLayout.putConstraint(SpringLayout.WEST, pTableData, 10, SpringLayout.EAST, pFontStyle);
        tpLayout.putConstraint(SpringLayout.NORTH, pTableData, 10, SpringLayout.NORTH, pTableProperties);
        tpLayout.putConstraint(SpringLayout.SOUTH, pTableData, 150, SpringLayout.NORTH, pTableProperties);
        tpLayout.putConstraint(SpringLayout.EAST, pTableData, -10, SpringLayout.EAST, pTableProperties);

        JLabel labelSVA = new JLabel("Show values as:");
        pTableData.add(labelSVA);
        tdLayout.putConstraint(SpringLayout.WEST, labelSVA, 5, SpringLayout.WEST, pTableData);
        tdLayout.putConstraint(SpringLayout.NORTH, labelSVA, 5, SpringLayout.NORTH, pTableData);

        JRadioButton rbText = new JRadioButton("Text");
        pTableData.add(rbText);
        tdLayout.putConstraint(SpringLayout.WEST, rbText, 15, SpringLayout.WEST, labelSVA);
        tdLayout.putConstraint(SpringLayout.NORTH, rbText, 5, SpringLayout.SOUTH, labelSVA);

        JRadioButton rbNumber = new JRadioButton("Number");
        pTableData.add(rbNumber);
        tdLayout.putConstraint(SpringLayout.WEST, rbNumber, 0, SpringLayout.WEST, rbText);
        tdLayout.putConstraint(SpringLayout.NORTH, rbNumber, 0, SpringLayout.SOUTH, rbText);

        JRadioButton rbCharacter = new JRadioButton("Character");
        pTableData.add(rbCharacter);
        tdLayout.putConstraint(SpringLayout.WEST, rbCharacter, 0, SpringLayout.WEST, rbNumber);
        tdLayout.putConstraint(SpringLayout.NORTH, rbCharacter, 0, SpringLayout.SOUTH, rbNumber);

        /* define Selection Mode & Options panel */
        JPanel pSMOPanel = new JPanel();
        SpringLayout smoLayout = new SpringLayout();
        pSMOPanel.setLayout(smoLayout);
        pSMOPanel.setBorder(
            BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Selection Mode & Options"), 
                BorderFactory.createEmptyBorder(5, 5, 5, 5)
            )
        );
        pTableProperties.add(pSMOPanel);
        tpLayout.putConstraint(SpringLayout.WEST, pSMOPanel, 0, SpringLayout.WEST, pFontStyle);
        tpLayout.putConstraint(SpringLayout.EAST, pSMOPanel, 0, SpringLayout.EAST, pFontStyle);
        tpLayout.putConstraint(SpringLayout.NORTH, pSMOPanel, 20, SpringLayout.SOUTH, pFontStyle);
        tpLayout.putConstraint(SpringLayout.SOUTH, pSMOPanel, -10, SpringLayout.SOUTH, pTableProperties);

        JLabel labelSM = new JLabel("Selection mode:");
        pSMOPanel.add(labelSM);
        smoLayout.putConstraint(SpringLayout.WEST, labelSM, 5, SpringLayout.WEST, pSMOPanel);
        smoLayout.putConstraint(SpringLayout.NORTH, labelSM, 5, SpringLayout.NORTH, pSMOPanel);

        JRadioButton rbSingleSelection = new JRadioButton("Single Selection");
        pSMOPanel.add(rbSingleSelection);
        smoLayout.putConstraint(SpringLayout.WEST, rbSingleSelection, 10, SpringLayout.WEST, labelSM);
        smoLayout.putConstraint(SpringLayout.NORTH, rbSingleSelection, 5, SpringLayout.SOUTH, labelSM);

        JRadioButton rbSingleIntervalSelection = new JRadioButton("Single Interval Selection");
        pSMOPanel.add(rbSingleIntervalSelection);
        smoLayout.putConstraint(SpringLayout.WEST, rbSingleIntervalSelection, 0, SpringLayout.WEST, rbSingleSelection);
        smoLayout.putConstraint(SpringLayout.NORTH, rbSingleIntervalSelection, 0, SpringLayout.SOUTH, rbSingleSelection);

        JRadioButton rbMultipleIntervalSelection = new JRadioButton("Multiple Interval Selection");
        pSMOPanel.add(rbMultipleIntervalSelection);
        smoLayout.putConstraint(SpringLayout.WEST, rbMultipleIntervalSelection, 0, SpringLayout.WEST, rbSingleIntervalSelection);
        smoLayout.putConstraint(SpringLayout.NORTH, rbMultipleIntervalSelection, 0, SpringLayout.SOUTH, rbSingleIntervalSelection);

        JLabel labelSO = new JLabel("Selection options:");
        pSMOPanel.add(labelSO);
        smoLayout.putConstraint(SpringLayout.WEST, labelSO, 5, SpringLayout.WEST, pSMOPanel);
        smoLayout.putConstraint(SpringLayout.NORTH, labelSO, 10, SpringLayout.SOUTH, rbMultipleIntervalSelection);

        JCheckBox cbxRowSelection = new JCheckBox("Row Selection");
        pSMOPanel.add(cbxRowSelection);
        smoLayout.putConstraint(SpringLayout.WEST, cbxRowSelection, 10, SpringLayout.WEST, labelSO);
        smoLayout.putConstraint(SpringLayout.NORTH, cbxRowSelection, 5, SpringLayout.SOUTH, labelSO);

        JCheckBox cbxColumnSelection = new JCheckBox("Column Selection");
        pSMOPanel.add(cbxColumnSelection);
        smoLayout.putConstraint(SpringLayout.WEST, cbxColumnSelection, 0, SpringLayout.WEST, cbxRowSelection);
        smoLayout.putConstraint(SpringLayout.NORTH, cbxColumnSelection, 0, SpringLayout.SOUTH, cbxRowSelection);

        JCheckBox cbxCellSelection = new JCheckBox("Cell Selection");
        pSMOPanel.add(cbxCellSelection);
        smoLayout.putConstraint(SpringLayout.WEST, cbxCellSelection, 0, SpringLayout.WEST, cbxColumnSelection);
        smoLayout.putConstraint(SpringLayout.NORTH, cbxCellSelection, 0, SpringLayout.SOUTH, cbxColumnSelection);

        /* define Additional Properties panel */
        JPanel pAddPropPanel = new JPanel();
        SpringLayout apLayout = new SpringLayout();
        pAddPropPanel.setLayout(apLayout);
        pAddPropPanel.setBorder(
            BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Additional Properties"), 
                BorderFactory.createEmptyBorder(5, 5, 5, 5)
            )
        );
        pTableProperties.add(pAddPropPanel);
        tpLayout.putConstraint(SpringLayout.WEST, pAddPropPanel, 0, SpringLayout.WEST, pTableData);
        tpLayout.putConstraint(SpringLayout.EAST, pAddPropPanel, 0, SpringLayout.EAST, pTableData);
        tpLayout.putConstraint(SpringLayout.NORTH, pAddPropPanel, 0, SpringLayout.NORTH, pSMOPanel);
        tpLayout.putConstraint(SpringLayout.SOUTH, pAddPropPanel, -10, SpringLayout.SOUTH, pTableProperties);

        JLabel labelPTC = new JLabel("Paint table cells where value is:");
        pAddPropPanel.add(labelPTC);
        apLayout.putConstraint(SpringLayout.WEST, labelPTC, 5, SpringLayout.WEST, pAddPropPanel);
        apLayout.putConstraint(SpringLayout.NORTH, labelPTC, 5, SpringLayout.NORTH, pAddPropPanel);

        JCheckBox cbxCellTrue = new JCheckBox();
        pAddPropPanel.add(cbxCellTrue);
        apLayout.putConstraint(SpringLayout.WEST, cbxCellTrue, 20, SpringLayout.WEST, labelPTC);
        apLayout.putConstraint(SpringLayout.NORTH, cbxCellTrue, 15, SpringLayout.SOUTH, labelPTC);

        JButton btnCellTrue = new JButton();
        pAddPropPanel.add(btnCellTrue);
        apLayout.putConstraint(SpringLayout.WEST, btnCellTrue, 5, SpringLayout.EAST, cbxCellTrue);
        apLayout.putConstraint(SpringLayout.EAST, btnCellTrue, 25, SpringLayout.EAST, cbxCellTrue);
        apLayout.putConstraint(SpringLayout.NORTH, btnCellTrue, 0, SpringLayout.NORTH, cbxCellTrue);
        apLayout.putConstraint(SpringLayout.SOUTH, btnCellTrue, 0, SpringLayout.SOUTH, cbxCellTrue);

        JLabel labelVTrue = new JLabel("True");
        pAddPropPanel.add(labelVTrue);
        apLayout.putConstraint(SpringLayout.WEST, labelVTrue, 10, SpringLayout.EAST, btnCellTrue);
        apLayout.putConstraint(SpringLayout.NORTH, labelVTrue, 2, SpringLayout.NORTH, cbxCellTrue);

        JCheckBox cbxCellFalse = new JCheckBox();
        pAddPropPanel.add(cbxCellFalse);
        apLayout.putConstraint(SpringLayout.WEST, cbxCellFalse, 0, SpringLayout.WEST, cbxCellTrue);
        apLayout.putConstraint(SpringLayout.NORTH, cbxCellFalse, 10, SpringLayout.SOUTH, cbxCellTrue);

        JButton btnCellFalse = new JButton();
        pAddPropPanel.add(btnCellFalse);
        apLayout.putConstraint(SpringLayout.WEST, btnCellFalse, 5, SpringLayout.EAST, cbxCellFalse);
        apLayout.putConstraint(SpringLayout.EAST, btnCellFalse, 25, SpringLayout.EAST, cbxCellFalse);
        apLayout.putConstraint(SpringLayout.NORTH, btnCellFalse, 0, SpringLayout.NORTH, cbxCellFalse);
        apLayout.putConstraint(SpringLayout.SOUTH, btnCellFalse, 0, SpringLayout.SOUTH, cbxCellFalse);

        JLabel labelVFalse = new JLabel("False");
        pAddPropPanel.add(labelVFalse);
        apLayout.putConstraint(SpringLayout.WEST, labelVFalse, 10, SpringLayout.EAST, btnCellFalse);
        apLayout.putConstraint(SpringLayout.NORTH, labelVFalse, 2, SpringLayout.NORTH, cbxCellFalse);

        JLabel labelATC = new JLabel("Allow table cells:");
        pAddPropPanel.add(labelATC);
        apLayout.putConstraint(SpringLayout.WEST, labelATC, 5, SpringLayout.WEST, pAddPropPanel);
        apLayout.putConstraint(SpringLayout.NORTH, labelATC, 20, SpringLayout.SOUTH, labelVFalse);

        JCheckBox cbxCellEditing = new JCheckBox("Cell Editing");
        pAddPropPanel.add(cbxCellEditing);
        apLayout.putConstraint(SpringLayout.WEST, cbxCellEditing, 20, SpringLayout.WEST, labelATC);
        apLayout.putConstraint(SpringLayout.NORTH, cbxCellEditing, 5, SpringLayout.SOUTH, labelATC);

        JCheckBox cbxCellToolTip = new JCheckBox("Cell Tool Tip");
        pAddPropPanel.add(cbxCellToolTip);
        apLayout.putConstraint(SpringLayout.WEST, cbxCellToolTip, 0, SpringLayout.WEST, cbxCellEditing);
        apLayout.putConstraint(SpringLayout.NORTH, cbxCellToolTip, 0, SpringLayout.SOUTH, cbxCellEditing);

        /* show frame */
        frame.pack();
        frame.setSize(550, 540);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}


看看SpringLayout()。此布局管理器允许您将组件的边缘(北、南、东和西)附加到其他组件的边缘。这还定义了调整大小行为。例如,以下语句将组合框的西边缘附加到标签的东边缘,两者之间有10个像素:

tfsLayout.putConstraint(SpringLayout.WEST, cb, 10, SpringLayout.EAST, labelFontName);
以下是使用SpringLayout实现表格属性和字体样式面板的示例程序:

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.JFrame;

import java.awt.Container;

public class TableProps {

    private static void createAndShowGUI() {

        JFrame frame = new JFrame("Settings");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container contentPane = frame.getContentPane();
        SpringLayout layout = new SpringLayout();
        contentPane.setLayout(layout);

        /* define Table Properties panel */
        JPanel pTableProperties = new JPanel();
        SpringLayout tpLayout = new SpringLayout();
        pTableProperties.setLayout(tpLayout);
        pTableProperties.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Table Properties"), BorderFactory.createEmptyBorder(5,5,5,5)));
        contentPane.add(pTableProperties);
        layout.putConstraint(SpringLayout.WEST, pTableProperties, 10, SpringLayout.WEST, contentPane);
        layout.putConstraint(SpringLayout.NORTH, pTableProperties, 10, SpringLayout.NORTH, contentPane);
        layout.putConstraint(SpringLayout.SOUTH, pTableProperties, -50, SpringLayout.SOUTH, contentPane);
        layout.putConstraint(SpringLayout.EAST, pTableProperties, -10, SpringLayout.EAST, contentPane);

        JButton btnClose = new JButton("Close");
        contentPane.add(btnClose);
        layout.putConstraint(SpringLayout.NORTH, btnClose, 10, SpringLayout.SOUTH, pTableProperties);
        layout.putConstraint(SpringLayout.SOUTH, btnClose, -10, SpringLayout.SOUTH, contentPane);
        layout.putConstraint(SpringLayout.EAST, btnClose, -12, SpringLayout.EAST, contentPane); 

        JButton btnSave = new JButton("Save");
        contentPane.add(btnSave);
        layout.putConstraint(SpringLayout.NORTH, btnSave, 10, SpringLayout.SOUTH, pTableProperties);
        layout.putConstraint(SpringLayout.SOUTH, btnSave, -10, SpringLayout.SOUTH, contentPane);
        layout.putConstraint(SpringLayout.EAST, btnSave, -10, SpringLayout.WEST, btnClose);


        /* define Font Style panel */
        JPanel pFontStyle = new JPanel();
        SpringLayout tfsLayout = new SpringLayout();
        pFontStyle.setLayout(tfsLayout);
        pFontStyle.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Font Style"), BorderFactory.createEmptyBorder(5,5,5,5)));
        pTableProperties.add(pFontStyle);
        tpLayout.putConstraint(SpringLayout.WEST, pFontStyle, 20, SpringLayout.WEST, pTableProperties);
        tpLayout.putConstraint(SpringLayout.NORTH, pFontStyle, 20, SpringLayout.NORTH, pTableProperties);
        tpLayout.putConstraint(SpringLayout.SOUTH, pFontStyle, 160, SpringLayout.NORTH, pTableProperties);
        tpLayout.putConstraint(SpringLayout.EAST, pFontStyle, 250, SpringLayout.WEST, pTableProperties);        

        JLabel labelFontName = new JLabel("Font name:");
        pFontStyle.add(labelFontName);
        tfsLayout.putConstraint(SpringLayout.WEST, labelFontName, 5, SpringLayout.WEST, pFontStyle);
        tfsLayout.putConstraint(SpringLayout.NORTH, labelFontName, 5, SpringLayout.NORTH, pFontStyle);

        JComboBox<String> cb = new JComboBox<String>();
        pFontStyle.add(cb);
        tfsLayout.putConstraint(SpringLayout.WEST, cb, 10, SpringLayout.EAST, labelFontName);
        tfsLayout.putConstraint(SpringLayout.EAST, cb, -10, SpringLayout.EAST, pFontStyle);
        tfsLayout.putConstraint(SpringLayout.NORTH, cb, 3, SpringLayout.NORTH, pFontStyle);

        JCheckBox cbxBold = new JCheckBox("Bold");
        pFontStyle.add(cbxBold);
        tfsLayout.putConstraint(SpringLayout.WEST, cbxBold, -5, SpringLayout.WEST, labelFontName);
        tfsLayout.putConstraint(SpringLayout.NORTH, cbxBold, 2, SpringLayout.SOUTH, cb);

        JCheckBox cbxItalic = new JCheckBox("Italic");
        pFontStyle.add(cbxItalic);
        tfsLayout.putConstraint(SpringLayout.WEST, cbxItalic, 0, SpringLayout.WEST, cbxBold);
        tfsLayout.putConstraint(SpringLayout.NORTH, cbxItalic, 2, SpringLayout.SOUTH, cbxBold);

        JCheckBox cbxUnder = new JCheckBox("Underline");
        pFontStyle.add(cbxUnder);
        tfsLayout.putConstraint(SpringLayout.WEST, cbxUnder, 0, SpringLayout.WEST, cbxItalic);
        tfsLayout.putConstraint(SpringLayout.NORTH, cbxUnder, 2, SpringLayout.SOUTH, cbxItalic);        

        JLabel labelFontSize = new JLabel("Font size:");
        pFontStyle.add(labelFontSize);
        tfsLayout.putConstraint(SpringLayout.WEST, labelFontSize, 20, SpringLayout.EAST, cbxBold);
        tfsLayout.putConstraint(SpringLayout.NORTH, labelFontSize, 10, SpringLayout.SOUTH, cb);

        JComboBox<String> cbFontSize = new JComboBox<String>();
        pFontStyle.add(cbFontSize);
        tfsLayout.putConstraint(SpringLayout.EAST, cbFontSize, 0, SpringLayout.EAST, cb);
        tfsLayout.putConstraint(SpringLayout.WEST, cbFontSize, 10, SpringLayout.EAST, labelFontSize);
        tfsLayout.putConstraint(SpringLayout.NORTH, cbFontSize, 5, SpringLayout.SOUTH, cb);

        /* define Table Data panel */
        JPanel pTableData = new JPanel();
        SpringLayout tdLayout = new SpringLayout();
        pTableData.setLayout(tdLayout);
        pTableData.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Table Data"), BorderFactory.createEmptyBorder(5,5,5,5)));
        pTableProperties.add(pTableData);
        tpLayout.putConstraint(SpringLayout.WEST, pTableData, 20, SpringLayout.EAST, pFontStyle);
        tpLayout.putConstraint(SpringLayout.NORTH, pTableData, 20, SpringLayout.NORTH, pTableProperties);
        tpLayout.putConstraint(SpringLayout.SOUTH, pTableData, 160, SpringLayout.NORTH, pTableProperties);
        tpLayout.putConstraint(SpringLayout.EAST, pTableData, -20, SpringLayout.EAST, pTableProperties);    
        /* ADD OTHER WIDGETS */

        /* define Selection Mode & Options panel */
        JPanel pSMOPanel = new JPanel();
        SpringLayout smoLayout = new SpringLayout();
        pSMOPanel.setLayout(smoLayout);
        pSMOPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Selection Mode & Options"), BorderFactory.createEmptyBorder(5,5,5,5)));
        pTableProperties.add(pSMOPanel);
        tpLayout.putConstraint(SpringLayout.WEST, pSMOPanel, 0, SpringLayout.WEST, pFontStyle);
        tpLayout.putConstraint(SpringLayout.EAST, pSMOPanel, 0, SpringLayout.EAST, pFontStyle);
        tpLayout.putConstraint(SpringLayout.NORTH, pSMOPanel, 20, SpringLayout.SOUTH, pFontStyle);
        tpLayout.putConstraint(SpringLayout.SOUTH, pSMOPanel, -10, SpringLayout.SOUTH, pTableProperties);
        /* ADD OTHER WIDGETS */

        /* define Additional Preferences panel */
        JPanel pAddPrefPanel = new JPanel();
        SpringLayout apLayout = new SpringLayout();
        pAddPrefPanel.setLayout(apLayout);
        pAddPrefPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Additional Preferences"), BorderFactory.createEmptyBorder(5,5,5,5)));
        pTableProperties.add(pAddPrefPanel);
        tpLayout.putConstraint(SpringLayout.WEST, pAddPrefPanel, 0, SpringLayout.WEST, pTableData);
        tpLayout.putConstraint(SpringLayout.EAST, pAddPrefPanel, 0, SpringLayout.EAST, pTableData);
        tpLayout.putConstraint(SpringLayout.NORTH, pAddPrefPanel, 0, SpringLayout.NORTH, pSMOPanel);
        tpLayout.putConstraint(SpringLayout.SOUTH, pAddPrefPanel, -10, SpringLayout.SOUTH, pTableProperties);       
        /* ADD OTHER WIDGETS */


        /* show frame */
        frame.pack();
        frame.setSize(550,475);
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
import javax.swing.BorderFactory;
导入javax.swing.JButton;
导入javax.swing.JCheckBox;
导入javax.swing.JComboBox;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.SpringLayout;
导入javax.swing.JFrame;
导入java.awt.Container;
公共类道具{
私有静态void createAndShowGUI(){
JFrame=新JFrame(“设置”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
容器contentPane=frame.getContentPane();
SpringLayout布局=新的SpringLayout();
contentPane.setLayout(布局);
/*“定义表属性”面板*/
JPanel pTableProperties=新的JPanel();
SpringLayout tpLayout=新的SpringLayout();
pTableProperties.setLayout(tpLayout);
pTableProperties.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitleBorder(“表属性”),BorderFactory.createEmptyBorder(5,5,5,5));
contentPane.add(pTableProperties);
布局.putConstraint(SpringLayout.WEST,pTableProperties,10,SpringLayout.WEST,contentPane);
布局.putConstraint(SpringLayout.NORTH,pTableProperties,10,SpringLayout.NORTH,contentPane);
putConstraint(SpringLayout.SOUTH,pTableProperties,-50,SpringLayout.SOUTH,contentPane);
putConstraint(SpringLayout.EAST,pTableProperties,-10,SpringLayout.EAST,contentPane);
JButton btnClose=新JButton(“关闭”);
contentPane.add(btnClose);
布局.putConstraint(SpringLayout.NORTH、btnClose、10、SpringLayout.SOUTH、pTableProperties);
布局.putConstraint(SpringLayout.SOUTH,btnClose,-10,SpringLayout.SOUTH,contentPane);
布局.putConstraint(SpringLayout.EAST,btnClose,-12,SpringLayout.EAST,contentPane);
JButton btnSave=新JButton(“保存”);
contentPane.add(btnSave);
布局.putConstraint(SpringLayout.NORTH、btnSave、10、SpringLayout.SOUTH、pTableProperties);
putConstraint(SpringLayout.SOUTH,btnSave,-10,SpringLayout.SOUTH,contentPane);
布局.putConstraint(SpringLayout.EAST,btnSave,-10,SpringLayout.WEST,btnClose);
/*定义字体样式面板*/
JPanel pFontStyle=新的JPanel();
SpringLayout tfsLayout=新的SpringLayout();
pFontStyle.setLayout(tfsLayout);
pFontStyle.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitleBorder(“字体样式”),BorderFactory.createEmptyBorder(5,5,5,5));
pTableProperties.add(pFontStyle);
tpLayout.putConstraint(SpringLayout.WEST,pFontStyle,20,SpringLayout.WEST,pTableProperties);
tpLayout.putConstraint(SpringLayout.NORTH,pFontStyle,20,SpringLayout.NORTH,pTableProperties);
tpLayout.putConstraint(SpringLayout.SOUTH、pFontStyle、160、SpringLayout.NORTH、pTableProperties);
tpLayout.putConstraint(SpringLayout.EAST、pFontStyle、250、SpringLayout.WEST、pTableProperties);
JLabel labelFontName=新的JLabel(“字体名称:”);
添加(labelFontName);
tfsLayout.putConstraint(SpringLayout.WEST,labelFontName,5,SpringLayout.WEST,pFontStyle);
tfsLayout.putConstraint(SpringLayout.NORTH,labelFontName,5,SpringLayout.NORTH,pFontStyle);
JComboBox cb=新的JComboBox();
pFontStyle.add(cb);
tfsLayout.putConstraint(SpringLayout.WEST、cb、10、SpringLayout.EAST、labelFontName);
tfsLayout.putConstraint(SpringLayout.EAST,cb,-10,SpringLayout.EAST,pFontStyle);
tfsLayout.putConstraint(SpringLayout.NORTH,cb,3,SpringLayout.NORTH,pFontStyle);
JCheckBox cbxBold=新JCheckBox(“粗体”);
pFontStyle.add(cbxBold);
tfsLayout.putConstraint(SpringLayout.WEST,cbxBold,-5,SpringLayout.WEST,labelFontName);
tfsLayout.putConstraint(SpringLayout.NORTH,cbxBold,2,SpringLayout.SOUTH,cb);
JCheckBox cbxItalic=新的JCheckBox(“斜体”);
pFontStyle.add(cbxItalic);
tfsLayout.putConstraint(SpringLayout.WEST,cbxItalic,0,SpringLayout.WEST,cbxBold);
tfsLayout.putConstraint(SpringLayout.NORTH、cbxItalic、2、SpringLayout.SOUTH、cbxBold);
JCheckBox cbxUnder=新的JCheckBox(“下划线”);
pFontStyle.add(cbxUnder);
tfsLayout.putConstraint(SpringLayout.WEST,cbxUnder,0,SpringLayout.WEST,cbxItalic);
tfsLayout.putConstraint(SpringLayout.NORTH、cbxUnder、2、SpringLayout.SOUTH、cbxItalic);
JLabel labelFontSize=新的JLabel(“字体大小:”);
添加(labelFontSize);
tfsLayout.putConstraint(SpringLayout.WEST,labelFontSize,20,SpringLayout.EAST,cbxBold);
tfsLayout.putConstraint(SpringLayout.NORTH,labelFontSize,10,SpringLayout.SOUTH,cb);
JComboBox cbFontSize=新jcombo