Не работает MouseListener - Java

Узнай цену своей работы

Формулировка задачи:

Добрый день. Пытался написать крестики-нолики, но нажатие на кнопку не срабатывает, сам так и не понял, почему.
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
 
import static java.awt.GridBagConstraints.*;
 
public class TicTacToe extends JFrame{
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;
    private JButton button5;
    private JButton button6;
    private JButton button7;
    private JButton button8;
    private JButton button9;
    private JLabel jLabel;
    private GridBagLayout layout = new GridBagLayout();
    private JPanel panel = new JPanel();
    private Font font = new Font("Times New Roman", Font.PLAIN, 70);
 
    private static final String PLAYER_ONE = "Player One";
    private static final String PLAYER_TWO = "Player Two";
    private TicTacToeEngine ticTacToeEngine;
    private String currentPlayer = PLAYER_ONE;
 
    TicTacToe() {
        this.setTitle("Tic-tac-toe");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setSize(300,400);
        this.setResizable(false);
        panel.setLayout(layout);
 
        //установка кнопок
        button1 = getButton(0,0);
        button2 = getButton(0,1);
        button3 = getButton(0,2);
        button4 = getButton(1,0);
        button5 = getButton(1,1);
        button6 = getButton(1,2);
        button7 = getButton(2,0);
        button8 = getButton(2,1);
        button9 = getButton(2,2);
 
        //установка поля с именем игрока
        jLabel = new JLabel(currentPlayer+"'s turn");
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.weightx = 1;
        gridBagConstraints.weighty = 1;
        gridBagConstraints.anchor = CENTER;
        gridBagConstraints.fill = BOTH;
        gridBagConstraints.gridheight = 1;
        gridBagConstraints.gridwidth = 3;
        jLabel.setFont(new Font("Times New Roman", Font.BOLD, 30));
        panel.add(jLabel, gridBagConstraints);
 
        this.setContentPane(panel);
 
        this.setVisible(true);
    }
 
    JButton getButton(int row, int column) {
        JButton jButton = new JButton();
 
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = column;
        gridBagConstraints.gridy = row;
        gridBagConstraints.weightx = 1;
        gridBagConstraints.weighty = 1;
        gridBagConstraints.anchor = CENTER;
        gridBagConstraints.fill = BOTH;
        gridBagConstraints.gridheight = 1;
        gridBagConstraints.gridwidth = 1;
 
        jButton.setFont(font);
        jButton.addMouseListener(ticTacToeEngine);
        panel.add(jButton, gridBagConstraints);
        return jButton;
    }
 
    void erase() {
        button1.setText("");
        button1.setText("");
        button1.setText("");
        button2.setText("");
        button2.setText("");
        button2.setText("");
        button3.setText("");
        button3.setText("");
        button3.setText("");
        setPlayerName(PLAYER_ONE);
    }
 
    void setPlayerName(String name) {
        currentPlayer = name;
    }
 
    String getPlayerName() {
        return currentPlayer;
    }
 
    class TicTacToeEngine extends MouseAdapter {
        private TicTacToe ticTacToe;
        TicTacToeEngine(TicTacToe ticTacToe) {
            this.ticTacToe = ticTacToe;
        }
 
        @Override
        public void mouseClicked(MouseEvent e) {
            JButton clickedButton = (JButton)e.getComponent();
            clickedButton.setText("a");
        }
    }
 
    public static void main(String[] args) {
        TicTacToe ticTacToe = new TicTacToe();
    }
}

Решение задачи: «Не работает MouseListener»

textual
Листинг программы
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
 
import static java.awt.GridBagConstraints.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
 
public class TicTacToe extends JFrame{
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;
    private JButton button5;
    private JButton button6;
    private JButton button7;
    private JButton button8;
    private JButton button9;
    private JLabel jLabel;
    private GridBagLayout layout = new GridBagLayout();
    private JPanel panel = new JPanel();
    private Font font = new Font("Times New Roman", Font.PLAIN, 70);
 
    private static final String PLAYER_ONE = "Player One";
    private static final String PLAYER_TWO = "Player Two";
    
    private String currentPlayer = PLAYER_ONE;
 
    TicTacToe() {
        this.setTitle("Tic-tac-toe");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setSize(300,400);
        this.setResizable(false);
        panel.setLayout(layout);
 
        //установка кнопок
        button1 = getButton(0,0);
        button2 = getButton(0,1);
        button3 = getButton(0,2);
        button4 = getButton(1,0);
        button5 = getButton(1,1);
        button6 = getButton(1,2);
        button7 = getButton(2,0);
        button8 = getButton(2,1);
        button9 = getButton(2,2);
 
        //установка поля с именем игрока
        jLabel = new JLabel(currentPlayer+"'s turn");
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.weightx = 1;
        gridBagConstraints.weighty = 1;
        gridBagConstraints.anchor = CENTER;
        gridBagConstraints.fill = BOTH;
        gridBagConstraints.gridheight = 1;
        gridBagConstraints.gridwidth = 3;
        jLabel.setFont(new Font("Times New Roman", Font.BOLD, 30));
        panel.add(jLabel, gridBagConstraints);
 
        this.setContentPane(panel);
 
        this.setVisible(true);
    }
    
    JButton getButton(int row, int column) {
        JButton jButton = new newButton();
 
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = column;
        gridBagConstraints.gridy = row;
        gridBagConstraints.weightx = 1;
        gridBagConstraints.weighty = 1;
        gridBagConstraints.anchor = CENTER;
        gridBagConstraints.fill = BOTH;
        gridBagConstraints.gridheight = 1;
        gridBagConstraints.gridwidth = 1;
        
        
        jButton.setFont(font);
        
        panel.add(jButton, gridBagConstraints);
        return jButton;
    }
 
    void erase() {
        button1.setText("");
        button1.setText("");
        button1.setText("");
        button2.setText("");
        button2.setText("");
        button2.setText("");
        button3.setText("");
        button3.setText("");
        button3.setText("");
        setPlayerName(PLAYER_ONE);
    }
 
    void setPlayerName(String name) {
        currentPlayer = name;
    }
 
    String getPlayerName() {
        return currentPlayer;
    }
    
    class newButton extends JButton{
        public newButton(){
            addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e) {        
                    setText("a");
                }
            });
        }
    }
 
    public static void main(String[] args) {
        TicTacToe ticTacToe = new TicTacToe();
    }
}

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

12   голосов , оценка 4.167 из 5