JLabel doesn't show up
Set a layout
for your panel
. Per example :
loginpanel.setLayout(new BorderLayout());
You can learn more about layouts here.
Here's what I get :
JLabel is not showing up?
The reason the label does not show up is that the content pane of a JFrame
uses a BorderLayout
as its LayoutManager
. When you call l.add(me)
and then l.add(p)
you are effectively replacing the Container
instance with the button. Try changing you Container
to a JPanel
, add both label and button components to that and then add it to your frame's content pane. Read up on Layout Managers, too.
The following example shows a JPanel
containing a JLabel
and JButton
arranged using the JPanel's
default LayoutManager
of FlowLayout
.
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel content = new JPanel();
content.add(new JLabel("A Label"));
content.add(new JButton("A Button"));
frame.add(content);
frame.pack();
frame.setVisible(true);
JLabels not showing up
Since you posted a lot of code and I'm not sure what were you trying to achieve, I modified your code adding 3 JLabel
s at the topPane
. And 3 JRadioButtons
(I didn't add the ButtonGroup
) below on a second JPanel
, I commented how to make them appear on a vertical and horizontal align.
Something you should take into account is:
Don't extend and create objects from
JFrame
(One or the other, not both, I recommend you to create objects).You were giving your
JPanel
a Layout after adding components to it, it should be done before.From the above point, you were also giving your Layout to your
JLabel
not yourJPanel
.You were adding a
JList
into aJLabel
.You missed to have a class constructor too.
Don't have multiple
JFrames
for more see The use of multiple JFrames, Good / Bad practiceNext time post a code which has no dependencies such as your
Truck
,Car
andMotorcycle
classes (i.e. a Runnable example). And use plain text instead so we can copy-paste the code and see the issue. Also try posting images (or the link and we can edit to add it).
Now, the outpus of my own program are:
And it was done with the following code.
import javax.swing.*;
import java.awt.*;
public class GUIExample {
JFrame frame;
JLabel label1, label2, label3;
JPanel topPane, radioPane;
JRadioButton radio1, radio2, radio3;
public static void main(String[] args) {
new GUIExample();
}
GUIExample () {
frame = new JFrame();
topPane = new JPanel();
radioPane = new JPanel();
topPane.setLayout(new FlowLayout());
// radioPane.setLayout(new BoxLayout(radioPane, BoxLayout.PAGE_AXIS)); //Vertical align
radioPane.setLayout(new FlowLayout()); //Horizontal align
label1 = new JLabel("Car");
label2 = new JLabel("Motorcycle");
label3 = new JLabel("Truck");
radio1 = new JRadioButton("Radio1");
radio2 = new JRadioButton("Radio2");
radio3 = new JRadioButton("Radio3");
topPane.add(label1);
topPane.add(label2);
topPane.add(label3);
radioPane.add(radio1);
radioPane.add(radio2);
radioPane.add(radio3);
frame.add(topPane, BorderLayout.PAGE_START);
frame.add(radioPane, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
JLabel not showing in JFrame
You are adding Components to an already visible JFrame
. Either add the Components before calling setVisible
(preferred - you may also want to call pack
to lay out the components as well)
add(lbl);
pack();
setVisible(true);
or call revalidate
on the JFrame after adding the Component(s)
setVisible(true);
add(lbl);
revalidate();
Related Topics
Java Httpservletrequest Get Url in Browsers Url Bar
Reflections - Java 8 - Invalid Constant Type
Matrix Multiplication in Apache Spark
Create Java Runtime Image on One Platform for Another Using Jlink
Displaying Fancy Equations with Java
Why Does the New Java 8 Date Time API Not Have Nanosecond Precision
Annotation to Make a Private Method Public Only for Test Classes
Can Hibernate Work with MySQL's "On Duplicate Key Update" Syntax
The Matching Wildcard Is Strict, But No Declaration Can Be Found for Element 'Context:Component-Scan
Why Does Main Method in Java Always Need Arguments
Found Shared References to a Collection Org.Hibernate.Hibernateexception
Pass Array to Oracle Procedure
Change Database Schema Used by Spring Boot
Exception Without Stack Trace in Java
How to Remove Element from Arraylist by Checking Its Value
How to Improve the Performance of G.Drawimage() Method for Resizing Images