Java 简单的8路新手游戏
嗨,我是java新手,我想我会尝试制作一个游戏,让用户自己尝试解决8皇后问题。然而,它增加了启动8辆车的难度,最多启动14位主教,然后启动8位皇后 我已经成功地创建了棋盘。我的鼠标听筒有问题。。。棋盘上的每个方块都是一个按钮,单击时,我的意图是该方块将改变颜色以表示其已被单击,然后所有不能再次单击的方块也将改变以表示退出游戏的方块 单击正方形时,它似乎不执行任何操作。 对不起,我知道这很琐碎。 谢谢Java 简单的8路新手游戏,java,mouseevent,Java,Mouseevent,嗨,我是java新手,我想我会尝试制作一个游戏,让用户自己尝试解决8皇后问题。然而,它增加了启动8辆车的难度,最多启动14位主教,然后启动8位皇后 我已经成功地创建了棋盘。我的鼠标听筒有问题。。。棋盘上的每个方块都是一个按钮,单击时,我的意图是该方块将改变颜色以表示其已被单击,然后所有不能再次单击的方块也将改变以表示退出游戏的方块 单击正方形时,它似乎不执行任何操作。 对不起,我知道这很琐碎。 谢谢 import javax.swing.*; 导入java.awt.*; 导入java.awt.e
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.MouseEvent;
导入java.awt.event.MouseListener;
公共类rooks扩展JFrame实现MouseStener{
专用最终集成电路板尺寸=8;
私人最终集成电路板尺寸=8;
专用最终集成电路板尺寸行数=8;
//私有JTextField bottom=新JTextField(“”);
//私人JLabel bannerl=新JLabel(“游戏”);
//私人JButton queens=新JButton(“玩皇后”);
私人JButton车=新JButton(“玩车”);
//列兵杰布顿主教=新杰布顿(“扮演骑士”);
私有JButton[][]cboard=新JButton[BOARD_SIZE][BOARD_SIZE];
私有JTextArea bottomtextarea=新JTextArea();
//创建棋盘的构造函数
公共车(){
这个。设置大小(500500);
本文件为setTitle(“rooks”);
//这个.setIconImage();
//创建JPanel并添加JComponents
JPanel main=newjpanel(newborderlayout());
这个.setContentPane(main);
JPanel north=新的JPanel();
北.设置布局(新网格布局(1,3));
main.add(北,BorderLayout.north);
//北加(皇后区);
北。添加(rooks);
//北加(主教);
JPanel南部=新JPanel();
main.add(南,BorderLayout.south);
南部。添加(底部区域);
bottomtextarea.setEditable(假);
bottomtextarea.setVisible(true);
//创建网格(实际棋盘)并初始化每个按钮,不使用字符
JPanel棋盘=新JPanel(新网格布局(棋盘大小,棋盘大小));
添加(棋盘、边框布局、中心);
对于(inti=0;i您只在最后一个按钮上添加鼠标侦听器,即cboard[7][7]上的JButton
- 为什么要对JButtons使用鼠标侦听器而不是ActionListener?这毫无意义
- 为什么不将ActionListener添加到for循环中的所有JButtons中呢
您只在最后一个按钮上添加了鼠标侦听器,即cboard[7][7]上的JButton
- 为什么要对JButtons使用鼠标侦听器而不是ActionListener?这毫无意义
- 为什么不将ActionListener添加到for循环中的所有JButtons中呢
您的代码正在运行。我运行它,当我单击7-7方块(右下角的方块)时,我会收到消息:“它已被单击”
由于您只将鼠标侦听器添加到这个方块中,因此代码的行为符合预期
但有些东西你应该重构:
- 为什么要定义棋盘大小、棋盘大小、棋盘大小行?如果只使用二次棋盘,则只需要棋盘大小,如果不需要,则不需要棋盘大小
- 它的惯例是用大写字母写你的类的第一个字母。所以它是Rooks而不是Rooks
- 您需要将侦听器添加到电路板的每一个方块中,而不是仅添加到一个方块中
这应该足够开始了。您的代码正在运行。我运行它,当我单击7-7方块(右下角的方块)时,我得到消息:“它已被单击”
由于您只将鼠标侦听器添加到这个方块中,因此代码的行为符合预期
但有些东西你应该重构:
- 为什么要定义棋盘大小、棋盘大小、棋盘大小行?如果只使用二次棋盘,则只需要棋盘大小,如果不需要,则不需要棋盘大小
- 它的惯例是用大写字母写你的类的第一个字母。所以它是Rooks而不是Rooks
- 您需要将侦听器添加到电路板的每一个方块中,而不是仅添加到一个方块中
这应该足够开始了。谢谢,我在发布后意识到我只是把它放在一个JButton上。我使用ActionListener并将它放在循环中,这样它就可以将它添加到所有按钮中。谢谢,我意识到发布后我只将它放在一个JButton上。我使用ActionListener并将它放在循环中,这样它就可以添加它了所有按钮。
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class rooks extends JFrame implements MouseListener{
private final int BOARD_SIZE = 8;
private final int BOARD_SIZE_COLS = 8;
private final int BOARD_SIZE_ROWS = 8;
// private JTextField bottom = new JTextField("") ");
// private JLabel bannerl = new JLabel("The game");
// private JButton queens = new JButton(" Play Queens ");
private JButton rooks = new JButton(" Play Rooks ");
// private JButton bishops = new JButton(" Play Knights ");
private JButton[][] cboard = new JButton[BOARD_SIZE][BOARD_SIZE];
private JTextArea bottomtextarea = new JTextArea();
// constructor creating the chessboard
public rooks(){
this.setSize(500, 500);
this.setTitle("rooks");
// this.setIconImage();
// create JPanels and add JComponents
JPanel main = new JPanel(new BorderLayout());
this.setContentPane(main);
JPanel north = new JPanel();
north.setLayout(new GridLayout(1,3));
main.add(north, BorderLayout.NORTH);
// north.add(queens);
north.add(rooks);
// north.add(bishops);
JPanel south = new JPanel();
main.add(south, BorderLayout.SOUTH);
south.add(bottomtextarea);
bottomtextarea.setEditable(false);
bottomtextarea.setVisible(true);
// create grid (actual chessboard) and initialise each button with no char
JPanel chessBoard = new JPanel(new GridLayout(BOARD_SIZE, BOARD_SIZE));
main.add(chessBoard, BorderLayout.CENTER);
for (int i=0; i<BOARD_SIZE_ROWS; i++){
for(int j=0; j<BOARD_SIZE_COLS; j++){
cboard[i][j] = new JButton("");
chessBoard.add(cboard[i][j]);
// as it loops add colour to the board, if (i+j=even then white, otherwise black)
if ((i + j) % 2 == 0) {
cboard[i][j].setBackground(Color.black);
}
else {
cboard[i][j].setBackground(Color.white);
}
}
}
cboard[7][7].addMouseListener(this);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
System.out.print("it has been clicked");
}
void saySomething(String eventDescription, MouseEvent e) {
}
}