Java Swing 中放置 JSeparator 后的间隙大小

发布于 2024-11-25 18:07:57 字数 1006 浏览 1 评论 0原文

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

        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

谢谢。

好的,这是根据上面发布的代码生成的窗口: 在此处输入图像描述

您可以看到 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)
            );  

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

Thank you.

Okay, this is the window generated from the code posted above:
enter image description here

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听风念你 2024-12-02 18:07:57

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

附录:评论您的 GroupLayout 面板的父级是另一个 JPanel,您提出了以下问题,

您知道如何在我的情况下实现此功能吗?

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

GroupPanel

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

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.

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?

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.

GroupPanel

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();
            }
        });
    }
}
终止放荡 2024-12-02 18:07:57

在垂直布局中,按以下方式添加分隔符:

addComponent(separator, GroupLayout.PREFERRED_SIZE,
             GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)

in the vertical layout, add the separator in the following way:

addComponent(separator, GroupLayout.PREFERRED_SIZE,
             GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文