java-GridBagLayout无法正常工作

我有一些生成此代码:

 

我希望3、4和5个JTextField位于旁边
JLabels,如1和2所示.
我的代码是一种尝试,但是上面的图像是结果.

谁能在我的代码中指出问题?

 

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

 public class example{  
       public static void main(String[] args){

        JDialog jd = new JDialog();
    jd.setTitle("Example");
    GridBagLayout gl = new GridBagLayout();
    final JTextField j1 = new JTextField("1");
    final JTextField j2 = new JTextField("2");
    final JTextField j3 = new JTextField("3");
    final JTextField j4 = new JTextField("4");
    final JTextField j5 = new JTextField("5");
    jd.setLayout(gl);
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1.0;
    c.weighty = 1.0;
    c.gridx = 0;
    c.gridy = 0;
    jd.add(new JLabel("TextField 1: "), c);
    c.gridx = 1;
    jd.add(j1,c);
    c.gridx = 0;
    c.gridy++;
    jd.add(new JLabel("TextField 2: "), c);
    c.gridx = 1;
    c.gridy=1;
    jd.add(j2,c);
    c.gridx = 0;
    c.gridy++;
    jd.add(new JLabel("TextField 3: "), c);
    c.gridx = 1;
    jd.add(j3);
    c.gridx = 0;
    c.gridy++;
    jd.add(new JLabel("TextField 4: "), c);
    c.gridx = 1;
    jd.add(j4);
    c.gridx = 0;
    c.gridy++;
    jd.add(new JLabel("TextField 5: "), c);
    c.gridx = 1;
    jd.add(j5);


    jd.pack();
    jd.setVisible(true);
       }
 } 

最佳答案

jd.add(j3);    // **** here
c.gridx = 0;
c.gridy++;
jd.add(new JLabel("TextField 4: "), c);
c.gridx = 1;
jd.add(j4);    // **** here
c.gridx = 0;
c.gridy++;
jd.add(new JLabel("TextField 5: "), c);
c.gridx = 1;
jd.add(j5);    // **** here

您没有在几个add(…)方法调用中使用GridBagConstraints.如果您不告诉它如何放置组件,则GridBagLayout将不起作用,并且解决方案与您期望的一样,在将组件添加到使用GBL的容器中时使用约束对象.

问题描述 ![图片说明](https://img-ask.csdn.net/upload/201810/27/1540615202_291812.png) 代码 package How; import java.awt.*; import javax.swing.*; public class bin_2 extends JFrame { JLabel name=new JLabel(" 用户名"); JLabel key=new JLabel(" 密码"); JTextField t_name=new JTextField(); JPasswordField t_key=new JPasswordField(); JTextArea jTextArea=new JTextArea("我现在不可编辑",10,30); JScrollPane jScrollPane=new JScrollPane(jTextArea); public bin_2 () { super("文本组件应用"); this.setLayout(new GridBagLayout()); this.setSize(500, 500); // this.setResizable(false); GridBagConstraints gridBagConstraints=new GridBagConstraints(); Container container=this.getContentPane(); myadd(name, container, gridBagConstraints, 0, 0, 1, 1); myadd(t_name, container, gridBagConstraints, 0, 1, 1, 1); myadd(key, container, gridBagConstraints, 1, 0, 1, 1); myadd(t_key, container, gridBagConstraints, 1, 1,1,1); myadd(jScrollPane, container, gridBagConstraints, 2, 0, 10, 4); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { bin_2 how=new bin_2(); } public void myadd(Component c, Container con, GridBagConstraints gridBagConstraints,int row, int col, int width,int height) { gridBagConstraints.fill=GridBagConstraints.HORIZONTAL; gridBagConstraints.anchor=GridBagConstraints.CENTER; if(c==jScrollPane) { gridBagConstraints.fill=GridBagConstraints.BOTH; } gridBagConstraints.gridx=col; gridBagConstraints.gridy=row; gridBagConstraints.gridwidth=width; gridBagConstraints.gridheight=height; gridBagConstraints.weightx=1; gridBagConstraints.weighty=1; con.add(c,gridBagConstraints); } }
©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页