【问题标题】:Program not Painting [closed]程序不绘画[关闭]
【发布时间】:2013-12-19 23:32:31
【问题描述】:

我打算为此在我的 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();
    }
}

【问题讨论】:

  • 您的代码本身没有显示任何错误,但也没有显示太多。因此,您的错误可能位于未显示的代码中的其他地方。是时候做一些调试了。我确实想知道您的静态游戏变量游戏(静态??),以及您从 JPanel 的构造函数中调用的 invokeLater,这很奇怪,因为该构造函数只能在 EDT 上调用,那么为什么在东部时间在吗?建议:创建并发布sscce。让我们真正看看你做错了什么。
  • 这是完整的 GUI 类:pastebin.com/5tDRvwL6 - 我是 Java 新手,所以我可能做错了很多事情。
  • 不,请不要这样做,不要在链接中发布代码。您应该调试,您应该努力创建和发布sscce,并且您应该在此处发布您的代码以及您的问题,而不是在链接中。拜托,我们是志愿者,所以努力应该是你的,而不是我们的。
  • 我发布它是因为我知道这是一个明显的错误,不需要调试。
  • "I posted it because I know it's an obvious error, that does not need debugging." -- 不...如果很明显,您会发布显示错误的代码,对吗?你没有,所以它显然不是那么明显。通过调试,我的意思是做一些工作来隔离错误。这可以通过 println 语句来完成。例如,如果这是我的错误,而我不知道是什么原因造成的,我会将 println 放在任何地方,我很快就会发现 paintComponent 方法甚至没有被调用。

标签: java swing graphics


【解决方案1】:

我查看了您的代码。你在哪里添加 GUI 到任何东西?答:你没有,如果你不这样做,什么都不会画。解决方案:将其添加到 gui,并阅读教程,因为您的代码中有很多需要修复的地方。

其他建议:

  • 删除除 contants 之外的所有静态变量。
  • 在您的 main 方法中调用您的 invokeLater,而不是在 JPanel 的构造函数中
  • 再次,将您的绘画 GUI 添加到您的实际 gui 中,以便将其添加到将显示它的东西中。
  • 不要在任何组件上调用 getGraphics(),因为这会得到一个非持久的 Graphics 对象。

例如,

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();
         }
      });
   }
}

【讨论】:

  • 你能链接我吗?我找不到说明如何“添加”它的教程。
  • @ModifyYou:您可以通过调用someComponent.add(thisComponent) 添加它。那 someComponent 必须是 JPanel 或其他组件,其组件层次结构最终会导致顶级窗口,这里是您的 JFrame。 Swing 教程可以在这里找到:The Really Big Index。在页面上搜索Swing
  • 我到底传递了什么参数?
  • @ModifyYou:这取决于你——你想把这个 JPanel 放在哪里?
  • @ModifyYou:例如,参见上面的编辑。在示例中,我将当前 JPanel 添加到 JFrame 的 contentPane。但是你添加你的 JPanel 将取决于你打算如何使用它。这取决于你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-15
  • 1970-01-01
  • 1970-01-01
  • 2021-11-19
  • 1970-01-01
  • 2011-04-26
相关资源
最近更新 更多