如何在 Java 小程序中将 GroupLayout 的内容居中

发布于 2024-11-16 01:17:10 字数 2298 浏览 3 评论 0原文

我想将 GroupLayout 的内容集中在 Java 小程序中。经过长时间的研究,我仍然不知道该怎么做。

下面的代码显示了我的小程序。所有元素都以布局为中心,但布局不以小程序为中心。

JPanel cp=new JPanel();

String[] s = new String[2];
s[0] = "Price";
s[1] = "Name";

JComboBox c = new JComboBox(s);

JProgressBar pb=new JProgressBar(17, 23);
pb.setValue(20);
pb.setStringPainted(true);

JLabel l=new JLabel("Name of product");

JButton b=new JButton("Send a message");
b.setEnabled(true);

cp.add(c);
cp.add(pb);
cp.add(l);
cp.add(b);

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

layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);

layout.setHorizontalGroup(
   layout.createSequentialGroup()
      .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
           .addComponent(c,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE)
           .addComponent(pb,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE)
           .addComponent(l,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE)
           .addComponent(b,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE))
);
layout.setVerticalGroup(
   layout.createSequentialGroup()
      .addComponent(c,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE)
      .addComponent(pb,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE)
      .addComponent(l,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE)
      .addComponent(b,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE)
);

我会感谢任何帮助。

I would like to center content of a GroupLayout in Java applet. After long research I still cannot figure out how to do it.

Code below shows my applet. All elements are centered to layout but the layout is not center to applet.

JPanel cp=new JPanel();

String[] s = new String[2];
s[0] = "Price";
s[1] = "Name";

JComboBox c = new JComboBox(s);

JProgressBar pb=new JProgressBar(17, 23);
pb.setValue(20);
pb.setStringPainted(true);

JLabel l=new JLabel("Name of product");

JButton b=new JButton("Send a message");
b.setEnabled(true);

cp.add(c);
cp.add(pb);
cp.add(l);
cp.add(b);

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

layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);

layout.setHorizontalGroup(
   layout.createSequentialGroup()
      .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
           .addComponent(c,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE)
           .addComponent(pb,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE)
           .addComponent(l,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE)
           .addComponent(b,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE))
);
layout.setVerticalGroup(
   layout.createSequentialGroup()
      .addComponent(c,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE)
      .addComponent(pb,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE)
      .addComponent(l,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE)
      .addComponent(b,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE)
);

I will appriciate any help.

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

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

发布评论

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

评论(2

无声无音无过去 2024-11-23 01:17:10

重要的行是:

        Container cont = getContentPane();
        cont.setLayout(new GridBagLayout());
        add(cp);

Screenshot

Applet GUI

SSCCE

“完整”79 行代码是:

//<applet code='TestApplet' width='250' height='250'></applet>
import java.awt.*;
import javax.swing.*;

public class TestApplet extends JApplet {

    public void init() {
        JPanel cp=new JPanel();

        Container cont = getContentPane();
        cont.setLayout(new GridBagLayout());
        add(cp);

        String[] s = new String[2];
        s[0] = "Price";
        s[1] = "Name";

        JComboBox c = new JComboBox(s);

        JProgressBar pb=new JProgressBar(17, 23);
        pb.setValue(20);
        pb.setStringPainted(true);

        JLabel l=new JLabel("Name of product");

        JButton b=new JButton("Send a message");
        b.setEnabled(true);

        cp.add(c);
        cp.add(pb);
        cp.add(l);
        cp.add(b);

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

        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);

        layout.setHorizontalGroup(
           layout.createSequentialGroup()
              .addGroup(layout.createParallelGroup(
                  GroupLayout.Alignment.CENTER)
                   .addComponent(c,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE)
                   .addComponent(pb,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE)
                   .addComponent(l,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE)
                   .addComponent(b,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
           layout.createSequentialGroup()
              .addComponent(c,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE)
              .addComponent(pb,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE)
              .addComponent(l,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE)
              .addComponent(b,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE)
        );
    }
}

The important lines are:

        Container cont = getContentPane();
        cont.setLayout(new GridBagLayout());
        add(cp);

Screenshot

Applet GUI

SSCCE

The 'full' 79 line code is:

//<applet code='TestApplet' width='250' height='250'></applet>
import java.awt.*;
import javax.swing.*;

public class TestApplet extends JApplet {

    public void init() {
        JPanel cp=new JPanel();

        Container cont = getContentPane();
        cont.setLayout(new GridBagLayout());
        add(cp);

        String[] s = new String[2];
        s[0] = "Price";
        s[1] = "Name";

        JComboBox c = new JComboBox(s);

        JProgressBar pb=new JProgressBar(17, 23);
        pb.setValue(20);
        pb.setStringPainted(true);

        JLabel l=new JLabel("Name of product");

        JButton b=new JButton("Send a message");
        b.setEnabled(true);

        cp.add(c);
        cp.add(pb);
        cp.add(l);
        cp.add(b);

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

        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);

        layout.setHorizontalGroup(
           layout.createSequentialGroup()
              .addGroup(layout.createParallelGroup(
                  GroupLayout.Alignment.CENTER)
                   .addComponent(c,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE)
                   .addComponent(pb,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE)
                   .addComponent(l,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE)
                   .addComponent(b,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
           layout.createSequentialGroup()
              .addComponent(c,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE)
              .addComponent(pb,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE)
              .addComponent(l,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE)
              .addComponent(b,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE,
                                GroupLayout.PREFERRED_SIZE)
        );
    }
}
夏至、离别 2024-11-23 01:17:10

您可以从 Box 工厂创建垂直和水平 Glue 元素,并将它们添加到布局的两侧。当布局布置好后,胶水会膨胀并迫使布局的其余部分进入中心。

You can create vertical and horizontal Glue elements from the Box factory and add these to the sides of your layout. When the layout is set out, the glue expands and forces the rest of the layout into the center.

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