Board
的类中有一个方法,并且在该类中我有一个名为updateScore()
的方法。
private void updateScore(){
int whitePawn = 0; int blackPawn = 0;
int whiteKnight = 0; int blackKnight = 0;
int whiteBishop = 0; int blackBishop = 0;
int whiteCastle = 0; int blackCastle = 0;
int whiteQueen = 0; int blackQueen = 0;
int whiteKing = 0; int blackKing = 0;
for(int k = 0; k < 8; k++){ //Checks through the board to see what pieces are on it.
for(int l = 0; l < 8; l++){
if(gameBoard[k][l] == 11){
whitePawn++; //increments the vales how many of a certain pieces are on the board.
}
if(gameBoard[k][l] == 12){
whiteCastle++;
}
if(gameBoard[k][l] == 13){
whiteKnight++;
}
if(gameBoard[k][l] == 14){
whiteBishop++;
}
if(gameBoard[k][l] == 15){
whiteQueen++;
}
if(gameBoard[k][l] == 16){
whiteKing++;
}
if(gameBoard[k][l] == 21){
blackPawn++;
}
if(gameBoard[k][l] == 22){
blackCastle++;
}
if(gameBoard[k][l] == 23){
blackKnight++;
}
if(gameBoard[k][l] == 24){
blackBishop++;
}
if(gameBoard[k][l] == 25){
blackQueen++;
}
if(gameBoard[k][l] == 26){
blackKing++;
}
}
}
if(whiteKing == 0){ //No white king? Checkmate.
checkMate(1);
}
else if(blackKing == 0){ //No black king? Checkmate.
checkMate(2);
}
score = new Scores();
score.showScores(whitePawn, blackPawn);
}
我还有另一个名为Scores
的类,它初始化另一个JFrame以显示游戏分数:
以下是分数类:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
class Scores extends JFrame{
private int width = 300;
private int height = 400;
int whitePawn = 0;
int blackPawn = 0;
public Scores(){
int a = this.getWidth();
setSize(width, height);
setTitle("Chess - Scores");
setIconImage(new ImageIcon("imgs/chess-icon.png").getImage());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setLocation(a+25,50);
JPanel p = new JPanel();
getContentPane().add(p);
this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.X_AXIS));
Box horizontalBox;
//showScores(whitePawn, blackPawn);
}
public void paintComponent(Graphics g){
super.paintComponents(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.WHITE);
}
public void showScores(int wp, int bp){
whitePawn = wp;
blackPawn = bp;
JLabel white_pawn = new JLabel("White Pawn: "+wp+" ", JLabel.CENTER);
JLabel black_pawn = new JLabel("White Pawn: "+bp+" ", JLabel.CENTER);
add(white_pawn, Box.createHorizontalGlue());
add(Box.createRigidArea(new Dimension(9, 0)));
add(black_pawn, Box.createHorizontalGlue());
add(Box.createRigidArea(new Dimension(9, 0)));
revalidate();
repaint();
}
}
但问题是,调用方法时窗口永远不会更新?任何人都可以提供解决方案吗?
答案 0 :(得分:2)
您确实意识到JFrame
没有paintComponent
方法吗?所以你没有压倒什么。如果您想使用JPanel
paintComponent
此外,这可能是范围问题吗?是否有其他组件依赖于whitePawn, whiteKing, etc
变量?因为所有这些都是本地范围的。无论您调用方法
答案 1 :(得分:1)
super.paintComponent's'(g);
看起来你有一个错字。摆脱paintComponent()中的“s”。
答案 2 :(得分:0)
在Scores.showScores方法中使用
getContentPane().add()
而不只是调用add