我的GUI框架现在出现但我从滑块获得的滑块值(速度)并没有出现在我的ActionLlistener类中,我需要将它用作计时器的延迟。我如何实现这一价值?关键是要运行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();
}
}
}
答案 0 :(得分:1)
不要依赖static
,它会在你脸上爆炸......
如果你真的只想创建一个实例并将其应用到SliderListener
(以及你的情况下)JSlider
,那么你正在创建多个实例Timer
。
话虽如此,我(亲自)将他们分开......
每次调用Timer
时你也会创建一个新的ActionListener
(就像每秒一千次!所以在1秒之后,你可以运行1001 Timer
!)所有这些都将同时调用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
时,都应该将其视为循环的迭代并相应地更新状态......