双人跳棋游戏(Java)
在我的春假假期里,我开始写跳棋游戏。我已经创建了我的棋盘,但我不能写鼠标听器部分。你能帮我做这个零件吗?我不太确定如何实现这一部分?MouseListener是对的吗?或者我应该选择另一个听众?我在谷歌上搜索过,但找不到任何线索。 非常感谢你双人跳棋游戏(Java),java,eclipse,Java,Eclipse,在我的春假假期里,我开始写跳棋游戏。我已经创建了我的棋盘,但我不能写鼠标听器部分。你能帮我做这个零件吗?我不太确定如何实现这一部分?MouseListener是对的吗?或者我应该选择另一个听众?我在谷歌上搜索过,但找不到任何线索。 非常感谢你 public class CheckersGUI { private final JPanel gui = new JPanel(new BorderLayout(3, 3)); private JButton[][] Squares = new JBu
public class CheckersGUI {
private final JPanel gui = new JPanel(new BorderLayout(3, 3));
private JButton[][] Squares = new JButton[8][8];
private JPanel Board;
private final JLabel message = new JLabel(
"Ready to play!");
private static final String COLS = "ABCDEFGH";
private ImageIcon brown = new ImageIcon("brown.jpg");
private ImageIcon red = new ImageIcon("red.png");
CheckersGUI() {
create();
//create ButtonHandler
/* ButtonHandler handler = new ButtonHandler();
for (int i=0; i<Squares.length; i++){
for (int j=0; i<Squares[0].length; i++){
Squares[i][j].addMouseListener(handler);
}
}
}
private class ButtonHandler implements MouseListener{
public void MouseClicked ( MouseEvent event){
}*/
}
public final void create() {
// set up the main GUI
gui.setBorder(new EmptyBorder(5, 5, 5, 5));
JToolBar tools = new JToolBar();
tools.setFloatable(false);
gui.add(tools, BorderLayout.PAGE_START);
Action newGameAction = new AbstractAction("New") {
@Override
public void actionPerformed(ActionEvent e) {
setupNewGame();
}
};
tools.add(newGameAction);
// TODO - add functionality!
tools.addSeparator();
tools.add(message);
gui.add(new JLabel(""), BorderLayout.LINE_START);
Board = new JPanel(new GridLayout(0, 9)) ;
Board.setBorder(new CompoundBorder(
new EmptyBorder(8,8,8,8),
new LineBorder(Color.BLACK)
));
JPanel boardConstrain = new JPanel(new GridBagLayout());
boardConstrain.add(Board);
gui.add(boardConstrain);
// create the chess board squares
Insets buttonMargin = new Insets(0, 0, 0, 0);
for (int ii = 0; ii < Squares.length; ii++) {
for (int jj = 0; jj < Squares[ii].length; jj++) {
JButton b = new JButton();
b.setMargin(buttonMargin);
ImageIcon icon = new ImageIcon(
new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB));
b.setIcon(icon);
if ((jj % 2 == 1 && ii % 2 == 1)
|| (jj % 2 == 0 && ii % 2 == 0)) {
b.setBackground(Color.WHITE);
} else {
b.setBackground(Color.BLACK);
}
Squares[jj][ii] = b;
}
}
/*
* fill the chess board
*/
Board.add(new JLabel(""));
// fill the top row
for (int ii = 0; ii < 8; ii++) {
Board.add(
new JLabel(COLS.substring(ii, ii + 1),
SwingConstants.CENTER));
}
// fill the black non-pawn piece row
for (int ii = 0; ii < 8; ii++) {
for (int jj = 0; jj < 8; jj++) {
switch (jj) {
case 0:
Board.add(new JLabel("" + (9-(ii + 1)),
SwingConstants.CENTER));
default:
Board.add(Squares[jj][ii]);
}
}
}
}
public final JComponent getGui() {
return gui;
}
/**
* Initializes the icons of the initial chess board piece places
*/
private final void setupNewGame() {
message.setText("Make your move!");
for (int ii = 0; ii < 8; ii++) {
Squares[ii][1].setIcon(red);
}
for (int ii = 0; ii < 8; ii++) {
Squares[ii][2].setIcon(red);
}
for (int ii = 0; ii < 8; ii++) {
Squares[ii][5].setIcon(brown);
}
for (int ii = 0; ii < 8; ii++) {
Squares[ii][6].setIcon(brown);
}
}
public static void main(String[] args) {
CheckersGUI cg = new CheckersGUI();
JFrame f = new JFrame("Checkers");
f.add(cg.getGui());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
f.setResizable(false);
f.setVisible(true);
}
}
公共类检查器GUI{
私有最终JPanel gui=新JPanel(新边界布局(3,3));
私有JButton[][]正方形=新JButton[8][8];
私人JPanel董事会;
专用最终JLabel消息=新JLabel(
“准备好玩了!”;
私有静态最终字符串COLS=“ABCDEFGH”;
private ImageIcon brown=新ImageIcon(“brown.jpg”);
私有图像图标红色=新图像图标(“red.png”);
CheckersGUI(){
创建();
//创建按钮句柄
/*ButtonHandler处理程序=新建ButtonHandler();
对于(int i=0;i您可以向每个按钮添加一个ActionListener
。如果按下按钮(实际上是在释放按钮时),将调用actionPerformed
方法
将以下内容添加到代码中,以在单击每个方块后使其变为红色:
public static class ButtonHandler implements ActionListener {
private JButton b;
public ButtonHandler(JButton b) {
this.b = b;
}
@Override
public void actionPerformed(ActionEvent e) {
b.setBackground(Color.red);
}
}
这是处理程序。请注意,每个处理程序实例都与一个按钮关联
现在,在每个按钮创建完成后,创建一个新的按钮Handler
:
JButton b = new JButton();
...
b.addActionListener(new ButtonHandler(b));
您可以更改actionPerformed
的实现
示例:将方法getCurrentPlayer()
添加到actionPerformed
,然后,如果当前播放器是player one,则将颜色更改为蓝色,否则,将颜色更改为红色
我相信这将帮助您实现所需的功能。好吧,您需要为每个按钮创建一个处理程序。当按下“我”按钮时,您希望发生什么?