简体   繁体   中英

My GUI window doesn't show anything

I'm trying to use a grid layout to make a GUI window. I add all my components and it compiles but when it runs it doesn't show anything. I'm trying to make a simple layout grouped and stacked like this.

{introduction message}
{time label
time input text}
{gravity label
gravity input text}
{answer label
answer text box}
{calculate button clear button}

Here is my code

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

public class TurnerRandyFallingGUI  extends JFrame
{   
final int WINDOW_HEIGHT=500;
final int WINDOW_WIDTH=500;

public TurnerRandyFallingGUI()
{
    setTitle("Falling Distance Calculator");

    setSize(WINDOW_WIDTH,WINDOW_HEIGHT);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setLayout(new GridLayout(1, 5));

    //labels
    JLabel introMessage = new JLabel("Welcome to the Falling distance"+
                                       "calculator");
    JLabel timeLabel = new JLabel("Please enter the amount of time "+
                "in seconds the object was falling.");
    JLabel gravityLabel = new JLabel("Enter the amount of gravity being "+
                    "forced onto the object");
    JLabel answerLabel = new JLabel("Answer");

    //text fields
    JTextField fTime = new JTextField(10);
    JTextField gForce = new JTextField(10);
    JTextField answerT = new JTextField(10);

    //buttons
    JButton calculate = new JButton("Calculate");
    JButton clr = new JButton("clear");

    //panels
    JPanel introP = new JPanel();
    JPanel timeP = new JPanel();
    JPanel gravityP = new JPanel();
    JPanel answerP = new JPanel();
    JPanel buttonsP = new JPanel();

    //adding to the panels
    //intro panel
    introP.add(introMessage);
    //time panel
    timeP.add(timeLabel);
    timeP.add(fTime);
    //gravity panel
    gravityP.add(gravityLabel);
    gravityP.add(gForce);
    //answer panel
    answerP.add(answerLabel);
    answerP.add(answerT);
    //button panel
    buttonsP.add(calculate);
    buttonsP.add(clr);




    setVisible(true);
}
    public static void main(String[] args)
    {
        new TurnerRandyFallingGUI();
    }
}

You've added nothing to the JFrame that your class above extends. You need to add your components to containers whose hierarchy eventually leads to the top level window, to the this if you will. In other words, you have no add(someComponent) or the functionally similar this.add(someComponent) method call in your code above.

  • Consider adding all of your JPanels to a single JPanel
  • Consider adding that JPanel to the JFrame instance that is your class by calling add(thatJPanel) .
  • Even better would be to not extend JFrame and just to create one when needed, but that will likely be the subject of another discussion at another time.

Before setVisible (true) statement add following statements:

add (introP);
add (timeP);
add (gravityP);
add (answerP);
add (buttonsP);

There is nothing in your JFrame. That is the reason

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

public class TurnerRandyFallingGUI  extends JFrame
{   
final int WINDOW_HEIGHT=500;
final int WINDOW_WIDTH=500;

public TurnerRandyFallingGUI()
{


    //labels
    JLabel introMessage = new JLabel("Welcome to the Falling distance"+
                                       "calculator");
    JLabel timeLabel = new JLabel("Please enter the amount of time "+
                "in seconds the object was falling.");
    JLabel gravityLabel = new JLabel("Enter the amount of gravity being "+
                    "forced onto the object");
    JLabel answerLabel = new JLabel("Answer");

    //text fields
    JTextField fTime = new JTextField(10);
    JTextField gForce = new JTextField(10);
    JTextField answerT = new JTextField(10);

    //buttons
    JButton calculate = new JButton("Calculate");
    JButton clr = new JButton("clear");

    //panels
    JPanel introP = new JPanel();
    JPanel timeP = new JPanel();
    JPanel gravityP = new JPanel();
    JPanel answerP = new JPanel();
    JPanel buttonsP = new JPanel();

    //adding to the panels
    //intro panel
    introP.add(introMessage);
    //time panel
    timeP.add(timeLabel);
    timeP.add(fTime);
    //gravity panel
    gravityP.add(gravityLabel);
    gravityP.add(gForce);
    //answer panel
    answerP.add(answerLabel);
    answerP.add(answerT);
    //button panel
    buttonsP.add(calculate);
    buttonsP.add(clr);

    setLayout(new GridLayout(5, 1));

    this.add(introP);
    this.add(timeP);
    this.add(gravityP);
    this.add(answerP);
    this.add(buttonsP);


 setTitle("Falling Distance Calculator");

    this.pack();

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    setVisible(true);
    this.validate();
}
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TurnerRandyFallingGUI();
            }
        });

    }
}

Consider the following

  • In GridLayout, the first parameter is Rows , Second is columns
  • Never set the size of JFrame manually. Use pack() method to decide the size
  • Use SwingUtilities.InvokeLater() to run the GUI in another thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM