如何在Java applet中居中GroupLayout的内容

时间:2011-06-17 09:42:54

标签: java swing applet grouplayout

我想在Java applet中集中GroupLayout的内容。经过长时间的研究,我仍然无法弄清楚如何去做。

下面的代码显示了我的applet。所有元素都以布局为中心,但布局不是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)
);

我会提供任何帮助。

2 个答案:

答案 0 :(得分:1)

重要的是:

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

截图

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

答案 1 :(得分:0)

您可以从Box工厂创建垂直和水平胶水元素,并将这些元素添加到布局的两侧。布局布局后,胶水会扩展并强制布局的其余部分进入中心。