Why are my items not showing up in JFrame?
Solution 1
but when i run it i get this:
You get an empty screen because you add the components to the frame after the frame is visible.
- As has already been suggested you need to use an appropriate layout manager. FlowLayout is the easiest to start with.
- invoke
setVisible(true)
AFTER adding the components to the frame.
So the code should be more like:
panel.add(...);
panel.add(...);
add(panel);
pack();
setVisible(true);
Solution 2
I agree to MadProgrammer's suggestions (+1)
Well, lets take a look at your program though
You actually have created a JFrame with components in it. Its working fine as well, but your question of "why are my items not showing up in the JFrame" is not because you did something wrong but because missed out something i.e. revalidate()
Try:
public static void main(String[] args){
FirstGUI a = new FirstGUI();
a.GUI();
a.revalidate();
}
I'm not saying this will give you perfect UI.. what I'm trying to say is this will help you understand the Swing better. Learn about Swing Layout managers and then work on your UI to have better results
revalidate(): This component and all parents above it are marked as needing to be laid out. This means the Layout Manager will try to realign the components. Often used after removing components. It is possible that some really sharp swing people may miss this. I would think that you will only know this if you are actually using Swing.
Solution 3
The default layout manager for JFrame
is BorderLayout
.
This means that your components are essentially all been added ontop of each other.
Try changing the layout manager to something like FlowLayout
(for example)...
Take a look at A Visual Guide to Layout Managers and Using Layout Managers for more details.
Also, avoid setSize
where possible, use Window#pack
instead
Update
I'd also like to introduce you to Initial Threads which should be used to launch your UI code...
Fouroh3
Updated on January 28, 2020Comments
-
Fouroh3 about 4 years
I'm fairly new to JFrame and I want to know why my items are not showing up on the window. I know i dont have a ActionHandler but I just want my textfield's to show up on my window. Here's my code:
import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; public class FirstGUI extends JFrame{ public void GUI(){ setTitle("Welcome"); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); setSize(600,600); JLabel title = new JLabel(); title.setText("Apple Inc. Member Login Port"); title.setFont(new Font("Arial", Font.PLAIN, 24)); JTextField login = new JTextField("Login",10); JPasswordField pass = new JPasswordField("Password"); add(title); add(login); add(pass); } public static void main(String[] args){ FirstGUI a = new FirstGUI(); a.GUI(); } }
but when i run it i get this:
-
Mika Sundland about 6 yearsPlease add an explanation of how this code works (by editing the answer).