如何修复这个错误,即使没有红色文本。

huangapple go评论71阅读模式
英文:

How do i fix this error even though there is no red text

问题

Exception in thread "main" java.awt.IllegalComponentStateException: contentPane cannot be set to null.
	at java.desktop/javax.swing.JRootPane.setContentPane(JRootPane.java:594)
	at java.desktop/javax.swing.JFrame.setContentPane(JFrame.java:679)
	at GUI.main(GUI.java:13)
import javax.swing.*;
import java.awt.*;

public class GUI {

    private Container mainPanel;

    public static void main(String[] args) {
        JFrame frame = new JFrame("Loot and Scoot");
        frame.setContentPane(new GUI().mainPanel);
        frame.setPreferredSize(new Dimension(1280,700));
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
    }
}

I've provided the translated code and error message as requested.

英文:

Right now i'm making a GUI and i'm getting this error code

Exception in thread "main" java.awt.IllegalComponentStateException: contentPane cannot be set to null.
	at java.desktop/javax.swing.JRootPane.setContentPane(JRootPane.java:594)
	at java.desktop/javax.swing.JFrame.setContentPane(JFrame.java:679)
	at GUI.main(GUI.java:13)

here is my base code

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

public class GUI {

    private Container mainPanel;

    public static void main(String[] args) {
        JFrame frame = new JFrame("Loot and Scoot");
        frame.setContentPane(new GUI().mainPanel);
        frame.setPreferredSize(new Dimension(1280,700));
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
    }
}

I've tried fixing this problem with Alt+Enter but nothing is wrong there is no red text

答案1

得分: 1

All you need to do is add a mainPanel = new JPanel i've edited my code correctly so it works now, here it is

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

public class GUI {

    private static Container mainPanel;

    public static void main(String[] args) {

        mainPanel = new JPanel();

        JFrame frame = new JFrame("Loot and Scoot");
        frame.setContentPane(new GUI().mainPanel);
        frame.setPreferredSize(new Dimension(1280,700));
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
    }
}
英文:

All you need to do is add a

mainPanel = new JPanel

i've edited my code correctly so it works now, here it is

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

public class GUI {

    private static Container mainPanel;

    public static void main(String[] args) {

        mainPanel = new JPanel();

        JFrame frame = new JFrame("Loot and Scoot");
        frame.setContentPane(new GUI().mainPanel);
        frame.setPreferredSize(new Dimension(1280,700));
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
    }
}

答案2

得分: 0

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

public class GUI extends JPanel{

public static void main(String[] args) {

    mainPanel = new JPanel();

    JFrame frame = new JFrame("Loot and Scoot");
    frame.setContentPane(new GUI());
    frame.setPreferredSize(new Dimension(1280,700));
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
}

}

英文:
import javax.swing.*;
import java.awt.*;

public class GUI extends JPanel{
  
    public static void main(String[] args) {

        mainPanel = new JPanel();

        JFrame frame = new JFrame("Loot and Scoot");
        frame.setContentPane(new GUI());
        frame.setPreferredSize(new Dimension(1280,700));
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
    }
}

huangapple
  • 本文由 发表于 2020年7月29日 01:54:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63140077.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定