java 如何从 netbeans 创建动态 JLabel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13216217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:54:45  来源:igfitidea点击:

how to create dynamic JLabel from netbeans

javaswingnetbeans-7

提问by lakshman

I have public static variable in a JFrame. I have created a JLabel in another JFrame. I want to set the JLabel text to that variable. when I create the JLabel from netbeans, it create autogenerated code that can't be changed. I used netbeans 7.2.

我在 JFrame 中有公共静态变量。我在另一个 JFrame 中创建了一个 JLabel。我想将 JLabel 文本设置为该变量。当我从 netbeans 创建 JLabel 时,它会创建无法更改的自动生成的代码。我使用了 netbeans 7.2。

I can change the JLabel using myJLabel.setText(JFrame.variableName);. but the problem is auto generated code doesn't allow me to edit above code snippet.

我可以使用myJLabel.setText(JFrame.variableName);. 但问题是自动生成的代码不允许我编辑上面的代码片段。

I would like to set the text to variable name by setting in property panel rather changing above code.

我想通过在属性面板中设置而不是更改上面的代码来将文本设置为变量名称。

Is there a way to set the dynamic text using property panel?

有没有办法使用属性面板设置动态文本?

P.S. - I noticed in property panel, we can set jLabel value from existing component but these components reside in same JFrame. My variable reside in another JFrame

P.S. - I noticed in property panel, we can set jLabel value from existing component but these components reside in same JFrame. My variable reside in another JFrame

回答by MadProgrammer

You have two choices that I can see.

我可以看到你有两个选择。

One, you simple set the text of the label after the call to initComponent

一、你简单的将调用后的标签文本设置为 initComponent

Or...

或者...

  • Click the label in question
  • Click the "Code" button on the properties sheet
  • Click the "..." button against "Post-Creation Code"
  • Enter the code you like to executed, something like myJLabel.setText(JFrame.variableName);. Remember, this code is inserted inline, so it must be well formatted and compilable
  • 单击相关标签
  • 单击属性表上的“代码”按钮
  • 单击“创建后代码”旁边的“...”按钮
  • 输入您喜欢执行的代码,例如myJLabel.setText(JFrame.variableName);. 请记住,此代码是内联插入的,因此必须格式正确且可编译

enter image description here

在此处输入图片说明

Which then produces something like...

然后产生类似......

private void initComponents() {    
    jLabel1 = new javax.swing.JLabel();
    jLabel1.setText("Hello");

    setLayout(new java.awt.GridBagLayout());
    add(jLabel1, new java.awt.GridBagConstraints());
}// </editor-fold>

NB- Make sure you clear the default text from the label ;)

注意-确保清除标签中的默认文本;)

回答by Stephane Grenier

Anything beyond the most basic of UI's, especially dynamic code, will require that you stop using the auto-generated features of almost any and all IDE's.

除了最基本的 UI 之外的任何东西,尤其是动态代码,都要求您停止使用几乎所有 IDE 的自动生成功能。