When I click a JButton
, I want a new Jlabel
to be created and to appear on a position on the GUI of my choice. Note that I am using the GUI builder in Netbeans, meaning it auto-generates the code for GUI components.
I tried seeing what the auto-generated code does when creating a label but it didn't work.
Is there any way to do this?
Without giving us any code the thing that should have more chance to work for you is the following:
Add the label from the builder with the regular way. Exactly as you added the other components. Switch to code and you will see somewhere like
JLabel label = new JLabel("some text");
Just right below this line make the label invisible (after it is being declared).
label.setVisible(false);
Now the label is there, in the position you want it. Now the only that is left is to show it - make it visible when the button is pressed. So...
button.addActionListener(e -> label.setVisible(true));
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.