1

我在这段代码中遇到了一些问题,由于某些奇怪的原因,图形似乎不想更新。我不知道如何解决这个问题,尝试解决这个问题真的很烦人,请帮忙!这是代码:

package org.jeopredy;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.*;

public class Main extends JFrame {

public Main() {
    super("Jeopredy");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(517, 640);
    this.setVisible(true);
    this.add(new panel());
    this.addMouseListener(new panel());
}

public static void main(String args[]) {
    new Main();
}

private class panel extends JPanel implements MouseListener {

    public Rectangle[][] tiles = new Rectangle[5][6];

    public boolean start, question, answer;

    public String[][] Catagories = { { "House of", "representitives" },
            { "Senate" }, { "President" }, { "Vice president" },
            { "Supreme court" } };

    public String[][] Questions = { { "1", "2", "3", "4", "5" },
            { "1", "2", "3", "4", "5" }, { "1", "2", "3", "4", "5" },
            { "1", "2", "3", "4", "5" }, { "1", "2", "3", "4", "5" } };

    public String[][] Answers = { { "", "", "", "", "" },
            { "", "", "", "", "" }, { "", "", "", "", "" },
            { "", "", "", "", "" }, { "", "", "", "", "" } };

    public Point q, a;

    public ArrayList<Point> used = new ArrayList<Point>();

    public panel() {
        start = true;
        for (int x = 0; x < 500; x += 100) {
            for (int y = 0; y < 600; y += 100) {
                tiles[x / 100][y / 100] = new Rectangle(x, y, 100, 100);
            }
        }
        repaint();
    }

    @Override
    public void paint(Graphics g) {
        // super.paintComponent(g);
             //g.setColor(Color.white);

         System.out.println("Start: " + start + " question: " + question
         + " answer: " + answer);
        if (start) {
            int i = 0;
            for (String[] str : Catagories) {
                if (str.length == 1) {
                    g.drawString(str[0], tiles[i][0].x, tiles[i][0].y + 20);
                } else {
                    g.drawString(str[0], tiles[i][0].x, tiles[i][0].y + 20);
                    g.drawString(str[1], tiles[i][0].x, tiles[i][0].y + 30);
                }
                i++;
            }
            for (Rectangle[] rect : tiles) {
                for (Rectangle r : rect) {
                    g.drawRect(r.x, r.y, r.width, r.height);
                }
            }
        } else if (question) {
            // System.out.println("QUeStionJOSNLN");
            g.drawString(Questions[q.x][q.y], 100, 100);
        }
        repaint();
    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
        Point p = new Point(arg0.getPoint().x - 17, arg0.getPoint().y - 40);
        // repaint();
        if (start) {
            for (int x = 0; x < tiles.length; x++) {
                for (int y = 0; y < tiles[x].length; y++) {
                    if (tiles[x][y].contains(p) && y != 0) {
                        question = true;
                        start = false;
                        answer = false;
                        q = new Point(x, y - 1);
                        used.add(q);
                    }
                }
            }
        } else if (question) {
            answer = true;
            question = false;
            start = false;
        } else if (answer) {
            start = true;
            answer = false;
            question = false;
        }

    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }
}
}
4

2 回答 2

3
  1. 首先,你永远不应该覆盖paint(). 我认为这是你的问题的主要原因

Swing中paint、paintComponent和paintComponents的区别

  1. 用于SwingUtilities.invokeLater()加载 GUI

  2. 不要扩展JFrame,完全不推荐,在上面的代码中也没用

  3. private class panel extends JPanel implements类应该以大写字母开头MyPanel

于 2013-01-16T05:23:04.907 回答
3

在我四处挖掘的同时,让我们从...

public void paint(Graphics g) { // <<-- This is bad
    // super.paintComponent(g);
    //g.setColor(Color.white);

    System.out.println("Start: " + start + " question: " + question+ " answer: " + answer);
    if (start) {
        int i = 0;
        for (String[] str : Catagories) {
            if (str.length == 1) {
                g.drawString(str[0], tiles[i][0].x, tiles[i][0].y + 20);
            } else {
                g.drawString(str[0], tiles[i][0].x, tiles[i][0].y + 20);
                g.drawString(str[1], tiles[i][0].x, tiles[i][0].y + 30);
            }
            i++;
        }
        for (Rectangle[] rect : tiles) {
            for (Rectangle r : rect) {
                g.drawRect(r.x, r.y, r.width, r.height);
            }
        }
    } else if (question) {
        // System.out.println("QUeStionJOSNLN");
        g.drawString(Questions[q.x][q.y], 100, 100);
    }
    repaint(); // <<-- This is bad
}

您应该避免覆盖paint. 有很多原因,但基本上,现在你已经完全摧毁了整个油漆链。Swing 中的绘画很复杂,而且很容易毁掉。你必须打电话super.paint(g)

最好覆盖paintComponent,别忘了打电话super.paintComponent!!

切勿在任何绘制方法中更改 UI。通常调用 Paint 以响应重绘请求,修改任何可能导致另一个请求的 UI 将使您陷入 CPU 过载和资源占用的滑坡......

更新

this.add(new panel());
this.addMouseListener(new panel());

这永远不会将鼠标事件发送到屏幕上的面板。您需要针对panel屏幕上的那个注册鼠标侦听器。

你实际上会更好地这样做......

public panel() {
    addMouseListener(this);
    //...continue...
}

更新#2

试着记住,油漆是无国籍的。之前绘制的内容不会出现在下一个绘制周期中。无论你需要画什么,都必须在每个周期上画。

更新#3

我对你的程序想要达到的目标有点困惑,但看看这些改动,看看是否有帮助......

public class Main extends JFrame {

  public Main() {
    super("Jeopredy");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(517, 640);
    this.add(new panel());
    this.setVisible(true);
  }

  public static void main(String args[]) {
    EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        new Main();
      }
    });
  }

  private class panel extends JPanel implements MouseListener {

    public Rectangle[][] tiles = new Rectangle[5][6];
    public boolean start, question, answer;
    public String[][] Catagories = {{"House of", "representitives"},
      {"Senate"}, {"President"}, {"Vice president"},
      {"Supreme court"}};
    public String[][] Questions = {{"1", "2", "3", "4", "5"},
      {"1", "2", "3", "4", "5"}, {"1", "2", "3", "4", "5"},
      {"1", "2", "3", "4", "5"}, {"1", "2", "3", "4", "5"}};
    public String[][] Answers = {{"", "", "", "", ""},
      {"", "", "", "", ""}, {"", "", "", "", ""},
      {"", "", "", "", ""}, {"", "", "", "", ""}};
    public Point q, a;
    public ArrayList<Point> used = new ArrayList<Point>();

    public panel() {
      addMouseListener(this);
      start = true;
      for (int x = 0; x < 500; x += 100) {
        for (int y = 0; y < 600; y += 100) {
          tiles[x / 100][y / 100] = new Rectangle(x, y, 100, 100);
        }
      }
      repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
      super.paintComponent(g); 

      System.out.println("Start: " + start + " question: " + question
          + " answer: " + answer);
      if (start) {
        int i = 0;
        for (String[] str : Catagories) {
          if (str.length == 1) {
            g.drawString(str[0], tiles[i][0].x, tiles[i][0].y + 20);
          } else {
            g.drawString(str[0], tiles[i][0].x, tiles[i][0].y + 20);
            g.drawString(str[1], tiles[i][0].x, tiles[i][0].y + 30);
          }
          i++;
        }
        for (Rectangle[] rect : tiles) {
          for (Rectangle r : rect) {
            g.drawRect(r.x, r.y, r.width, r.height);
          }
        }
      } else if (question) {
        // System.out.println("QUeStionJOSNLN");
        g.drawString(Questions[q.x][q.y], 100, 100);
      }
//      repaint();
    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
//      Point p = new Point(arg0.getPoint().x - 17, arg0.getPoint().y - 40);
      Point p = arg0.getPoint();
      // repaint();
      if (start) {
        for (int x = 0; x < tiles.length; x++) {
          for (int y = 0; y < tiles[x].length; y++) {
            if (tiles[x][y].contains(p) && y != 0) {
              question = true;
              start = false;
              answer = false;
              q = new Point(x, y - 1);
              used.add(q);
            }
          }
        }
      } else if (question) {
        answer = true;
        question = false;
        start = false;
      } else if (answer) {
        start = true;
        answer = false;
        question = false;
      }

      repaint();

    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
      // TODO Auto-generated method stub
    }

    @Override
    public void mouseExited(MouseEvent arg0) {
      // TODO Auto-generated method stub
    }

    @Override
    public void mousePressed(MouseEvent arg0) {
      // TODO Auto-generated method stub
    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
      // TODO Auto-generated method stub
    }
  }
}
于 2013-01-16T05:26:52.630 回答