Java JButton在actionlistener和mouselistener保持不变的情况下不执行任何操作
当我点击Java JButton在actionlistener和mouselistener保持不变的情况下不执行任何操作,java,swing,jbutton,actionlistener,Java,Swing,Jbutton,Actionlistener,当我点击JButtons时,我不知道出了什么问题。非常感谢您的任何帮助 注意:实现侦听器的类位于扫雷面板类中 public class MineSweeperPanel extends JPanel { // declarations of all variables and components private ActionListener listener; private Cell iCell; //private Icon emptyIcon;
JButtons
时,我不知道出了什么问题。非常感谢您的任何帮助
注意:实现侦听器的类位于扫雷面板
类中
public class MineSweeperPanel extends JPanel {
// declarations of all variables and components
private ActionListener listener;
private Cell iCell;
//private Icon emptyIcon;
//private Icon mineIcon;
//private Icon flagIcon;
private int size;
private int mines;
private int lostCount;
private int winCount;
private JButton[][] board;
private JButton quitButton;
private JButton resetButton;
private JLabel lostLabel;
private JLabel winLabel;
private JPanel somePanel;
private JPanel otherPanel;
private MineSweeperGame game;
private MouseListener mListener;
/**********************************************************************
* Default constructor that creates all of the components of the
* gui including the positioning of all of the components.
*********************************************************************/
public MineSweeperPanel() {
// initializations of all components and variables
// that are used in the constructor.
setSize();
winCount = 0;
lostCount = 0;
board = new JButton[size][size];
somePanel = new JPanel();
otherPanel = new JPanel();
game = new MineSweeperGame(size,mines);
winLabel = new JLabel("Wins: " + winCount);
lostLabel = new JLabel("Loses: " + lostCount);
quitButton = new JButton("Quit");
resetButton = new JButton("New Game");
somePanel.setLayout(new GridLayout(size,size));
//emptyIcon = new ImageIcon("empty_icon.png");
// creates the board (each cell) and adds it to the panel
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
board[row][col] = new JButton (""/*, emptyIcon*/);
board[row][col].setPreferredSize(new Dimension(25,25));
board[row][col].setMargin(new Insets(0,0,0,0));
board[row][col].setFocusPainted(false);
board[row][col].addActionListener(listener);
board[row][col].addMouseListener(mListener);
somePanel.add(board[row][col]);
}
}
// calls to refresh the board display
displayBoard();
// adds the win and lost counters/labels to the panel
otherPanel.add(winLabel);
otherPanel.add(lostLabel);
// adds the reset button to the panel
resetButton.addActionListener(listener);
otherPanel.add(resetButton);
// adds the quit button to the panel
quitButton.addActionListener(listener);
otherPanel.add(quitButton);
otherPanel.setLayout(new GridLayout(3, 2));
// adds panels to JPanel
add(somePanel);
add(otherPanel);
}
...
/**********************************************************************
* An ActionListener for the quit and reset buttons.
*********************************************************************/
public class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == quitButton) {
System.exit(1);
}
else if(e.getSource() == resetButton) {
game.reset();
}
displayBoard();
}
}
/**********************************************************************
* A MouseListener for left/right click actions - handles flagging
* as well as revealing cells.
*********************************************************************/
public class ButtonMouseListener implements MouseListener {
@Override
public void mouseClicked(MouseEvent e) {
// if left clicked exposes cell
if(e.getButton() == MouseEvent.BUTTON1) {
for(int row = 0; row < size; row++) {
for(int col = 0; col < size; col++) {
if(e.getSource() == board[row][col])
if(!game.getCell(row, col).isExposed()) {
game.select(row, col);
board[row][col].setEnabled(false);
}
}
}
displayBoard();
}
// if right clicked determines a flag or erases flag
else if(e.getButton() == MouseEvent.BUTTON3) {
for(int row = 0; row < size; row++) {
for(int col = 0; col < size; col++) {
if(e.getSource() == board[row][col]) {
if(game.getCell(row, col).isFlagged()) {
game.getCell(row,col).setFlagged(false);
}
else {
game.getCell(row,col).setFlagged(true);
}
}
}
}
displayBoard();
}
// if game is won notify player and start a new game
if(game.getGameStatus() == GameStatus.Won) {
JOptionPane.showMessageDialog(null, "You Won!");
game.reset();
winCount++;
displayBoard();
repaint();
}
// if game is lost notify player and start a new game
if(game.getGameStatus() == GameStatus.Lost) {
JOptionPane.showMessageDialog(null, "You Lose.");
game.reset();
lostCount++;
displayBoard();
repaint();
}
}
// the following methods are to satisfy the
// internal class of MouseListener requirements
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
}
}
公共级扫雷板扩展了JPanel{
//所有变量和组件的声明
私人行动听者;
私人电池;
//私人图标emptyIcon;
//私人图标;
//私人图标;
私有整数大小;
私人采矿;
私人整数损失账户;
私人温克蒙特;
私有JButton[]]板;
私有JButton按钮;
私有JButton重置按钮;
私人JLabel lostLabel;
私有JLabel-winLabel;
私人JPanel-somePanel;
私人JPanel小组;
私人扫雷游戏;
私人鼠标器;
/**********************************************************************
*默认构造函数,用于创建
*gui包括所有组件的定位。
*********************************************************************/
公共扫雷板(){
//所有组件和变量的初始化
//在构造函数中使用的。
设置大小();
winCount=0;
lostCount=0;
board=新的JButton[尺寸][尺寸];
somePanel=newjpanel();
otherPanel=newjpanel();
游戏=新扫雷游戏(大小,地雷);
winLabel=新的JLabel(“Wins:+winCount”);
lostLabel=新的JLabel(“loss:+lostCount”);
quitButton=新的JButton(“退出”);
resetButton=新的JButton(“新游戏”);
setLayout(新的GridLayout(大小、大小));
//emptyIcon=新图像图标(“empty_icon.png”);
//创建板(每个单元)并将其添加到面板
对于(int row=0;rowlistener = new ButtonListener();//init the listener first, currently the listener is NULL then continue your code
// initializations of all components and variables
// that are used in the constructor.
setSize();
winCount = 0;
lostCount = 0