我的GUI窗口没有显示任何内容

时间:2013-04-13 02:49:29

标签: java swing user-interface jframe

我正在尝试使用网格布局来创建GUI窗口。我添加了所有组件并进行编译但是当它运行时它没有显示任何内容。我正在尝试制作一个像这样分组和堆叠的简单布局。

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

这是我的代码

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();
    }
}

3 个答案:

答案 0 :(得分:2)

您没有向上面的类扩展的JFrame添加任何内容。您需要将组件添加到其层次结构最终通向顶级窗口的容器,如果愿意,则添加到this。换句话说,您的代码中没有add(someComponent)或功能相似的this.add(someComponent)方法调用。

  • 考虑将所有JPanel添加到单个JPanel
  • 考虑通过调用add(thatJPanel)将JPanel添加到您的类的JFrame实例。
  • 更好的做法是不扩展JFrame,只是在需要时创建一个,但这可能是另一次讨论的主题。

答案 1 :(得分:2)

setVisible (true)语句之前添加以下语句:

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

答案 2 :(得分:2)

您的JFrame中没有任何内容。这就是原因

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();
            }
        });

    }
}

考虑以下

  • 在GridLayout中,第一个参数是,第二个参数是
  • 切勿手动设置JFrame的大小。使用pack()方法来决定 大小
  • 使用SwingUtilities.InvokeLater()在另一个线程中运行GUI。