6

我已经解决了2天的问题,这对我来说会很头疼!我使用 swing 为我的应用程序创建 GUI。我想在运行代码后通过单击按钮向我的面板添加一个标签,但我不能。请帮我解决这个问题。大部分代码是由 swing 自动生成的,而不是我编写的代码。

package javaapplication1;

import java.awt.Color;
import javax.swing.SwingConstants;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;

public class RandomWordGUI extends javax.swing.JFrame {

/** Creates new form RandomWordGUI */
public RandomWordGUI() {
    initComponents();
}


/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();
    jLabel1 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jButton1.setText("jButton1");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    jLabel1.setText("jLabel1");

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(155, 155, 155)
                    .addComponent(jButton1))
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(172, 172, 172)
                    .addComponent(jLabel1)))
            .addContainerGap(172, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
            .addGap(70, 70, 70)
            .addComponent(jLabel1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jButton1)
            .addGap(91, 91, 91))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(186, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
    );

    pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    JLabel jLabel2 = new JLabel();
    jLabel2.setText("this is a label");
    this.jPanel1.add(jLabel2);
    this.jPanel1.repaint();
    this.jPanel1.revalidate();


}                                        

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {


        public void run() {
            new RandomWordGUI().setVisible(true);


        }
        RandomWordGUI randWord=new RandomWordGUI();



    });
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
// End of variables declaration

}

4

3 回答 3

2

您正在尝试添加以使用GroupLayout. 动态添加会非常棘手(如果可能的话),我不建议这样做。我建议向JPanel窗格添加一个助手,并将任何新组件添加到该助手面板。

编辑:

要清除使用的解决方案:JPanel使用表单编辑器添加了一个帮助程序(jPanel3在下面的代码中)。表单设计器也默认使用GroupLayout添加的面板,这会导致与最初要解决的问题相同,因此将辅助面板的布局管理器更改为FlowLayout. 操作代码最终很简单:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    JLabel jLabel2 = new JLabel();
    jLabel2.setText("this is a label");
    this.jPanel3.add(jLabel2);
    this.jPanel3.revalidate();
}
于 2013-08-15T17:51:01.340 回答
1

我在我的窗口中输入了另一个面板 - jPanel3- 并将 jLabel2 添加到其中...然后将此代码添加到 ActionEvent 的内容中 jPanel3.setLayout(new FlowLayout());

一切都变得好起来...... :)

感谢我亲爱的朋友@Kiheru的帮助和指导。

于 2013-08-15T19:56:26.313 回答
0

最后,我的朋友这是代码。我们需要的是

  • 当我们动态添加标签时,我们需要再次设置布局。
  • 所以我们需要复制 IDE 创建的布局
  • 手动输入水平组和垂直组

.addComponent(jLabel2)

.addGap()

  • 您的位置位于屏幕底部,因此我建议您更改位置以获得更好的视图
  • 请注意间隙和定位..
  • 只需将代码粘贴到 actionPerformed 函数中就可以了

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    
    JLabel jLabel2 = new JLabel();
    jLabel2.setText("this is a label");
    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(155, 155, 155)
                    .addComponent(jButton1))
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(172, 172, 172)
                    .addComponent(jLabel2)
                    .addGap(1,1,1)
                    .addComponent(jLabel1)))
            .addContainerGap(172, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
            .addGap(70, 70, 70)
            .addComponent(jLabel1)
            ***.addGap(1,1,1)
            .addComponent(jLabel2)***
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jButton1)
            .addGap(91, 91, 91))
    );
    
    this.jPanel1.repaint();
    this.jPanel1.revalidate();
    

    }

于 2013-08-15T19:03:14.377 回答