简体   繁体   English

在Swing面板上安排摆动组件

[英]Arranging swing components on a Swing panel

I am new to Swing but kind of struggling to achieve what i am trying to do. 我是Swing的新手,但有点努力实现我想做的事情。 I would like to build a screen that looks like this: 我想构建一个如下所示的屏幕:

在此输入图像描述

I am not sure which layout is best for this kind of screen. 我不确定哪种布局最适合这种屏幕。 I tried using BorderLayout but it never comes out right. 我尝试使用BorderLayout但它永远不会出现。 I got the buttons at the bottom right but the components in the middle prove to be quite challenging. 我得到了右下方的按钮,但中间的组件证明是非常具有挑战性的。 I know how to add the components on to the panel but the area i am struggling is how to align them. 我知道如何将组件添加到面板上,但我正在努力的区域是如何对齐它们。 Sometimes if i add a text field on a panel with a BORDERLAYOUT, it takes up the full space of the whole panel which i dont want. 有时如果我在带有BORDERLAYOUT的面板上添加文本字段,它会占用我不想要的整个面板的整个空间。

I managed to get it to almost work using AbsoluteLayout but i suspect that is not the best option if i want the screen to be fluid. 我设法让它几乎使用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);
    }

Try making the upper part within a GridLayout with 1 row and 2 columns, add that in a BorderLayout as CENTER and the Buttons as BorderLayout.BOTTOM . 尝试在GridLayout使用1行和2列创建上部,在BorderLayout中将其添加为CENTER,将Buttons添加为BorderLayout.BOTTOM Within the upper part, use two panels, one for left, one for right. 在上部,使用两个面板,一个用于左侧,一个用于右侧。

And here is some code to show this (I couldn't really copy/paste your example): 以下是一些显示此代码的代码(我无法真正复制/粘贴您的示例):

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);
    }
}

Look at this picture where I boxed in the nested components that would do this 看看这张图片,我在其中嵌入了可以执行此操作的嵌套组件

在此输入图像描述

At the top level, two brown sections in a vertical box. 在顶层,垂直框中有两个棕色部分。

Then the upper one of those has either a grid or a horizontal box shown in blue. 然后其中上面的一个有蓝色的网格或水平框。

Then each of those has a yellowish box layout with appropriate spacing between then. 然后每个都有一个黄色的盒子布局,然后之间有适当的间距。

You next the containers and use the appropriate layouts in each and the final components go inside the container. 接下来是容器并在每个容器中使用适当的布局,最后的组件进入容器内部。

There are some tools that make this easier. 有一些工具可以使这更容易。 I used to use NetBeans editor long ago. 我很久以前就习惯使用NetBeans编辑器。 Then I used the editor in the IBM WSAD/RAD version of eclipse. 然后我在eclipse的IBM WSAD / RAD版本中使用了编辑器。 Now I use the Matisse editor in MyEclipse if I ever need to do such a layout. 现在我在MyEclipse中使用Matisse编辑器,如果我需要做这样的布局。

Edit 编辑

Just noticed @DaDaDom suggested something simlar on the outside, brown level. 刚刚注意到@DaDaDom在外面提出了类似于褐色的东西。 He likes a border layout with the upper box in the CENTER and the button box in the SOUTH. 他喜欢边框布局,中间的上方框和南方的按钮框。 That would work really well. 这将非常有效。

我也会使用Group Layout,因为它可以帮助您对齐标签和文本框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM