我对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);
}发布于 2013-01-24 04:54:32
尝试在GridLayout中制作1行2列的上部,将其添加到BorderLayout中作为中心,并将按钮添加为BorderLayout.BOTTOM。在上半部分,使用两个面板,一个用于左侧,一个用于右侧。
下面是显示这一点的一些代码(我无法真正复制/粘贴您的示例):
public class MW extends JFrame {
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
private static void createAndShowUI() {
MW x = new MW();
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(1, 2));
JPanel leftPanel = new JPanel();
JLabel srcLabel = new JLabel("Source record:");
leftPanel.add(srcLabel);
JPanel rightPanel = new JPanel();
JLabel dstLabel = new JLabel("Destination record:");
rightPanel.add(dstLabel);
mainPanel.add(leftPanel);
mainPanel.add(rightPanel);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
JButton okButton = new JButton("OK");
buttonPanel.add(okButton);
JButton cancelButton = new JButton("Cancel");
buttonPanel.add(cancelButton);
x.setSize(400, 400);
x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
x.add(mainPanel, BorderLayout.CENTER);
x.add(buttonPanel, BorderLayout.PAGE_END);
x.setLocationRelativeTo(null);
x.setVisible(true);
}
}发布于 2013-01-24 04:58:48
看看这张图片,我把能做到这一点的嵌套组件打包在一起

在顶层,垂直方框中的两个棕色部分。
然后上面的那个有一个网格或一个水平框,用蓝色显示。
然后每一个都有一个淡黄色的方框布局,中间有适当的间距。
接下来是容器,并在每个容器中使用适当的布局,最后一个组件进入容器内部。
有一些工具可以让这一切变得更容易。我很久以前用过NetBeans编辑器。然后,我使用了eclipse的IBM WSAD/RAD版本中的编辑器。现在,如果我需要进行这样的布局,我会使用MyEclipse中的马蒂斯编辑器。
编辑
刚刚注意到@DaDaDom建议了一些类似的外观,棕色级别。他喜欢上框在中心,按钮框在南的边框布局。这将会很好的工作。
发布于 2017-03-18 10:00:01
我也会使用Group Layout,因为这将帮助您对齐标签和文本框。
https://stackoverflow.com/questions/14489121
复制相似问题