我想做一个像
这样的对话框
这是我到目前为止所做的事情
我正在处理的问题是“表格属性”面板所具有的网格布局。我目前不喜欢的是创建空的空间网格布局。 我可以以某种方式将这个面板调整到原型图片中面板的大小吗?
如果没有,我正在考虑摆脱“GridLayout”,而是将“BoxLayout”与“Y_AXIS”一起使用,然后使用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 Layout的工作:
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();
}
});
}
}
答案 0 :(得分:2)
看一下SpringLayout(http://docs.oracle.com/javase/tutorial/uiswing/layout/spring.html)。此布局管理器允许您将组件的边(北,南,东和西)附加到其他组件的边。这也定义了调整大小行为。例如,以下语句将组合框的西边缘附加到标签的东边缘,它们之间有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();
}
});
}
}