【问题标题】:Create and show a label after running java application运行 java 应用程序后创建并显示标签
【发布时间】:2013-08-15 17:44:12
【问题描述】:

我已经解决了 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

}

【问题讨论】:

    标签: java swing


    【解决方案1】:

    您正在尝试使用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();
    }
    

    【讨论】:

    • 我手动添加了一个面板并为其添加了标签。然后将面板添加到框架(this.getContentPane().add(jPanel2)),但它不起作用。
    • @user2686824 我的意思是在窗口中添加一个JPanel,与添加其他所有内容相同。然后它没有内容并默认为零大小。然后,您可以将所需的任何组件添加到原本不可见的面板中。
    • 或者,如果它只是您有时想要显示的一个组件。只需将其放在窗口中并调用setVisible(false)就可以了。一旦你想展示它,只需致电setVisible(true)
    • 不,我想向面板添加多个组件。亲爱的 Kiheru 我定义了一个新面板并将其添加到框架等,但它没有用,相信我...如果我错了,请说出您的方式并提供更多详细信息...
    • @user2686824 好的,然后使用辅助面板方法。在表单设计器中创建助手,就像您为窗口的其余部分所做的那样。并在动作监听器中执行panel2.add(jlabel2)。 (哦,检查表单设计器没有做一些愚蠢的事情,比如设置帮助面板的布局管理器;你应该在那里使用 GroupLayout 以外的东西。JPanels 默认使用 FLowLayout,这可能是也可能不是你想要的)。
    【解决方案2】:

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

    一切都变好了... :)

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

    【讨论】:

    • 在将新的JLabel 添加到这个已经可见的JPanel 之后,您仍然需要调用jPanel3.revalidate()。我怀疑,如果不使用相同的方法,您能否获得一致的结果。
    • @nlcE 是的,显然我们应该在添加任何组件后调用 revalidate()。
    【解决方案3】:

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

    • 当我们动态添加标签时,我们需要重新设置布局。
    • 所以我们需要复制 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();
      

      }

    【讨论】:

      猜你喜欢
      • 2022-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多