Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.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_Graphics - Fatal编程技术网

Java 程序不是绘画

Java 程序不是绘画,java,swing,graphics,Java,Swing,Graphics,我本打算在我的JPanel上画一个正方形,但是,它没有出现。 我做错了什么 class GUI extends JPanel { private static Game game = new Game(); ... public GUI () { SwingUtilities.invokeLater(new Runnable() { public void run() { setAttributes(

我本打算在我的JPanel上画一个正方形,但是,它没有出现。 我做错了什么

class GUI extends JPanel {
    private static Game game = new Game();
    ...
    public GUI () {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                setAttributes();
                makeMenu();
            }
        });
    }
    ... 
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.white);
        g.drawRect(20, 20, 100, 100);
    }
}

编辑:代码

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

class GUI extends JPanel {
    private static Game game = new Game();

    private static JPanel panel = new JPanel();
    private static JFrame frame = new JFrame();
    final private static int FRAME_HEIGHT = 500;
    final private static int FRAME_WIDTH = 500;
    //Board size 25x25px
    final private static int PIXEL_SIZE = 20;
    public GUI () {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            setAttributes();
            makeMenu();
         }
      });
    }
    public static void setAttributes() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("");
        frame.setBackground(Color.black);
        frame.setVisible(true);
    }

    private static void makeMenu() {
        JButton start = new JButton("Start");
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                game.startGame();
            }
        });
        panel.add(start);
        frame.add(panel);
        frame.pack();
    }
    public void setGameFrame() {
        panel.removeAll();
        frame.setTitle("Snake v0.1");
        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    }
    public void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.setColor(Color.white);
      g.drawRect(20, 20, 100, 100);
   }
    public void paintGraphics() {
        int[][] pixels = Game.getGraphics();
    }
}

我看了你的密码。你在哪里添加过GUI?回答:你没有,如果你没有,什么都不会被画。解决方案:将其添加到gui中,并阅读教程,因为代码中有很多问题需要解决

其他建议:

  • 除去contant之外的所有静态变量
  • 在主方法中调用invokeLater,而不是在JPanel的构造函数中
  • 同样,将绘画GUI添加到实际GUI中,以便将其添加到将显示它的内容中
  • 不要在任何组件上调用
    getGraphics()
    ,因为这样会得到非持久性图形对象

e、 g


您的代码本身没有显示任何错误,但也没有显示太多错误。因此,您的错误可能存在于未显示的代码中的其他地方。是时候做一些调试了。我确实想知道您的静态游戏变量Game(static???),以及您从JPanel的构造函数中调用invokeLater的情况,这很奇怪,因为这个构造函数应该只在EDT上调用,所以为什么要在EDT上排队运行?建议:创建并发布一个。让我们看看你做错了什么。这是完整的GUI类:-我是Java新手,所以我可能做错了很多事情。不,请不要这样做,不要在链接中发布代码。您应该调试,您应该努力创建并发布一个,您应该在这里发布您的代码和您的问题,而不是在链接中。拜托,我们是志愿者,所以工作应该是你的,而不是我们的。我发布它是因为我知道这是一个明显的错误,不需要调试。
“我发布它是因为我知道这是一个明显的错误,不需要调试。”
--不。。。如果这很明显,你会发布显示错误的代码,对吗?你没有,所以很明显不是很明显。通过调试,我的意思是做一些工作来隔离错误。这可以通过println语句实现。例如,如果这是我的错误,我不知道是什么导致了它,我会把println放在所有地方,我会很快发现paintComponent方法甚至没有被调用。你能链接我吗?我找不到演示如何“添加”它的教程。@ModifyYou:您可以通过调用
someComponent.add(thisComponent)
来添加它。someComponent必须是一个JPanel或其他组件,其组件层次结构最终会导致一个顶级窗口,这里是您的JFrame。可以在此处找到Swing教程:。在页面上搜索
Swing
。我到底应该在参数中传递什么?@ModifyYou:这取决于你--你想把这个JPanel放在哪里?@ModifyYou:例如,请参见上面的编辑。在本例中,我将当前JPanel添加到JFrame的contentPane中。但您将JPanel添加到的内容将完全取决于您计划如何使用它。这取决于你。
import javax.swing.*;
import java.awt.*;

class GUI extends JPanel {
   private static final int PREF_W = 200;
   private static final int PREF_H = PREF_W;
   private static final int RECT_X = 20;
   private static final int RECT_Y = RECT_X;
   private static final int RECT_WIDTH = 100;

   public GUI() {
      setBackground(Color.darkGray);
   }

   // use @Override to be sure that your method is a true override
   // Note that paintComponent should be protected, not public
   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.setColor(Color.white);

      // avoiding "magic" numbers here.
      g.fillRect(RECT_X, RECT_Y, RECT_WIDTH, RECT_WIDTH);
      g.setColor(Color.red);
      g.drawRect(RECT_X, RECT_Y, RECT_WIDTH, RECT_WIDTH);
   }

   // so that the layout managers know how big I want this JPanel to be.
   // this is a dumb method implementation. Yours will hopefully be smarter
   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H); 
   }

   private static void createAndShowGui() {
      GUI mainPanel = new GUI();

      JFrame frame = new JFrame("GUI");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}