zig*_*ggy 3 java user-interface swing border layout-manager
我是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);
}
Run Code Online (Sandbox Code Playgroud)
尝试将上半部分放在GridLayout1行和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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8438 次 |
| 最近记录: |