Reputation: 13
I want to write a little programm. There is defined number of cycles the programm shall run. The User inserts text in a Jtextfield. Everytime the user hits enter, one cycle is counted and the same JFrame shall be created again. This procedure repeats until the max. number of cycles is reached. The programm shall create another Jframe. I tried to write the code, but the program does not increase the number of cycles.
public class oneJFrame extends javax.swing.JFrame {
private int Cycles = 0;
public oneJFrame() {
initComponents();
}
private void clsWinBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
dispose();
}
protected void playerNameFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (Cycles <= 4) {
System.out.println(Cycles );
dispose();
oneJFrame JFrame1 = new oneJFrame();
JFrame1.setVisible(true);
Cycles++;
} else {
anotherJFrame Jframe2 = new anotherJFrame ();
Jframe2.setVisible(true);
}
}
I think I know the problem that I´ve got: Evertime the If Statement is true and the Jframe is disposed and a new one is created the int Cycles is set to 0. If this is the problem, I have no Idea to solve this. Is there a need to dispose the Jframe or is there a way just to save the text in the textfield and then reload the frame? Or is there something else I could do?
I am using the NetBeans GUI Builder.
Upvotes: 1
Views: 138
Reputation: 813
This is happend because when you create a new OneFrame
, new Cycles
created and automaticly equals to 0.
To fix that, change the variable Cycles
to static
, so will be 1 variable to every OneFrame
you will create, and will not reset to 0 every time you create a new OneFrame
.
For example, use:
private static int Cycles = 0;
Instead of:
private int Cycles = 0;
Upvotes: 1