运行java应用程序后创建并显示标签 [英] Create and show a label after running java application

查看:76
本文介绍了运行java应用程序后创建并显示标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been working on a problem for 2 days and it's going to be a headache for me! I use swing to create the GUI for my app. I want to add a label to my panel after running the code by clicking on the button, but I can't. Please help me to solve this problem. Much of this code is auto-generated by swing and it's not code I wrote.

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

}

推荐答案

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

You're trying adding to add to a panel using GroupLayout. Adding dynamically to that would be quite tricky (if possible), and I do not recommend that. I recommend adding a helper JPanel to the pane, and adding any new components to that helper panel.

编辑:

要清除使用的解决方案:使用表单编辑器添加了帮助 JPanel jPanel3 在下面的代码中)。表单设计器默认使用 GroupLayout 添加的面板,这将导致首先要解决的相同问题,因此帮助器的布局管理器面板已更改为 FlowLayout 。操作代码最终变得如此简单:

To clear the solution that was used: a helper JPanel was added using the form editor (jPanel3 in the code below). The form designer defaults to using GroupLayout for the added panel too, which would lead to the same problem that was to be solved in the first place, so the layout manager of the helper panel was changed to FlowLayout. The action code ends up being as simple as:

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();
}

这篇关于运行java应用程序后创建并显示标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆