• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Want to display components on a frame after running programm immediately  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have designed one frame which code given below. When i run the program, it does not show label,button immediately.for that i have to maximize the screen or enhance the size of frame(output screen) then it shows labels,buttons.
Can anybody give me solution why this happen and what i have to do?

class loginframe extends JFrame
{
JButton b1;

loginframe(String s)
{
super(s);
setSize(500,500);
setVisible(true);
JPanel p=new JPanel();
p.setLayout(null);

JLabel l1=new JLabel("Welcome in Student Record");
l1.setBounds(150,250,700,50);

l1.setFont (new Font ("ITALIC", Font.BOLD, 30));

b1=new JButton("Login");
b1.setBounds(200,350,70,20);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(this);


p.add(l1);
p.add(b1);
add(p);
}


public static void main(String[] args)
{
loginframe obj=new loginframe("Student Registry");
}
}
 
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's because you call setVisible(true) before you add any components. Make setVisible(true) your last line in the constructor.
 
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to set a layout to your panel. But in your code, you have set null
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!