Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.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 在使用GUI生成器创建的JPanel中添加方法_Java_Jpanel - Fatal编程技术网

Java 在使用GUI生成器创建的JPanel中添加方法

Java 在使用GUI生成器创建的JPanel中添加方法,java,jpanel,Java,Jpanel,我已经使用netbeans GUI builder创建了GUI。我想在按下JButton之后添加一个对象(让我们尝试使用button) 我在ActionListener中编写的非常简单的代码非常有效: button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { panel2.add(new JButton("X"));

我已经使用netbeans GUI builder创建了GUI。我想在按下JButton之后添加一个对象(让我们尝试使用button)

我在ActionListener中编写的非常简单的代码非常有效:

button1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            panel2.add(new JButton("X"));
            panel2.validate();
        }
    });
但是,在netbeans创建的Gui中,这不起作用:

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
 jPanel1.add(new JButton("X"));
 jPanel1.validate();
 System.out.print("aa"); 
 }
但它打印“aa”,所以动作监听器可以工作

这看起来像是一个类似于此处显示的问题:

但我甚至看不到约萨写的那个长方形


如果有任何建议,我将不胜感激。

在编辑器中创建一个占位符面板,然后将该面板添加到其中。我认为问题与布局等有关,因为某些布局要求在添加组件时显示一些布局数据(即,设置了add(c,…)中的第二个字段)。因此,使用GUI creator工具创建一个占位符JPanel,将其命名以便有对它的引用,然后将该组件添加到该面板中。给它一个布局,比如FlowLayout,它不需要布局数据。您可能还希望删除面板上的所有插图,这样就不会产生巨大的间距,这可能可以在UI编辑器中实现