且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

一一按下按钮

更新时间:2023-12-05 19:48:04

您可以拥有一个计数器变量,每次您按一下按钮时,它的值就会增加1,当该值是5时,您可以在其上调用setVisible您的第二个JFrame.

You could have a counter variable that each time you clic on a button it increases by 1 its value and when that value is 5, you call setVisible on your second JFrame.

但是我建议您阅读使用多个JFrame ,好/不好的做法?.普遍的共识是,这是一种不好的做法.

However I suggest you to read The use of multiple JFrames, Good / Bad practice?. The general consensus says it's a bad practice.

由于您没有提供代码,所以我只能通过下面的图像和ActionListener代码向您展示,但是您必须自己实现此解决方案:

As you provided not code, I can only show you that it's possible with the below image and the ActionListener code, however you must implement this solution on your own:

ActionListener listener = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLS; j++) {
                if (e.getSource().equals(buttons[i][j])) {
                    clics++;

                    sequenceLabel.setText("Number of Clics: " + clics);
                    if (clics == 5) {
                        clics = 0;
                        frame2.pack();
                        frame2.setLocationRelativeTo(frame1);
                        frame2.setVisible(true);
                    }
                }
            }
        }
    }
};