第二个JButton和第三个JButton重叠  
代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class test2  extends JApplet //implements ActionListener,ItemListener
{
JButton bt1=new JButton("K"),
bt2=new JButton("安全"),
bt3=new JButton("设置");

JFrame jf=null; public test2()
{
jf=new JFrame("QQ用户登录");
Container cp=jf.getContentPane();

GridBagLayout grid=new GridBagLayout();
GridBagConstraints c=null;
int gridx,gridy,gridwidth,gridheight,anchor,fill,ipadx,ipady;
double weightx,weighty;
Insets inset;

cp.setLayout(grid);

gridx=0;
gridy=0;
gridwidth=4;
gridheight=1;
weightx=0;
weighty=0;
anchor=GridBagConstraints.NORTH;
fill=GridBagConstraints.NONE;
inset = new Insets(0,0,0,0); 
ipadx=10;
ipady=10;

c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
            weightx,weighty,anchor,fill,inset,ipadx,ipady);
grid.setConstraints(bt3, c);
cp.add(bt3);

gridy=1;
gridheight=2;
c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
            weightx,weighty,anchor,fill,inset,ipadx,ipady);
grid.setConstraints(bt1, c);
cp.add(bt1);

gridy=2;
gridheight=1;
c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
            weightx,weighty,anchor,fill,inset,ipadx,ipady);
grid.setConstraints(bt2, c);
cp.add(bt2);

jf.setBounds(300,400,400,300);
jf.setVisible(true);}public static void main(String[]args)
{
new test2(); }}

解决方案 »

  1.   

    个人认为最好的布局就是没有布局,JPanel都设为setLayout(null)
    在通过每个component的setBounds(new Rectangle(int x, int y, int width, int height))方法来设置其位置、大小。非常的好用
    不过最好是要有一个Visual Editor配合使用
      

  2.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class test2 extends JApplet // implements ActionListener,ItemListener
    {
        JButton bt1 = new JButton("K"), bt2 = new JButton("security"), bt3 = new JButton("set");    JFrame jf = null;    public test2() {
            jf = new JFrame("login");
            JPanel panel = new JPanel();
            GridBagLayout grid = new GridBagLayout();
            GridBagConstraints c = null;
            int gridx, gridy, gridwidth, gridheight, anchor, fill, ipadx, ipady;
            double weightx, weighty;
            Insets inset;        panel.setLayout(grid);        gridx = 0;
            gridy = 0;
            gridwidth = 4;
            gridheight = 1;
            weightx = 0;
            weighty = 0;
            anchor = GridBagConstraints.CENTER;
            fill = GridBagConstraints.NONE;
            inset = new Insets(0, 0, 0, 0);
            ipadx = 10;
            ipady = 10;        c = new GridBagConstraints(
                gridx,
                gridy,
                gridwidth,
                gridheight,
                weightx,
                weighty,
                anchor,
                fill,
                inset,
                ipadx,
                ipady);
            grid.setConstraints(bt3, c);
            panel.add(bt3);        gridx = 1;
            gridy = 1;
            gridheight = 2;
            c = new GridBagConstraints(
                gridx,
                gridy,
                gridwidth,
                gridheight,
                weightx,
                weighty,
                anchor,
                fill,
                inset,
                ipadx,
                ipady);
            grid.setConstraints(bt1, c);
            panel.add(bt1);        gridy = 3;
            gridheight = 1;
            c = new GridBagConstraints(
                gridx,
                gridy,
                gridwidth,
                gridheight,
                weightx,
                weighty,
                anchor,
                fill,
                inset,
                ipadx,
                ipady);
            grid.setConstraints(bt2, c);
            panel.add(bt2);
            jf.setContentPane(panel);
            jf.setBounds(300, 400, 400, 300);
            jf.setVisible(true);
            jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }    public static void main(String[] args) {
            new test2();    }}
      

  3.   

    SAGEM_M() : 可能这gridwidth个设的太大了
    和gridwidth没有关系,我试着将gridheight改小后,就可以显示出来,但是问题是我想让bt1占的高度是bt3、bt2的两倍亚;zwgs1985(流氓狗) :为什么要使用面板?即便是用了,bt1组件的高度也不是bt2的两倍;如果将bt1组件的gridheight改为大于2的数,则bt1和bt2又会重叠;希望大家能说明原因,谢谢
      

  4.   

    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.WindowConstants;public class test2 extends JApplet // implements ActionListener,ItemListener
    {
        JButton bt1 = new JButton("K"), bt2 = new JButton("security"), bt3 = new JButton("set");    JFrame jf = null;    public test2() {
            jf = new JFrame("login");
            JPanel panel = new JPanel();
            GridBagLayout grid = new GridBagLayout();
            GridBagConstraints c = new GridBagConstraints();
            // int gridx, gridy, gridwidth, gridheight, anchor, fill, ipadx, ipady;
            // double weightx, weighty;
            // Insets inset;        panel.setLayout(grid);        c.gridx = 0;
            c.gridy = 0;
            c.weightx = 1.0;
            c.weighty = 1.0;
            // gridwidth = 4;
            // gridheight = 1;
            // weightx = 0;
            // weighty = 0;
            // anchor = GridBagConstraints.CENTER;
            c.fill = GridBagConstraints.VERTICAL;
            // inset = new Insets(0, 0, 0, 0);
            // ipadx = 10;
            // ipady = 10;        // c = new GridBagConstraints(
            // gridx,
            // gridy,
            // gridwidth,
            // gridheight,
            // weightx,
            // weighty,
            // anchor,
            // fill,
            // inset,
            // ipadx,
            // ipady);
            grid.setConstraints(bt3, c);
            panel.add(bt3);        c.weighty = 2.0;
            c.gridy = 1;
            c.gridheight = 2;
            // c = new GridBagConstraints(
            // gridx,
            // gridy,
            // gridwidth,
            // gridheight,
            // weightx,
            // weighty,
            // anchor,
            // fill,
            // inset,
            // ipadx,
            // ipady);
            grid.setConstraints(bt1, c);
            panel.add(bt1);        c.weighty = 1.0;
            c.gridy = 3;
            c.gridheight = 1;
            // c = new GridBagConstraints(
            // gridx,
            // gridy,
            // gridwidth,
            // gridheight,
            // weightx,
            // weighty,
            // anchor,
            // fill,
            // inset,
            // ipadx,
            // ipady);
            grid.setConstraints(bt2, c);
            panel.add(bt2);
            jf.setContentPane(panel);
            jf.setBounds(300, 400, 400, 300);
            jf.setVisible(true);
            jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }    public static void main(String[] args) {
            new test2();
        }}
      

  5.   

    c.fill = GridBagConstraints.VERTICAL;
    让控件能够在纵向上填满整个空间,
    如果不设置这个的话,可能控件没有办法调整高度
    只是可能,我也没深看底层的实现,自己看看吧
      

  6.   

    其实你把cp.add(bt1);后面的gridy=2改成3就可以了