英文:
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());
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论