GUI 动画:Action 和 Change 之间的滑块值 类?

GUI Animation: Slider Value between Action and Change classes?

我的 GUI 框架现在出现了,但是我从滑块获得的滑块值(速度)没有出现在我的 ActionLlistener class 中,我需要将它用作计时器的延迟。我如何把这个价值带过来?重点是 运行 12 个图像,如帧,其速度由它们滑动的值决定。就像他们滑动到 12,每张图像之间会有 12 毫秒。

import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.*;

public class SliderGUI {

    public static JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 1);
    public static JLabel label = new JLabel ();
    public static JPanel panel = new JPanel();
    public static int delay;
    public static int speed;
    public static ImageIcon imageIcon;
    public static Timer timer = new Timer (delay, new SliderListener());

    public static void main(String[] args) {    
        JFrame frame = new JFrame("Legend Of Zelda");
        panel.setLayout(new GridLayout(5, 5, 5, 25));
        slider.setPaintLabels(true);
        slider.addChangeListener(new SliderListener());
        System.out.println (speed);
        timer.addActionListener (new SliderListener());

        frame.setVisible(true);
        frame.setResizable(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));

        panel.add(label);
        panel.add(slider);
        frame.setContentPane(panel);
        frame.pack();
    }

    private static class SliderListener implements ChangeListener, ActionListener {

        public void stateChanged(ChangeEvent e) {
            speed = slider.getValue();
            slider.setMajorTickSpacing(25);

        }

        public void actionPerformed (ActionEvent e) {
            for (int i = 1; i < 13; i++) {
                if (i == 12){
                    i = 1;
                }
                imageIcon = new ImageIcon(i + ".jpg");
                label.setIcon(imageIcon);
            }
            System.out.println ("Hi");
            timer = new Timer(speed, new SliderListener());
            timer.start();
        }
    }
}

不要依赖static,它会在你脸上爆炸...

您正在创建多个实例 SliderListener,而您实际上应该只创建一个并将其应用于 JSlider(在您的情况下)Timer

话虽如此,我(个人)将它们分开...

每次调用 ActionListener 时,您也会创建一个新的 Timer(就像每秒一千次!所以 1 秒后,您可能会得到 1001 Timer s 运行!) 所有这些都将同时调用 SliderListener,并且因为它们都是通过全局变量链接的...面临爆炸...

但我实际上没有看到您在什么地方说 Timer 开头...

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SliderGUI {

    public static void main(String[] args) {
        new SliderGUI();
    }

    public SliderGUI() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public JSlider slider = new JSlider(JSlider.HORIZONTAL, 10, 100, 10);
        public JLabel label;
        public int delay;
        public int speed;
        public ImageIcon imageIcon;
        public Timer timer;

        public TestPane() {
            setLayout(new GridLayout(5, 5, 5, 25));
            slider.setPaintLabels(true);

            label = new JLabel();
            try {
                BufferedImage frameImage = ImageIO.read(getClass().getResource("/Run-0.png"));
                label.setIcon(new ImageIcon(frameImage));
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            SliderListener sliderListener = new SliderListener();
            timer = new Timer(delay, sliderListener);
            slider.addChangeListener(sliderListener);
            System.out.println(speed);
            timer.addActionListener(sliderListener);

            timer.start();

            setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
            setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

            add(label);
            add(slider);
        }

        private class SliderListener implements ChangeListener, ActionListener {

            public void stateChanged(ChangeEvent e) {

                int value = slider.getValue();
                timer.setDelay(value);

            }

            private int frame = 0;

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Tick " + ((Timer) e.getSource()).getDelay());
                try {
                    BufferedImage frameImage = ImageIO.read(getClass().getResource("/Run-" + frame + ".png"));
                    label.setIcon(new ImageIcon(frameImage));
                } catch (IOException exp) {
                    exp.printStackTrace();
                }
                frame++;
                if (frame > 11) {
                    frame = 0;
                }
            }
        }

    }

}

此外,请记住,Timer 就像一个循环,每次调用 ActionListener 时,您应该将其视为循环的迭代并相应地更新状态...