GridBagLayout未按预期工作

时间:2015-04-02 18:12:26

标签: java swing layout-manager gridbaglayout

GridBagLayout未按预期工作。我无法找到我犯错误的地方。我知道在某个地方,我正在做一些愚蠢的错误,但找不到它。

这里我制作了3个按钮,4个文本字段和3个组合框,我希望使用GridBagLayout以正确的顺序对齐它们。

代码如下:

    GridBagConstraints c = new GridBagConstraints();
            JPanel secondPanel = new JPanel();
            f=new JFrame("Server Side");
            b=new JButton("3DES ALGO");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 1;
            c.gridy = 0;
            secondPanel.add(b, c);    
            b1=new JButton("Browse");
            c.gridx = 2;
            c.gridy = 0;
            secondPanel.add(b1, c);
                b2=new JButton("Color");
            c.gridx = 0;
            c.gridy = 0;
            secondPanel.add(b2, c);
            comboBox1 = new JComboBox(s);
            c.gridx = 0;
            c.gridy = 1;
            secondPanel.add(b, c);
                comboBox2 = new JComboBox(s);
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 1;
            c.gridy = 1;
            secondPanel.add(b, c);
                comboBox3 = new JComboBox(s);
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 2;
            c.gridy = 1;
            secondPanel.add(b, c);

            b10=new JButton("OK");

            tf=new TextField();
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 2;
            c.gridy = 1;
            secondPanel.add(tf, c);

            tf1=new TextField();
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 2;
            secondPanel.add(tf1, c);
                tf2=new TextField();
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 1;
            c.gridy = 2;
            secondPanel.add(tf2, c);
                tf3=new TextField();
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 2;
            c.gridy = 2;
            secondPanel.add(tf3, c);

            b.addActionListener(this);
            b1.addActionListener(this);
            b2.addActionListener(this);

            b.setBounds(200,0,330,50);
            b1.setBounds(500,100,90,30);
            b2.setBounds(0,0,70,30);

            Container content = f.getContentPane();
            content.setBackground(Color.RED);
            b.setForeground(Color.white);
            b.setBackground(Color.black);
            b1.setForeground(Color.white);
            b1.setBackground(Color.black);
            b2.setForeground(Color.white);
            b2.setBackground(Color.black);
 tf.setBackground(Color.CYAN);
            tf1.setBackground(Color.GRAY);
            tf1.setForeground(Color.white);

            tf2.setBackground(Color.GRAY);
            tf2.setForeground(Color.white);
                tf3.setBackground(Color.GRAY);
            tf3.setForeground(Color.white);

            f.add(b);
            f.add(b1);
            f.add(b2);


            secondPanel.setLayout(new GridBagLayout());
            secondPanel.add(comboBox1);
            secondPanel.add(comboBox2);
            secondPanel.add(comboBox3);
            f.add(tf);
            f.add(tf1);
            f.add(tf2);
            f.add(tf3);
                f.add(secondPanel);
             f.setLayout(new GridBagLayout());

            f.addWindowListener(new WindowEventListener());
            f.pack();

            f.setVisible(true);

2 个答案:

答案 0 :(得分:1)

网格基于0,因此基本代码为:

c.gridx = 0;
c.gridy = 0;
panel.add(b1, c);
c.gridx = 1;
panel.add(b2, c);
c.gridx = 2;
panel.add(b3, c);
c.gridx = 0;
c.gridy = 1;
panel.add(combo1, c)
c.gridx = 1;
panel.add(combo2, c);
...

答案 1 :(得分:1)

我非常确定您需要在将组件添加到secondPanel之前将布局设置为GridBagLayout。

我也看到你添加你的JComboBoxes没有任何约束,这几乎肯定不是你想要做的。

另外,我看到你正在调用setBounds。不要这样做。 LayoutManager的工作是设置子组件'边界,不是你的。

尽管文档并不尽可能清晰,但在使用GridBagConstraints.RELATIVEGridBagConstraints.REMAINDER时,GridBagConstraints更容易使用。 gridx和gridy默认设置为RELATIVE,可能是因为它通常是最简单的用例。

如果我先创建所有组件,然后将它们添加到布局中,我发现我的代码最容易阅读。

所以,如果你想把你的按钮和字段分成三行,你可以这样做:

b = new JButton("3DES ALGO");
b1 = new JButton("Browse");
b2 = new JButton("Color");

b.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);

comboBox1 = new JComboBox(s);
comboBox2 = new JComboBox(s);
comboBox3 = new JComboBox(s);

tf = new JTextField(10);
tf1 = new JTextField(10);
tf2 = new JTextField(10);
tf3 = new JTextField(10);

GridBagConstraints c = new GridBagConstraints();
JPanel secondPanel = new JPanel(new GridBagLayout());

c.fill = GridBagConstraints.HORIZONTAL;

// Components are easier to see and use if there is some space between them.
c.insets.right = 6;
c.insets.bottom = 6;

// At this point, c.gridx and c.gridy are both GridBagConstraints.RELATIVE.
// c.gridwidth and c.gridheight are 1.

secondPanel.add(b, c);
secondPanel.add(b1, c);
// A gridwidth of REMAINDER means "This is the last component on this row."
c.gridwidth = GridBagConstraints.REMAINDER;
secondPanel.add(b2, c);

// Since the last component was added with a width of REMAINDER, the next
// component is automatically on the next row, as long as gridx and gridy
// are still set to GridBagConstraints.RELATIVE.

c.gridwidth = 1;
secondPanel.add(tf, c);
secondPanel.add(tf1, c);
secondPanel.add(tf2, c);
c.gridwidth = GridBagConstraints.REMAINDER;
secondPanel.add(tf3, c);

c.gridwidth = 1;
secondPanel.add(comboBox1, c);
secondPanel.add(comboBox2, c);
c.gridwidth = GridBagConstraints.REMAINDER;
secondPanel.add(comboBox3, c);