动态值在摆动计时器中不起作用
Dynamic value not working in swing swing Timer
我正在尝试在 java 中使用摇摆计时器 class。当我对延迟进行硬编码时,代码是 运行 延迟时间。在这种情况下 5 秒
Timer timer= new Timer(5000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("running...");
}
});
但是,当我尝试使用构造函数动态设置延迟变量时,它是 运行 0 毫秒延迟。
Timer timer= new Timer(delay, new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("running...");
}
});
请告诉我如何在定时器中动态设置延迟 class。
完整代码:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Timer;
class StartStop implements ActionListener{
JFrame f=new JFrame();
JButton startButton=new JButton("START");
JButton stopButton=new JButton("STOP");
int delay;
Timer timer= new Timer(delay, new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("running...");
}
});
public StartStop(int interval) {
this.delay=interval; // setting the delay variable to 5 seconds
System.out.println(delay);
startButton.setBounds(50,10,95,30);
startButton.setFocusable(false);
startButton.addActionListener(this);
stopButton.setBounds(200,10,95,30);
stopButton.setFocusable(false);
stopButton.addActionListener(this);
f.add(startButton);
f.add(stopButton);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400,100);
f.setLayout(null);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==startButton)
{
System.out.println("started");
start();
}
if(e.getSource()==stopButton)
{
System.out.println("stopped");
stop();
}
}
void start()
{
timer.setInitialDelay(0);
timer.start();
}
void stop()
{
timer.stop();
}
public static void main(String[] args) {
StartStop ss=new StartStop(5000); //passing a 5 second delay to constructor
}
}
您当前代码的问题是您创建了一个新的计时器,其延迟还没有值,变为 0。
您可以通过添加以下内容来解决此问题:
void start() {
timer.setDelay(delay);
timer.start();
}
它是 0,因为当您创建定时器时,delay
的值是 0。
class StartStop implements ActionListener{
JFrame f=new JFrame();
JButton startButton=new JButton("START");
JButton stopButton=new JButton("STOP");
int delay;
Timer timer;
public StartStop(int interval) {
this.delay=interval; // setting the delay variable to 5 seconds
System.out.println(delay);
timer= new Timer(delay, new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("running...");
}
});
我正在尝试在 java 中使用摇摆计时器 class。当我对延迟进行硬编码时,代码是 运行 延迟时间。在这种情况下 5 秒
Timer timer= new Timer(5000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("running...");
}
});
但是,当我尝试使用构造函数动态设置延迟变量时,它是 运行 0 毫秒延迟。
Timer timer= new Timer(delay, new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("running...");
}
});
请告诉我如何在定时器中动态设置延迟 class。
完整代码:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Timer;
class StartStop implements ActionListener{
JFrame f=new JFrame();
JButton startButton=new JButton("START");
JButton stopButton=new JButton("STOP");
int delay;
Timer timer= new Timer(delay, new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("running...");
}
});
public StartStop(int interval) {
this.delay=interval; // setting the delay variable to 5 seconds
System.out.println(delay);
startButton.setBounds(50,10,95,30);
startButton.setFocusable(false);
startButton.addActionListener(this);
stopButton.setBounds(200,10,95,30);
stopButton.setFocusable(false);
stopButton.addActionListener(this);
f.add(startButton);
f.add(stopButton);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400,100);
f.setLayout(null);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==startButton)
{
System.out.println("started");
start();
}
if(e.getSource()==stopButton)
{
System.out.println("stopped");
stop();
}
}
void start()
{
timer.setInitialDelay(0);
timer.start();
}
void stop()
{
timer.stop();
}
public static void main(String[] args) {
StartStop ss=new StartStop(5000); //passing a 5 second delay to constructor
}
}
您当前代码的问题是您创建了一个新的计时器,其延迟还没有值,变为 0。 您可以通过添加以下内容来解决此问题:
void start() {
timer.setDelay(delay);
timer.start();
}
它是 0,因为当您创建定时器时,delay
的值是 0。
class StartStop implements ActionListener{
JFrame f=new JFrame();
JButton startButton=new JButton("START");
JButton stopButton=new JButton("STOP");
int delay;
Timer timer;
public StartStop(int interval) {
this.delay=interval; // setting the delay variable to 5 seconds
System.out.println(delay);
timer= new Timer(delay, new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("running...");
}
});