Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.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 未显示回转组件_Java_Swing_Jframe_Jpanel - Fatal编程技术网

Java 未显示回转组件

Java 未显示回转组件,java,swing,jframe,jpanel,Java,Swing,Jframe,Jpanel,这是我的代码,当我执行时,我得到的只是一个空白窗口。首先,当我尝试执行main.setContentPane()时,出现以下错误: “无法对非静态字段面板进行静态引用” 所以我把它放在构造器中,但什么也看不出来 import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.s

这是我的代码,当我执行时,我得到的只是一个空白窗口。首先,当我尝试执行
main.setContentPane()
时,出现以下错误:

“无法对非静态字段面板进行静态引用”

所以我把它放在构造器中,但什么也看不出来

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainFrame extends JFrame{
    private JPanel panel;
    private JButton performance;
    private JButton concordance;
    private JButton discordance;
    private JButton resultat;

    public void MainFrame() {
        panel = new JPanel(new GridLayout(4, 1, 10, 10));
        performance = new JButton("performance");
        concordance = new JButton("concordance");
        discordance = new JButton("discordance");
        resultat = new JButton("resultat");

        performance.setSize(50, 30);
        concordance.setSize(50, 30);
        discordance.setSize(50, 30);
        resultat.setSize(50, 30);

        panel.add(performance);
        panel.add(concordance);
        panel.add(discordance);
        panel.add(resultat);
        getContentPane().add(panel);

    }
    public static void main(String[] args) {
        MainFrame main = new MainFrame();
        main.setSize(300, 200);
        main.setDefaultCloseOperation(EXIT_ON_CLOSE);
        main.setVisible(true);
    }
}
您有一个名为MainFrame的方法(返回void),该方法从未被调用

您应该将该方法设置为构造函数,或者创建一个调用该方法的构造函数,如下所示:

public class MainFrame extends JFrame {
  private JPanel panel;
  private JButton performance;
  private JButton concordance;
  private JButton discordance;
  private JButton resultat;

  public MainFrame() {
    super();
    MainFrame();
  }

  public void MainFrame() {
    panel = new JPanel(new GridLayout(4, 1, 10, 10));
    performance = new JButton("performance");
    concordance = new JButton("concordance");
    discordance = new JButton("discordance");
    resultat = new JButton("resultat");

    performance.setSize(50, 30);
    concordance.setSize(50, 30);
    discordance.setSize(50, 30);
    resultat.setSize(50, 30);

    panel.add(performance);
    panel.add(concordance);
    panel.add(discordance);
    panel.add(resultat);
    this.getContentPane().add(panel);

  }

  public static void main(String[] args) {
    MainFrame main = new MainFrame();
    main.setSize(300, 200);
    main.setDefaultCloseOperation(EXIT_ON_CLOSE);
    main.setVisible(true);
    main.MainFrame();
  }
}

哇,我完全没有注意到上面说的是void,我以为是构造器,只是扫了一眼(测试结果和构造器的预期效果一样),不用担心!这是常有的事。有时,让一只眼睛看一看会有帮助:)