好的,所以这解决了我对使用布局管理器和为组件组添加边框的大部分担忧:
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
public class TestingGround extends JFrame {
private JTextField tLenUnit, tLenResult, tAreUnit, tAreResult;
private JComboBox<String> cLenInUnit, cLenOutUnit, cAreInUnit, cAreOutUnit;
private JPanel lenMicro, lenMicro1, lenMicro2, lenNormal, lenMacro, area, volume;
private Border bGreyLine, bTitled1, bTitled2;
private TestingGround() {
setTitle("Testing Ground for an Application");
setVisible(true);
setResizable(true);
setLocationRelativeTo(null);
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
lenMicro = new JPanel();
lenMicro.setLayout(new GridBagLayout());
lenMicro1 = new JPanel();
lenMicro1.setLayout(new GridBagLayout());
bGreyLine = BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1, true);
bTitled1 = BorderFactory.createTitledBorder(bGreyLine, "Length", TitledBorder.LEFT, TitledBorder.TOP);
lenMicro1.setBorder(bTitled1);
tLenUnit = new JTextField("0.0");
tLenUnit.setColumns(10);
lenMicro1.add(tLenUnit, new GBC(0, 0, 2, 1).setAnchor(GBC.WEST).setInsets(5, 5, 5, 5));
cLenInUnit = new JComboBox<String>();
cLenInUnit.addItem("");
cLenInUnit.addItem("First");
cLenInUnit.addItem("Second");
cLenInUnit.setSelectedIndex(0);
cLenInUnit.setPreferredSize(new Dimension(120, 25));
lenMicro1.add(cLenInUnit, new GBC(2, 0, 3, 1));
tLenResult = new JTextField("");
tLenResult.setColumns(10);
lenMicro1.add(tLenResult, new GBC(5, 0, 2, 1).setInsets(5, 5, 5, 5));
cLenOutUnit = new JComboBox<String>();
cLenOutUnit.addItem("First");
cLenOutUnit.addItem("Second");
cLenOutUnit.setSelectedIndex(1);
cLenOutUnit.setPreferredSize(new Dimension(120, 25));
lenMicro1.add(cLenOutUnit, new GBC(7, 0, 1, 1));
// Area part:
lenMicro2 = new JPanel();
lenMicro2.setLayout(new GridBagLayout());
bTitled2 = BorderFactory.createTitledBorder(bGreyLine, "Area", TitledBorder.LEFT, TitledBorder.TOP);
lenMicro2.setBorder(bTitled2);
tAreUnit = new JTextField("0.0");
tAreUnit.setColumns(10);
lenMicro2.add(tAreUnit, new GBC(0, 1, 2, 1).setAnchor(GBC.WEST).setInsets(5, 5, 5, 5));
cAreInUnit = new JComboBox<String>();
cAreInUnit.addItem("");
cAreInUnit.addItem("One sqm");
cAreInUnit.addItem("Two sqm");
cAreInUnit.setSelectedIndex(0);
cAreInUnit.setPreferredSize(new Dimension(120, 25));
lenMicro2.add(cAreInUnit, new GBC(2, 1, 3, 1));
tAreResult = new JTextField("");
tAreResult.setColumns(10);
lenMicro2.add(tAreResult, new GBC(5, 1, 2, 1).setInsets(5, 5, 5, 5));
cAreOutUnit = new JComboBox<String>();
cAreOutUnit.addItem("One sqm");
cAreOutUnit.addItem("Two sqm");
cAreOutUnit.setSelectedIndex(1);
cAreOutUnit.setPreferredSize(new Dimension(120, 25));
lenMicro2.add(cAreOutUnit, new GBC(7, 1, 1, 1));
// Joining all lenMicroX panels into one:
lenMicro.add(lenMicro1, new GBC(0, 0, 8, 1).setAnchor(GBC.FIRST_LINE_START).setInsets(5, 5, 5, 5).setIpad(10, 10));
lenMicro.add(lenMicro2, new GBC(0, 1, 8, 1).setAnchor(GBC.LINE_START).setInsets(5, 5, 5, 5).setIpad(10, 10));
volume = new JPanel();
volume.setLayout(null);
// Panel definition --begin:
JTabbedPane tPane = new JTabbedPane();
tPane.addTab("Length & Area", null, lenMicro, "Length & Area units");
tPane.addTab("Volume", null, volume, "Volume units");
add(tPane);
// Panel --end.
}
public static void main(String[] args) {
TestingGround app = new TestingGround();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.pack();
app.setVisible(true);
}
}
GridBagConstraints 辅助类取自 Cay Horstmann 和 Gary Cornell 的Core Java,可在此处找到:http: //www.horstmann.com/articles/GBC.java。
我设法从 Gagandeep 的示例中弄清楚,面板可以放在上面,我们称它为母面板。这样每个子面板都可以有自己的边框。
第二件事:我决定使用 GridBagLayout,因为在我的这个和其他项目中使用它似乎是最准确的。好吧,我仍然需要学习很多相关知识(例如,为了使组件与某些行对齐;-) [编辑:刚刚将setPreferredSize()添加到 JComboBoxes;使布局更好看;-)])。
谢谢大家的帮助和宝贵的提示!