简体   繁体   中英

JButton to change JButton text on click

I am creating a reversi game and want to know how to change button text from B to W or vice versa on click.

I have tried adding action listners to my forloop which creates my board for the game and when i click a button labelled W it changed the whole of the first row of buttons into B's. what i want it to do it change only the clicked button to the opposite if B initially after click change to W

                                if (src == buttonPos[x][y]
                                        && buttonPos[x][y].getText() == currentPlayer) {
                                    buttonPos[x][y].setText("W");
                                } else {
                                    buttonPos[x][y].setText("B");
                                }

expected output is the button that's clicked will change to the opposite letter so if b initially it will change to W.

what i want it to do it change only the clicked button to the opposite

Your ActionListener code is too complex. There is no need for the looping code. You already know the source of the button so you just apply your if/else logic to that button.

Also, there is no need to create multiple ActionListeners. The logic is the same for all buttons so you can share the same ActionListener with each button.

So your code might look something like:

ActionListener al = new ActionListener() 
{
    @Override
    public void actionPerformed(ActionEvent e) 
    {
        JButton src = (JButton) e.getSource();
        String text = button.getText();
        button.setText( text.equals("W") ? "B" : "W" );
    }
});

for (int x = 0; x < buttonPos.length; x++) {
    for (int y = 0; y < buttonPos[x].length; y++) {
        buttonPos[x][y] = new JButton();
        reversiBoard.add(buttonPos[x][y]);
        buttonPos[x][y].addActionListener( al ); // changed
    }
}

You don't need to iterate over all the buttons in your actionPerformed() method. Also remember that you should use object.equals() instead of the == operator when checking for equality between objects.

Replace your actionPerformed() method with the following:

public void actionPerformed(ActionEvent e){
    JButton button = (JButton) e.getSource();
    button.setText(“W”.equals(button.getText()) ? “B” : “W”);
}

Here is the whole file. I have moved the listener to a separate method, that feels cleaner.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class Board {
    private final JButton[][] buttonPos = new JButton[8][8];
    private JPanel reversiBoard = new JPanel();
    private String currentPlayer;

    public Board() {
        currentPlayer = "B";
        JFrame frame = new JFrame();
        frame.getContentPane().add(reversiBoard);
        reversiBoard.setLayout(new GridLayout(8, 8, 2, 2));
        for (int x = 0; x < buttonPos.length; x++) {
            for (int y = 0; y < buttonPos[x].length; y++) {
                JButton button = new JButton();
                reversiBoard.add(button);
                button.addActionListener(this::changeButtonOwner);
                buttonPos[x][y] = button;
            }
        }
        frame.setSize(500, 500);
        frame.setVisible(true);
        buttonPos[4][3].setText("W");
        buttonPos[3][3].setText("B");
        buttonPos[4][4].setText("B");
        buttonPos[3][4].setText("W");
    }

    private void changeButtonOwner(ActionEvent e) {
        JButton src = (JButton) e.getSource();
        if (src.getText().equals("B")) src.setText("W");
        else if (src.getText().equals("W")) src.setText("B");
    }

    public JPanel getPanel() {
        return reversiBoard;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM