本文介绍了一一按下按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是Java语言的新手,并且已经在NetBeans IDE 8.2中创建了JFrame.JFrame包含8个直接从Swing调色板创建的按钮.这种情况是我试图在单击例如5个按钮后打开另一个JFrame表单.我知道在最后一个btnActionPerformed中使用setVisible(true)方法来显示另一种JFrame形式;我要问的是如何使单击5个按钮然后出现其他Jframe表单成为可能?如果有人知道我要问的内容,请帮助我找到解决方案?

Hello I am new to java language,and I have created a JFrame in NetBeans IDE 8.2 .The JFrame contains 8 buttons created diretly from swing palette.The case is that I am trying to open another JFrame form after clicking for example 5 buttons.I know that for appearing another JFrame form it is used setVisible(true) method, in the last btnActionPerformed;What I am asking is that how to make possible clicking 5 buttons and then appear the other Jframe form??If somebody knows what I am asking please help me to find the solution?

推荐答案

您可以拥有一个计数器变量,每次您按一下按钮时,它的值就会增加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);
                    }
                }
            }
        }
    }
};

这篇关于一一按下按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 12:34