将JSeparator放入Java Swing后的间隙大小 [英] Gap size after placing JSeparator in Java Swing

查看:115
本文介绍了将JSeparator放入Java Swing后的间隙大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java Swing中有一个简单的问题。我将代码简化为以下代码段。我不确定如何最小化水平JSeparator与下一个JTextField之间的间隙大小,因为当前代码在两者之间产生巨大差距。

I have a simple problem here in Java Swing. I simplified my code to the following snippet. I am not sure how I can minimize the gap size between the horizontal JSeparator with the next JTextField, as current code generates a huge gap between the two.

        GroupLayout layout = new GroupLayout(jPanel1);          
        jPanel1.setLayout(layout);

        layout.setHorizontalGroup(layout.createParallelGroup()
            .addGroup(layout.createSequentialGroup()
                  .addGroup(layout.createSequentialGroup()
                        .addComponent(button)
                      ))
                  .addComponent(jSeparator)
                  .addComponent(jTextField)
            );
        layout.setVerticalGroup(layout.createSequentialGroup()
                .addComponent(button)
                .addComponent(jSeparator)
                .addComponent(jTextField)
            );  

而且一般来说,如何将间隙大小控制为任何整数表示的值,而不是使用 addPreferredGap

And also in general, how can I control the gap size to any integer represented value, instead of using the addPreferredGap?

谢谢。

好的,这是从上面发布的代码生成的窗口:

Okay, this is the window generated from the code posted above:

你可以看到JSeparator和JTextField之间的空间很宽。

You can see the space between the JSeparator and JTextField is very wide.

推荐答案

如果没有您的 sscce ,则问题似乎出现在您未显示的代码中。可能涉及父容器的布局或 pack()。请注意, JFrame 的默认布局是 BorderLayout ;默认位置是 CENTER 。这是一个 sscce ,用于比较您的代码。

Absent your sscce, the problem appears to be in the code you don't show. The parent container's layout or pack() may be involved. Note that the default layout of JFrame is BorderLayout; the default position is CENTER. Here's an sscce with which to compare your code.

附录:评论您的 GroupLayout 面板的父级是另一个 JPanel ,您询问了以下内容,

Addendum: Commenting that the parent of your GroupLayout panel is another JPanel, you asked the following,


你知道如何在我的情况下完成这项工作吗?

Do you know how to make this work in my situation?

是的,给封闭的 JPanel 一个合适的布局,例如 GridLayout ,如下所示。在这方面,后者的行为与 JFrame BorderLayout.CENTER 非常相似。

Yes, give the enclosing JPanel a suitable layout, e.g. GridLayout as shown below. The latter behaves much like the BorderLayout.CENTER of the JFrame in this regard.

import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;

/** @see http://stackoverflow.com/questions/6769722 */
public class GroupPanel extends JPanel {

    private final JButton button = new JButton("Start");
    private final JSeparator jSeparator = new JSeparator();
    private final JTextField jTextField = new JTextField(10);

    public GroupPanel() {
        GroupLayout layout = new GroupLayout(this);          
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        layout.setHorizontalGroup(layout.createParallelGroup()
            .addComponent(button)
            .addComponent(jSeparator)
            .addComponent(jTextField)
        );
        layout.setVerticalGroup(layout.createSequentialGroup()
            .addComponent(button, GroupLayout.PREFERRED_SIZE,
                GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
            .addComponent(jSeparator, GroupLayout.PREFERRED_SIZE,
                GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
            .addComponent(jTextField, GroupLayout.PREFERRED_SIZE,
                GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        );
    }

    private static void display() {
        JFrame f = new JFrame("GroupPanel");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new GridLayout(1, 0));
        f.add(new GroupPanel());
        f.add(new GroupPanel());
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                display();
            }
        });
    }
}

这篇关于将JSeparator放入Java Swing后的间隙大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆