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?
Asked
Active
Viewed 89 times
0
-
1Just some friendly words of advice: It is always always best to first try to come up with a solution, and then to post your best good-faith attempt at this solution with your question. The quality of the question improves greatly if you do this, and because of this, usually the quality of the answers do too. Also, you would learn so much from the effort. – Hovercraft Full Of Eels Mar 21 '17 at 19:02
-
Actually the solution was making enable five buttons in the btn5ActionPerformed() setting the setVisible(true) for another Jframe form and it was opened .But making buttons btn.Enable does not solve the case,because even just making button5 enabled it opens another JFrame.. – andu Mar 21 '17 at 19:10
-
Are you trying to create some kind of key code pad? Where you need to press a predefined sequence? – MadProgrammer Mar 21 '17 at 19:15
-
In a JFrame form which contains buttons,how to make that after clicking 5 butons one after another ,a Jframe form(wich exists) to appear? – andu Mar 21 '17 at 19:21
-
Use a counter...? – MadProgrammer Mar 21 '17 at 19:41
-
See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Mar 21 '17 at 23:28
1 Answers
1
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
.
However I suggest you to read The use of multiple JFrames, Good / Bad practice?. The general consensus says it's a bad practice.
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);
}
}
}
}
}
};