だから私はプログラミングに比較的慣れていないので、基本的なチェッカーゲームを作成しようとしています。現時点で問題が発生しているのは、本質的に新しいボタンに置き換える必要がある部分を含む JButtons の 1 つをクリックしたときです。それは特定の場所です。これまでのコードでは、上記の JButton を削除していますが、新しいものに置き換えていません。
ボード全体に適用する前に、0,0 ピースでのみこれを達成しようとしています。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class checkersBeBitchin extends JFrame{
JButton[][] squares = new JButton[8][8];
Container box = getContentPane();
int[][] pieceTracker = new int[8][8];
JPanel board = new JPanel();
/**
* @param args
*/
public static void main(String[] args) {
checkersBeBitchin begin = new checkersBeBitchin();
}
public checkersBeBitchin(){
box.setLayout(new BorderLayout());
makeBoard();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(600,600);
setTitle("Checkers");
}
@SuppressWarnings("deprecation")
private void makeBoard() {
board.setLayout(new GridLayout(8,8));
for (int i=0; i<8; i++){
for (int j=0; j<8; j++) {
squares[i][j] = new JButton();
ActionListener actionListener = new Board();
squares[i][j].addActionListener(actionListener);
if((i%2 != 0 && j%2 !=0) ||(i%2==0 && j%2 == 0) ){
squares[i][j].setBackground(Color.black);
squares[i][j].setLabel("xy "+i+j);
//System.out.println("Black"+i+","+j); debugging
if(i<3){
int blue = 1;
Icon piece = new ImageIcon(getClass().getResource("/resources/piece.png"));
JButton button = new JButton();
JLabel bluePiece =new JLabel(piece);
pieceTracker[i][j]=blue;
//squares[i][j].setRolloverIcon("image dir") to make it prettier down the road.
squares[i][j].add(bluePiece);
ActionListener Listener = new Blue();
button.addActionListener(Listener);
}
else if (i>4){
int red=-1;
Icon piece = new ImageIcon(getClass().getResource("/resources/piece2.png"));
JButton button = new JButton(piece);
JLabel redPiece =new JLabel(piece);
squares[i][j].add(redPiece);
pieceTracker[i][j]=red;
ActionListener Listener = new Red();
button.addActionListener(Listener);
//squares[i][j].setRolloverSelectedIcon("/resources/piece2alt.png");
}
}
else{
squares[i][j].setBackground(Color.white);
squares[i][j].setLabel("xy "+i+j);
pieceTracker[i][j]=0;
//System.out.println("White"+i+","+j); //debugging
}
board.add(squares[i][j]);
}
}
box.add(board, BorderLayout.CENTER);
}
private class Blue implements ActionListener{
public void actionPerformed (ActionEvent e){
System.out.println("You sexy Blue beast.");
Object x = e.getSource();
Component comp = (Component) x;
board.remove(comp);
JButton replace = new JButton();
replace.setBackground(Color.GREEN);
squares.toString();
squares[5][5].add(replace);
board.revalidate();
board.repaint();
System.err.println(x);
}
}
private class Red implements ActionListener{
public void actionPerformed (ActionEvent e){
// System.out.println("You sexy Red beast.");
Object x = e.getSource();
Component comp = (Component) x;
board.remove(comp);
board.revalidate();
board.repaint();
}
}
private class Board implements ActionListener{
public void actionPerformed (ActionEvent e){
Object x = e.getSource();
Component comp = (Component) x;
// System.out.println(comp);
board.remove(comp); //removes the piece
// System.out.println("You clicked the board. Good for you.");
if (e.getActionCommand().equals("xy 00")){
System.out.println("Awesome");
// String s = e.getActionCommand();
String jj = Character.toString(s.charAt(4));
String ii = Character.toString(s.charAt(3));
// System.out.println("j:"+jj+" i:"+ii);
int coordsI = Integer.parseInt(ii);
int coordsJ = Integer.parseInt(jj);
JButton replace = new JButton();
replace.setBackground(Color.GREEN);
// squares.toString();
squares[coordsI][coordsJ].add(replace);
board.revalidate();
board.repaint();
}
board.revalidate();
board.repaint();
}
}
}