简体   繁体   中英

Make labels visible in code

I'm just starting in Java and i'm stuck on something really annoying.
I'm using NetBeans IDE and i'm building a JFrame using drag and drop. I added two labels to my frame and renamed them (using the WYSIWYG interface designer ).

Now I want to go in the code and change the names of the labels but they are not visible (in the code). Any idea why ? (I usually program in C# and with the designer the label are automatically visible in the code)


Update :

I get the following error when I try to refer to the label.

在此输入图像描述

In the code they are declared in the initComponents() method generated by Netbeans.

If I understood you correctly, you are just looking for the JLabel variable names? If so, this is the place they would be located -- not sure if Netbeans allows you to change anything in the initComponents() method though. You might have to do it in the GUI editor by right clicking the label and setting the properties as you like there.

EDIT

That is happening because you are likely doing this in main which is a static method. I would personally create a JPanel for the labels. For example, lets say the labels are 'Titles'. So I would create a TitlePanel class to hold these JLabels (with whatever layout you'd like).

This way you can reference the class from your main and add it like so:

TitlePanel panel = new TitlePanel();
panel.setFirstTitle("test");    //this would be a custom method where you get the label and set it inside there
frame.add(panel);

On another note though, it would be wiser to learn swing via the language rather than the GUI builder simply because you might not always want to use Netbeans as the IDE but may change your preference in the future.

默认情况下,它们通常被命名为jLabel1,jLabel2 ..但除非你以不同的方式告别它们(即在设计时更改了它们的名字),否则无法从如何访问它们的问题中分辨出来。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM