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