Java Swing - 使用 getComponent() 更新所有 JButton

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18704904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:20:09  来源:igfitidea点击:

Swing - using getComponent() to update all JButtons

javaswingjbutton

提问by jimbo123

I am making a tictactoe game where each board piece is represented by a JButton. When someone clicks the button the text is changed to "X" or "O". I am writing a reset function which resets the text in all the buttons to "". I am accessing all the buttons from an array using getComponents() method.

我正在制作一个 tictactoe 游戏,其中每个棋盘都由一个 JButton 表示。当有人单击按钮时,文本会更改为“X”或“O”。我正在编写一个重置函数,它将所有按钮中的文本重置为“”。我正在使用 getComponents() 方法访问数组中的所有按钮。

I just wondered what I am doing wrong because this bit compiles correctly

我只是想知道我做错了什么,因为这部分编译正确

component[i].setEnabled(true);

but this bit does not

但这一点没有

component[i].setText("");

I get a "cannot find symbol" error. Please have a look at the code below. I only included the code I thought was necessary.

我收到“找不到符号”错误。请看下面的代码。我只包含了我认为必要的代码。

    JPanel board = new JPanel(new GridLayout(3, 3));

    JButton button1 = new JButton("");
    JButton button2 = new JButton("");
    JButton button3 = new JButton("");
    JButton button4 = new JButton("");
    JButton button5 = new JButton("");
    JButton button6 = new JButton("");
    JButton button7 = new JButton("");
    JButton button8 = new JButton("");
    JButton button9 = new JButton("");

    board.add(button1);
    board.add(button2);
    board.add(button3);
    board.add(button4);
    board.add(button5);
    board.add(button6);
    board.add(button7);
    board.add(button8);
    board.add(button9);

public void reset()
{
    Component[] component = board.getComponents();

    // Reset user interface
    for(int i=0; i<component.length; i++)
    {
        component[i].setEnabled(true);
        component[i].setText("");
    }

        // Create new board logic
        tictactoe = new Board();
        // Update status of game
        this.updateGame();
}

采纳答案by Laf

getComponents ()returns an array of Components, which does not have a setText(String)method. You should either keep your JButtoninstances as class members (this is the way I strongly suggest), and use them directly, or loop through all the Componentobjects, check if it is a JButtoninstance. If it is, explicitly cast it as a JButton, then call setText(String)on it. E.g.

getComponents ()返回一个Component没有setText(String)方法的s数组。您应该将您的JButton实例保留为类成员(这是我强烈建议的方式),并直接使用它们,或者循环遍历所有Component对象,检查它是否是一个JButton实例。如果是,则将其显式转换为 a JButton,然后调用setText(String)它。例如

public void reset()
{
    Component[] component = board.getComponents();

    // Reset user interface
    for(int i=0; i<component.length; i++)
    {
        if (component[i] instanceof JButton)
        {
            JButton button = (JButton)component[i];
            button.setEnabled(true);
            button.setText("");
        }

    }
}