Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 文件编译正确,但JFrame不';不出现_Java_Swing_Jframe - Fatal编程技术网

Java 文件编译正确,但JFrame不';不出现

Java 文件编译正确,但JFrame不';不出现,java,swing,jframe,Java,Swing,Jframe,我一直在努力让这段代码正常工作,但我不知道它出了什么问题。我所展示的一切都表明要正确地声明JFrame,但我已经这样做了,它没有出现。这是我的密码: import javax.swing.*; import java.awt.*; public class test extends JFrame { private JFrame f; private JPanel p; private JButton b1; private JLabel lab; public voi

我一直在努力让这段代码正常工作,但我不知道它出了什么问题。我所展示的一切都表明要正确地声明JFrame,但我已经这样做了,它没有出现。这是我的密码:

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

public class test extends JFrame {

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

  public void test() {
    gui();
  }

  public void gui() {
    JFrame f = new JFrame("Frame");
    f.setBounds(30, 30, 700, 1000);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    f.setFocusable(true);

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

    b1 = new JButton("Button");
    lab = new JLabel("Label");

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

    f.add(p, BorderLayout.SOUTH);
  }

  public static void main(String[] args) {
    new test();
  }
}

我对编码理解不够,无法诊断问题,所以我来这里寻求帮助。提前谢谢你

这是因为您没有调用
test()
方法。似乎您的意图是使此方法成为构造函数:

  public void test() {
    gui();
  }
它应该是(构造函数没有返回类型):


这是因为您没有调用
test()
方法。似乎您的意图是使此方法成为构造函数:

  public void test() {
    gui();
  }
它应该是(构造函数没有返回类型):

这是一个简单的错误。 您需要创建该类的实例并调用方法gui()。 您应该将测试重命名为test。这是最佳实践

这是一个简单的错误。 您需要创建该类的实例并调用方法gui()。 您应该将测试重命名为test。这是最佳实践


我制作了一个测试对象并调用了testmethod,该方法调用的是
gui()在其中

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

public class test extends JFrame {

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

    public void test() {
      gui();
    }

    public void gui() {
        JFrame f = new JFrame("Frame");
        f.setBounds(30, 30, 700, 1000);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        f.setFocusable(true);

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

        b1 = new JButton("Button");
        lab = new JLabel("Label");

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

        f.add(p, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        test t1 =  new test();
        t1.test();
    }
  }

我制作了一个测试对象并调用了testmethod,它调用
gui()在其中

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

public class test extends JFrame {

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

    public void test() {
      gui();
    }

    public void gui() {
        JFrame f = new JFrame("Frame");
        f.setBounds(30, 30, 700, 1000);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        f.setFocusable(true);

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

        b1 = new JButton("Button");
        lab = new JLabel("Label");

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

        f.add(p, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        test t1 =  new test();
        t1.test();
    }
  }
newtest()不会触发任何事件。从
public void test(){
中删除void您没有定义构造函数
new test();
不会触发任何事件。从
public void test()中删除void{
您没有定义构造函数