You have two issues to address.
The first being the location of the code that should remove the JPanel
from the JFrame
:
if (Currentframe == 176) {
f.getContentPane().removeAll();
f.setVisible(false);
}
This condition is checked only once when you add the ActionListener
to the JButton
. So remove this code. This logic has to be moved to the actionPerformed
method in class Main
.
The second problem is your Timer
. It won't stop running automatically by only removing the JPanel
. You'll have to stop it explicitly (and optionally restart it when required).
A general note: don't start variable names with an uppercase letter. This is considered bad practice.
The solution:
public void actionPerformed(ActionEvent e) {
do {
repaint();
} while (Currentframe >= ImageArray.length);
if (Currentframe == 176) {
animate.stop();
this.getRootPane().removeAll();
//optional, without repainting the image which was painted last will remain visible
//JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this);
//topFrame.repaint();
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…