我是 Swing 的新手,但有点努力实现我想要做的事情。我想构建一个如下所示的屏幕:
我不确定哪种布局最适合这种屏幕。我尝试使用 BorderLayout 但它永远不会正确。我得到了右下角的按钮,但中间的组件被证明是相当具有挑战性的。我知道如何将组件添加到面板上,但我正在努力的领域是如何对齐它们。有时,如果我在带有 BORDERLAYOUT 的面板上添加一个文本字段,它会占用我不想要的整个面板的全部空间。
我设法使用 AbsoluteLayout 让它几乎可以工作,但我怀疑如果我希望屏幕流畅,这不是最好的选择。
setBounds(100, 100, 775, 599);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBackground(SystemColor.control);
contentPanel.setForeground(Color.RED);
contentPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(new BorderLayout(0, 0));
{
JPanel topHeadersPanel = new JPanel();
contentPanel.add(topHeadersPanel, BorderLayout.NORTH);
topHeadersPanel.setLayout(new BorderLayout(0, 0));
{
JLabel lblSetSourceRecord = new JLabel("Source customer record:");
lblSetSourceRecord.setFont(new Font("Tahoma", Font.BOLD, 12));
topHeadersPanel.add(lblSetSourceRecord, BorderLayout.WEST);
}
{
JLabel lblSetDestinationRecord = new JLabel("Destination customer record:");
lblSetDestinationRecord.setFont(new Font("Tahoma", Font.BOLD, 12));
topHeadersPanel.add(lblSetDestinationRecord, BorderLayout.EAST);
}
{
JLabel lblSetDestinationRecord = new JLabel("Source customer:");
lblSetDestinationRecord.setFont(new Font("Tahoma", Font.BOLD, 12));
topHeadersPanel.add(lblSetDestinationRecord, BorderLayout.SOUTH);
}
}
{
JPanel buttonPane = new JPanel();
buttonPane.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JPanel panel = new JPanel();
buttonPane.add(panel);
}
{
JButton okButton = new JButton("OK");
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}