Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

java - Static int wont trigger remove frame or change panel

I am creating a program using NetBeans 11.2 that has an animation in it. While creating it after the animation when the static variable (CurrentFrame is equal to 176) I want the Frame to remove the Jpanel so I can add pictures of cars to my program and move the pictures. so i made an if statement so when Currentframe == 176 it will remove said panel but it wont work no matter what i do. I did System.out.print to see if it even counted up to 176 and it did. So what's the problem any tips? package Frametest;

import javax.swing.*;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main extends JPanel implements ActionListener {

public final Timer animate;
public static Boolean z = false;
public final ImageIcon ImageArray[];
public static int delay = 50, FrameCount = 177, Currentframe = 0;

public Main() {

    ImageArray = new ImageIcon[FrameCount];

    //Array used to store 200 Frames 
    for (int i = 1; i < ImageArray.length; i++) {

        ImageArray[i] = new ImageIcon(getClass().getResource("Cartest (" + i + ").jpg"));

        //i++ causes the Array to constantly pull up an image that i have named 
        //from numbers 1-200
    }

    animate = new Timer(delay, this);
    animate.start();
    //Setting a delay using a timer

}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    System.out.println(Currentframe);
    Currentframe++;

    ImageArray[Currentframe].paintIcon(this, g, 0, 0);
    //each time this method is called the image is going to differentiate 

}

public void actionPerformed(ActionEvent e) {
    do {
        repaint();
    } while (Currentframe >= ImageArray.length);

    //clears the graphics and images and recalls the method
}

static class Action implements ActionListener {

    

    @Override
    public void actionPerformed(ActionEvent e) {

        JFrame f = new JFrame("hello");
       
        Main s = new Main();
        f.add(s, BorderLayout.CENTER);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(1280, 760);
        f.setVisible(true);

        if (Currentframe == 176) {
            f.getContentPane().removeAll();
            f.setVisible(false);
        }

    }

}

public static void main(String[] args) throws MalformedURLException, IOException, InterruptedException {
    //Title Page
    JFrame f = new JFrame("Button Example");
    JButton b = new JButton("Click Here");

    b.setBounds(50, 100, 95, 30);

    f.add(b);
    b.addActionListener(new Action());
    f.setSize(400, 400);
    f.setLayout(null);
    f.setVisible(true);   

}
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...