有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java静态int不会触发删除框架或更改面板

我正在使用NetBeans 11.2创建一个程序,其中包含一个动画。当静态变量(CurrentFrame等于176)在动画之后创建时,我希望该帧删除Jpanel,以便我可以将汽车图片添加到程序中并移动图片。所以我做了一个if声明,所以当Currentframe == 176它将移除上述面板,但无论我做什么,它都不会工作。我做了一个系统。出来打印出来看看它是否达到了176,它确实做到了。有什么问题吗?有什么建议吗? 包装框架测试

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

}
}

共 (1) 个答案

  1. # 1 楼答案

    你有两个问题需要解决

    第一个是应该JFrame中删除JPanel的代码的位置:

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

    ActionListener添加到JButton时,仅会检查一次此条件。所以删除这个代码。必须将此逻辑移到类Main中的actionPerformed方法

    第二个问题是你的Timer。它不会仅仅通过删除JPanel自动停止运行。您必须显式地停止它(并在需要时选择性地重新启动它)

    一般注意事项:不要以大写字母开头变量名。这被认为是不好的做法

    解决方案是:

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