cApple
cApple

Reputation: 311

GUI showing up blank

Here's my problem. I'm just trying to write a nice little application for a friend for her birthday. The problem is when I ran the program, I get a blank GUI initially. But If I adjust the borders of the window even the slightest bit, the problem appears up like it is supposed to.

This only happened after I put in the JTextArea. All that will do is display text. It won't be used for entering text. Any suggestions on what I am doing wrong? I'm I going over the bounds of the JFrame?

Thanks for any help.

import javax.swing.*;
import java.awt.*;

public class Birthday {

  private JFrame mainFrame;
  private JPanel mainPanel;
  private JPanel labelPanel;
  private JPanel buttonPanel;
  private JButton story;
  private JButton misc;
  private JButton next;
  private JLabel mainLabel;

  private JFrame miscFrame;
  private JPanel miscPanel;
  private JLabel miscLable;

  private JTextArea text;

  public Birthday(){
    gui();
  }

  public static void main(String [] args){
    Birthday b = new Birthday();

  }

  public void gui(){
    mainFrame = new JFrame("The Buttercup Project"); //main window
    mainFrame.setVisible(true);
    mainFrame.setSize(550,650);
    //mainFrame.setResizable(false);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    text = new JTextArea(30,35);

    mainPanel = new JPanel(); //displays content in window
    mainPanel.setBackground(Color.YELLOW);
    mainPanel.setVisible(true);
    mainPanel.add(text);

    labelPanel = new JPanel();
    labelPanel.setVisible(true);
    labelPanel.setBackground(Color.LIGHT_GRAY);

    buttonPanel = new JPanel();
    buttonPanel.setVisible(true);
    buttonPanel.setBackground(Color.LIGHT_GRAY);


    story = new JButton("Story"); //button
    misc = new JButton("?");
    next = new JButton ("Next Story");
    mainLabel = new JLabel("The Buttercup Project"); //label


    labelPanel.add(mainLabel); //adds buttons to panel
    buttonPanel.add(story, BorderLayout.CENTER);
    buttonPanel.add(misc, BorderLayout.CENTER);
    buttonPanel.add(next, BorderLayout.CENTER);

    mainFrame.add(labelPanel,BorderLayout.NORTH );
    mainFrame.add(buttonPanel, BorderLayout.AFTER_LAST_LINE);

    mainFrame.add(mainPanel,BorderLayout.CENTER );  //put panel inside of frame

  }

}

Upvotes: 3

Views: 3234

Answers (2)

Code-Apprentice
Code-Apprentice

Reputation: 83587

You do not need to call setVisible() on each panel. You only need to do it once for the JFrame which contains them; you should also do this after all the components are added. Also, be sure to call mainFrame.pack() so that the correct size will be calculated.

Upvotes: 4

Mike Nadaud
Mike Nadaud

Reputation: 106

Make one call to mainFrame.setVisible(true)at the end of the gui() method. Remove all other occurrences.

Upvotes: 4

Related Questions