我的JFrame不会打开

时间:2016-05-18 19:46:54

标签: java jframe

当我尝试在JGrasp或Eclipse上运行代码时,弹出没有JFrame。编码很新但我认为我做的一切都很正确。

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

public class test {

    private JFrame f;
    private JPanel p;
    private JButton b1;
    private JLabel lab;

    public void test()
    {

        gui();

    }

    public void gui()

    {

        f = new JFrame("This is the title of the JFrame");
        f.setVisible(true);
        f.setSize(600,400);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        p = new JPanel();
        p.setBackground(Color.CYAN);

        b1 = new JButton("This is the JButton");
        lab = new JLabel("This is the JLabel");

        p.add(b1);
        p.add(lab);

        f.add(p);

    }

    public static void main (String args []){

        new test();

    }


}

2 个答案:

答案 0 :(得分:2)

我同意whiskeyspider,但你的构造函数不应该有返回类型。 public void test()应更改为public test()

答案 1 :(得分:0)

最后致电setVisible(true)。请参阅How To Make Frames

f.pack();
f.setVisible(true);
相关问题