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

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

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

问题

  1. Exception in thread "main" java.awt.IllegalComponentStateException: contentPane cannot be set to null.
  2. at java.desktop/javax.swing.JRootPane.setContentPane(JRootPane.java:594)
  3. at java.desktop/javax.swing.JFrame.setContentPane(JFrame.java:679)
  4. at GUI.main(GUI.java:13)
  1. import javax.swing.*;
  2. import java.awt.*;
  3. public class GUI {
  4. private Container mainPanel;
  5. public static void main(String[] args) {
  6. JFrame frame = new JFrame("Loot and Scoot");
  7. frame.setContentPane(new GUI().mainPanel);
  8. frame.setPreferredSize(new Dimension(1280,700));
  9. frame.pack();
  10. frame.setVisible(true);
  11. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  12. frame.setLayout(new BorderLayout());
  13. }
  14. }

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

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

here is my base code

  1. import javax.swing.*;
  2. import java.awt.*;
  3. public class GUI {
  4. private Container mainPanel;
  5. public static void main(String[] args) {
  6. JFrame frame = new JFrame("Loot and Scoot");
  7. frame.setContentPane(new GUI().mainPanel);
  8. frame.setPreferredSize(new Dimension(1280,700));
  9. frame.pack();
  10. frame.setVisible(true);
  11. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  12. frame.setLayout(new BorderLayout());
  13. }
  14. }

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

  1. import javax.swing.*;
  2. import java.awt.*;
  3. public class GUI {
  4. private static Container mainPanel;
  5. public static void main(String[] args) {
  6. mainPanel = new JPanel();
  7. JFrame frame = new JFrame("Loot and Scoot");
  8. frame.setContentPane(new GUI().mainPanel);
  9. frame.setPreferredSize(new Dimension(1280,700));
  10. frame.pack();
  11. frame.setVisible(true);
  12. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  13. frame.setLayout(new BorderLayout());
  14. }
  15. }
英文:

All you need to do is add a

  1. mainPanel = new JPanel

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

  1. import javax.swing.*;
  2. import java.awt.*;
  3. public class GUI {
  4. private static Container mainPanel;
  5. public static void main(String[] args) {
  6. mainPanel = new JPanel();
  7. JFrame frame = new JFrame("Loot and Scoot");
  8. frame.setContentPane(new GUI().mainPanel);
  9. frame.setPreferredSize(new Dimension(1280,700));
  10. frame.pack();
  11. frame.setVisible(true);
  12. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  13. frame.setLayout(new BorderLayout());
  14. }
  15. }

答案2

得分: 0

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

public class GUI extends JPanel{

  1. public static void main(String[] args) {
  2. mainPanel = new JPanel();
  3. JFrame frame = new JFrame("Loot and Scoot");
  4. frame.setContentPane(new GUI());
  5. frame.setPreferredSize(new Dimension(1280,700));
  6. frame.pack();
  7. frame.setVisible(true);
  8. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  9. frame.setLayout(new BorderLayout());
  10. }

}

英文:
  1. import javax.swing.*;
  2. import java.awt.*;
  3. public class GUI extends JPanel{
  4. public static void main(String[] args) {
  5. mainPanel = new JPanel();
  6. JFrame frame = new JFrame("Loot and Scoot");
  7. frame.setContentPane(new GUI());
  8. frame.setPreferredSize(new Dimension(1280,700));
  9. frame.pack();
  10. frame.setVisible(true);
  11. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  12. frame.setLayout(new BorderLayout());
  13. }
  14. }

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:

确定