java - 二维数组和 JApplet : button visibility and label change

标签 java arrays button applet visibility

我遇到了 2 个比较简单的问题:

1) 我试图将 btns[0][0](第 0 行,第 0 列)设置为我的 00-99 按钮网格上唯一可见的按钮,在我按下按钮,触摸它的按钮变得可见,并且该过程继续直到所有按钮都可见。

2) 按下每个按钮后,按钮(即按下,而不是每个按钮)将其数字标签更改为笑脸,setLabel(":)")

public class Project5 extends JApplet implements ActionListener {

    JButton button;
    Container contentPane;
    JButton[][] btns = new JButton[10][10];
    int clicks = 0;

    public void init() {

        setSize(600, 600);
        contentPane = getContentPane();
        contentPane.setBackground(Color.WHITE); 


        GridLayout grid = new GridLayout(10,10,0,4);
        contentPane.setLayout(grid);
        clicks = 0;

        for (int i = 0; i < 10; i++) {

            for (int j = 0; j < 10; j++) {

                btns[i][j] = new JButton();                
                button = new JButton(""+i+j);
                button.addActionListener(this);
                contentPane.add(button);
                //button.setVisible(false);
            }
        }

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Container contentPane = getContentPane();

        btns[0][0].setVisible(true);

        String button_label = e.getActionCommand();

        int row = Character.getNumericValue(button_label.charAt(0));
        int column = Character.getNumericValue(button_label.charAt(1));          


        clicks++;

        if (clicks % 5 == 0) {
            contentPane.setBackground(Color.PINK);
        } else if (clicks % 5 == 1) {
            contentPane.setBackground(Color.GREEN);
        } else if (clicks % 5 == 2) {
            contentPane.setBackground(Color.BLUE);
        } else if (clicks % 5 == 3) {
            contentPane.setBackground(Color.YELLOW);
        } else if (clicks % 5 == 4) {
            contentPane.setBackground(Color.RED);          
        }

    }

    public void checkDone() {
        //if all buttons visible, change contentPane color to black.

    }

}

最佳答案

您的按钮可见性问题源于您在循环中创建了两个按钮。
您将其中一个放入数组中,并在另一个上完成所有工作。

代替这个

btns[i][j] = new JButton();                
button = new JButton(""+i+j);

你想这样做

button = new JButton(""+i+j);  
btns[i][j] = button; 

关于您的 actionPerformed() 方法,只要单击一个按钮,它就会创建一个 ActionEvent 并向其监听器“触发”它。这意味着它将遍历所有这些监听器并在每个监听器上调用 actionPerformed()。每当调用监听器时,传递给它的事件都有一个特殊的 Object 内部称为 source(即触发事件的任何东西)。

在您的情况下,源将是被点击的按钮,您可以获得被点击的按钮,并获得其标签文本,如下所示:

    JButton button = (JButton) e.getSource();
    String label = button.getText();
    button.setText(":)");

然后您需要做的就是解析 label 以获取单击按钮的坐标。
一旦你有了坐标,你就可以找出邻居并将它们设置为可见。

关于java - 二维数组和 JApplet : button visibility and label change,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23439318/

相关文章:

php - 在不使用foreach In PHP的情况下,将方程式应用于关联数组的每个值

JavaFX如何将 "crop"图形转为按钮

xcode - 将操作链接到模态/弹出窗口上的按钮(swift 2 Xcode 7)

java - 在将类的变量存储在java中之前对其进行加密

java - tomcat启动时ContextLoaderListener异常

javascript - 最小-最大总和 - 输出错误

jquery - 随机音频onmouseover jQuery不起作用

java - 处理: different response format for REST API and web pages时出错

java - 具有私有(private)构造函数的类中静态变量的初始化

css - 如何使按钮在单击时消失并显示另一个按钮?