Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过另一个组件中的代码向GridBagLayout插入新行_Java_Swing - Fatal编程技术网

Java 通过另一个组件中的代码向GridBagLayout插入新行

Java 通过另一个组件中的代码向GridBagLayout插入新行,java,swing,Java,Swing,我有一个JFrame,其中一些控件与GridBagLayout一起定位,如下所示: setLayout(new GridBagLayout()); final GridBagConstraints c = new GridBagConstraints; ... this.add(control, c); ... final GridBagConstraints c = new GridBagConstraints; c.gridx = 0; c.gridy = 0; ... myframere

我有一个JFrame,其中一些控件与GridBagLayout一起定位,如下所示:

setLayout(new GridBagLayout());
final GridBagConstraints c = new GridBagConstraints;
...
this.add(control, c);
...
final GridBagConstraints c = new GridBagConstraints;
c.gridx = 0;
c.gridy = 0;
...
myframereference.add(mytextfield, c);
...
我想从一个按钮中插入一行,按代码显示textfield。有可能做到这一点吗?? 我没有对GridBagConstraints的引用,从现在起我会这样做:

setLayout(new GridBagLayout());
final GridBagConstraints c = new GridBagConstraints;
...
this.add(control, c);
...
final GridBagConstraints c = new GridBagConstraints;
c.gridx = 0;
c.gridy = 0;
...
myframereference.add(mytextfield, c);
...
它可以工作,我的文本字段顶部有一个新行,但当我指定正确的位置(gridx=0和gridy=2)时,所有布局都会中断

多谢各位

编辑:

有关更多信息,请参阅一个可运行的示例,我想在另一帧的标签1和标签3之间插入新行

package test;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LayoutExample extends JFrame {

    public LayoutExample()
    {
        super("Layout Example");
    }

    private void createAndDisplayGUI()
    {       
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        setLayout(new GridBagLayout());
        final GridBagConstraints c = new GridBagConstraints();

        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(2, 3, 2, 2);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.WEST;

        final JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridBagLayout());

        // Row 1
        final JLabel label1 = new JLabel("Label 1");
        label1.setHorizontalAlignment(SwingConstants.RIGHT);
        contentPane.add(label1, c);

        final JTextField textfield1 = new JTextField(8);
        c.gridx++;
        contentPane.add(textfield1, c);

        //------

        final JLabel label2 = new JLabel("Label 2");
        label2.setHorizontalAlignment(SwingConstants.RIGHT);
        c.gridx++;
        contentPane.add(label2, c);

        final JTextField textfield2 = new JTextField(8);
        c.gridx++;
        contentPane.add(textfield2, c);

        // Row 2
        final JLabel label3 = new JLabel("Label 3");
        label3.setHorizontalAlignment(SwingConstants.RIGHT);
        c.gridx = 0;
        c.gridy++;
        contentPane.add(label3, c);

        final JTextField textfield3 = new JTextField(8);
        c.gridx++;
        contentPane.add(textfield3, c);

        //------

        final JLabel label4 = new JLabel("Label 4");
        label4.setHorizontalAlignment(SwingConstants.RIGHT);
        c.gridx++;
        contentPane.add(label4, c);

        final JTextField textfield4 = new JTextField(8);
        c.gridx++;
        contentPane.add(textfield4, c);

        setContentPane(contentPane);
        pack();
        setLocationByPlatform(true);
        setVisible(true);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new LayoutExample().createAndDisplayGUI();
            }
        });
    }
}

“我没有提到GridBagConstraints”-你不需要;“所有布局中断”-脱离上下文的代码片段不会帮助我们解决您的问题。考虑提供一个说明你的问题的方法。这不是一个代码转储,而是您正在做的一个示例,它突出了您所遇到的问题。这将减少混乱和更好的响应谢谢你的回复@MadProgrammer,我刚刚用一个可运行的示例编辑了我的帖子,我想从另一个框架在label1和label3之间插入一行。