javaGeek
javaGeek

Reputation: 314

JFrame issues in Java

Does anyone have any idea as to why I have to expand my JFrame and then shrink it back down to get my text and buttons to display? Here's an example of one of my JFrames I am having an issue with.

//method that develops 'WelcomeFrame'
public void WelcomeFrame()
{
  //creates new frame with 'choiceFrame' variable
  welcomeFrame = new JFrame("Welcome!");

  //sets 'welcomeFrame' to visible
  welcomeFrame.setVisible(true);

  //sets size of frame
  welcomeFrame.setSize(330,115);

  //frame will close when you hit close button
  welcomeFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  //displays frame in middle of page
  welcomeFrame.setLocationRelativeTo(null);

  //initializes labels and what they will display
  instructions = new JLabel("<html>Welcome to Moe's Original BBQ Application!<br>"
     +"Please click CONTINUE to proceed to the home page!</html>");

  //creates new panel, hold all components and displays them on frame
  JPanel welcomePanel = new JPanel();

  //adds label to panel
  welcomePanel.add(instructions);

  //sets panel background to light gray
  welcomePanel.setBackground(Color.lightGray);

  //sets 'continueButton' background/foreground to light gray
  continueButton.setBackground(Color.lightGray);
  continueButton.setForeground(Color.darkGray);

  //adds button to panel
  welcomePanel.add(continueButton);     

  //adds panel to frame   
  welcomeFrame.add(welcomePanel);  
}

Thanks

Upvotes: 0

Views: 68

Answers (1)

NESPowerGlove
NESPowerGlove

Reputation: 5496

You call setVisible() before your JFrame is finished being set up, try calling setVisible() when your JFrame is finished being set up completely. setVisible() will call a repaint, and repaint will paint anything that's already been added and is currently visible (which for you at the point you call it nothing but the JFrame itself). Resizing the frame causes another repaint event to be queued, which by then more visible components have been added to and will now be painted.

Upvotes: 2

Related Questions