gpt4 book ai didi

java - 在 Swing 面板上排列 Swing 组件

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:12:55 25 4
gpt4 key购买 nike

我是 Swing 的新手,但有点难以实现我想要做的事情。我想构建一个如下所示的屏幕:

enter image description here

我不确定哪种布局最适合这种屏幕。我尝试使用 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);
}

最佳答案

尝试在 GridLayout 中制作 1 行和 2 列的上部,将其添加到 BorderLayout 中作为 CENTER 并将按钮作为 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);
}
}

关于java - 在 Swing 面板上排列 Swing 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14489121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com