我想让一个文本框位于两个按钮下一行,并且占两列,但是结果是错的.
请帮我看一下,代码如下:
import java.awt.*;
import javax.swing.*;public class Mytest
{
public static void main(String[] args)
{
MytestFrame frame = new MytestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class MytestFrame extends JFrame
{
public MytestFrame()
{
setSize(400, 300);Container contentPane = getContentPane();
GridBagLayout layout = new GridBagLayout();
GridBagConstraints cons = new GridBagConstraints();contentPane.setLayout(layout);cons.gridx = 0;
cons.gridy = 0;
cons.gridwidth = 1;
contentPane.add(new JButton("Button1"), cons);cons.gridx = 1;
cons.gridy = 0;
contentPane.add(new JButton("Button2"), cons);

cons.gridx=0;
cons.gridy=1;
cons.gridwidth=2;
contentPane.add(new JTextField("Text"));}
}

解决方案 »

  1.   

    import java.awt.*;
    import javax.swing.*;public class Mytest
    {
    public static void main(String[] args)
    {
    MytestFrame frame = new MytestFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }
    }class MytestFrame extends JFrame
    {
    public MytestFrame()
    {
    setSize(400, 300);Container contentPane = getContentPane();
    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints cons = new GridBagConstraints();contentPane.setLayout(layout);cons.gridx = 0;
    cons.gridy = 0;
    cons.gridwidth = 1;
    contentPane.add(new JButton("Button1"), cons);cons.gridx = 1;
    cons.gridy = 0;
    contentPane.add(new JButton("Button2"), cons);cons.gridx=0;
    cons.gridy=1;
    cons.gridwidth=2;
    contentPane.add(new JTextField("Text",13),cons);}
    }
      

  2.   

    import java.awt.*;
    import javax.swing.*;public class Mytest
    {
    public static void main(String[] args)
    {
    MytestFrame frame = new MytestFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }
    }class MytestFrame extends JFrame
    {
    public MytestFrame()
    {
    setSize(400, 300);Container contentPane = getContentPane();
    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints cons = new GridBagConstraints();contentPane.setLayout(layout);cons.gridx = 0;
    cons.gridy = 0;
    cons.gridwidth = 1;
    contentPane.add(new JButton("Button1"), cons);cons.gridx = 1;
    cons.gridy = 0;
    contentPane.add(new JButton("Button2"), cons);cons.gridx=0;
    cons.gridy=1;
    cons.gridwidth=GridBagConstraints.CENTER;
    contentPane.add(new JTextField("Text"),cons);}
    }
      

  3.   

    首先给你一个建议:将布局写成一个函数,便于理解,更便于重复调用!
    GridVBagLayout gb;
    GridBagConstraints gbc;
    布局函数:public void addComponent(Component c,int row,int col,int nrow,int ncol)
    {
    gbc.gridx=col;
    gbc.gridy=row;gbc.gridwidth=ncol;
    gbc.gridheight=nrow;gb.setConstraints(c,gbc);
    add(c);
    }调用函数:addComponent(txtArea,0,0,4,1);
      

  4.   

    我知道,但是现在的问题是布局无法按我的想法安排.
    照我对书上对Constraints的各个数据成员的解释做的时候老是搞得乱七八糟
      

  5.   

    have a try~---------------------------------------------------------------------------
    import java.awt.*;
    import javax.swing.*;public class MyTest {
    public static void main(String[] args) {
    MytestFrame frame = new MytestFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }
    }class MytestFrame extends JFrame {
    public MytestFrame() {
    setSize(400, 300); Container contentPane = getContentPane();
    GridBagLayout layout = new GridBagLayout(); contentPane.setLayout(layout); contentPane.add(new JButton("Button1"), new GBC(0, 0).setFill(
    GBC.HORIZONTAL).setIpad(25, 0));
    contentPane.add(new JButton("Button2"), new GBC(1, 0).setFill(
    GBC.HORIZONTAL).setIpad(25, 0));
    contentPane.add(new JTextField("Text", 13), new GBC(0, 1, 2, 1)
    .setFill(GBC.HORIZONTAL));
    }
    }
      

  6.   

    GBC的-----------------------------------------------------------------------------------
    import java.awt.*;/**
     * This class simplifies the use of the GridBagConstraints class.
     */
    public class GBC extends GridBagConstraints {
    /**
     * Constructs a GBC with a given gridx and gridy position and all other grid
     * bag constraint values set to the default.
     * 
     * @param gridx
     *            the gridx position
     * @param gridy
     *            the gridy position
     */
    public GBC(int gridx, int gridy) {
    this.gridx = gridx;
    this.gridy = gridy;
    } /**
     * Constructs a GBC with given gridx, gridy, gridwidth, gridheight and all
     * other grid bag constraint values set to the default.
     * 
     * @param gridx
     *            the gridx position
     * @param gridy
     *            the gridy position
     * @param gridwidth
     *            the cell span in x-direction
     * @param gridheight
     *            the cell span in y-direction
     */
    public GBC(int gridx, int gridy, int gridwidth, int gridheight) {
    this.gridx = gridx;
    this.gridy = gridy;
    this.gridwidth = gridwidth;
    this.gridheight = gridheight;
    } /**
     * Sets the anchor.
     * 
     * @param anchor
     *            the anchor value
     * @return this object for further modification
     */
    public GBC setAnchor(int anchor) {
    this.anchor = anchor;
    return this;
    } /**
     * Sets the fill direction.
     * 
     * @param fill
     *            the fill direction
     * @return this object for further modification
     */
    public GBC setFill(int fill) {
    this.fill = fill;
    return this;
    } /**
     * Sets the cell weights.
     * 
     * @param weightx
     *            the cell weight in x-direction
     * @param weighty
     *            the cell weight in y-direction
     * @return this object for further modification
     */
    public GBC setWeight(double weightx, double weighty) {
    this.weightx = weightx;
    this.weighty = weighty;
    return this;
    } /**
     * Sets the insets of this cell.
     * 
     * @param distance
     *            the spacing to use in all directions
     * @return this object for further modification
     */
    public GBC setInsets(int distance) {
    this.insets = new Insets(distance, distance, distance, distance);
    return this;
    } /**
     * Sets the insets of this cell.
     * 
     * @param top
     *            the spacing to use on top
     * @param left
     *            the spacing to use to the left
     * @param bottom
     *            the spacing to use on the bottom
     * @param right
     *            the spacing to use to the right
     * @return this object for further modification
     */
    public GBC setInsets(int top, int left, int bottom, int right) {
    this.insets = new Insets(top, left, bottom, right);
    return this;
    } /**
     * Sets the internal padding
     * 
     * @param ipadx
     *            the internal padding in x-direction
     * @param ipady
     *            the internal padding in y-direction
     * @return this object for further modification
     */
    public GBC setIpad(int ipadx, int ipady) {
    this.ipadx = ipadx;
    this.ipady = ipady;
    return this;
    }
    }