简体   繁体   English

我的GUI窗口未显示任何内容

[英]My GUI window doesn't show anything

I'm trying to use a grid layout to make a GUI window. 我正在尝试使用网格布局制作GUI窗口。 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. 您没有为上述类扩展的JFrame添加任何内容。 You need to add your components to containers whose hierarchy eventually leads to the top level window, to the this if you will. 您需要将您的组件添加到容器中,其层次最终导致顶层窗口,在this如果你愿意。 In other words, you have no add(someComponent) or the functionally similar this.add(someComponent) method call in your code above. 换句话说,在上面的代码中没有add(someComponent)或功能类似的this.add(someComponent)方法调用。

  • Consider adding all of your JPanels to a single JPanel 考虑将所有JPanels添加到单个JPanel中
  • Consider adding that JPanel to the JFrame instance that is your class by calling add(thatJPanel) . 考虑通过调用add(thatJPanel)将该JPanel添加到您的类的JFrame实例中。
  • 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. 更好的办法是不扩展JFrame并仅在需要时创建一个JFrame,但这可能是在另一时间进行另一次讨论的主题。

Before setVisible (true) statement add following statements: setVisible (true)语句之前,添加以下语句:

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

There is nothing in your JFrame. 您的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 在GridLayout中,第一个参数是Rows ,第二个参数是columns
  • Never set the size of JFrame manually. 切勿手动设置JFrame的大小。 Use pack() method to decide the size 使用pack()方法确定大小
  • Use SwingUtilities.InvokeLater() to run the GUI in another thread. 使用SwingUtilities.InvokeLater()在另一个线程中运行GUI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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