简体   繁体   English

JButton在点击时更改JButton文本

[英]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. 我正在创建一个reversi游戏,想知道如何在点击时将按钮文本从B更改为W,反之亦然。

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. 我尝试在我的forloop中添加动作列表器,为游戏创建我的棋盘,当我点击标有W的按钮时,它将整个第一行按钮改为B。 what i want it to do it change only the clicked button to the opposite if B initially after click change to W 我想要它做什么它只改变点击按钮到相反,如果B最初点击更改为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. 预期输出是单击的按钮将更改为相反的字母,因此如果b最初它将更改为W.

what i want it to do it change only the clicked button to the opposite 我想要它做什么只改变点击按钮到相反的方向

Your ActionListener code is too complex. 您的ActionListener代码太复杂了。 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. 您已经知道按钮的来源,因此您只需将if / else逻辑应用于该按钮即可。

Also, there is no need to create multiple ActionListeners. 此外,无需创建多个ActionListener。 The logic is the same for all buttons so you can share the same ActionListener with each button. 所有按钮的逻辑都相同,因此您可以与每个按钮共享相同的ActionListener。

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. 您无需遍历actionPerformed()方法中的所有按钮。 Also remember that you should use object.equals() instead of the == operator when checking for equality between objects. 还要记住,在检查对象之间的相等性时,应该使用object.equals()而不是==运算符。

Replace your actionPerformed() method with the following: 用以下内容替换actionPerformed()方法:

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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