Hello there.
Here's another program I want to present to you: it's a self-running experiment that tests the Timer class provided in JAVA. This class fires an action event at specified intervals of time, with a delay in between. You specify the delay you want in the Timer object, then when it comes time to use it, use the methods accordingly.
For this case, we want to test the Timer class for incrementing a value. To show the roll, this is done on a GUI program. You are welcome to modify this as you wish.
How it works is, for every 60 milliseconds, the Timer object fires an action event that upon implementation of the actionPerformed method, the value displayed is incremented with the new value displayed.
Here's the code to this experiment. There is no documentation provided.
Here's another program I want to present to you: it's a self-running experiment that tests the Timer class provided in JAVA. This class fires an action event at specified intervals of time, with a delay in between. You specify the delay you want in the Timer object, then when it comes time to use it, use the methods accordingly.
For this case, we want to test the Timer class for incrementing a value. To show the roll, this is done on a GUI program. You are welcome to modify this as you wish.
How it works is, for every 60 milliseconds, the Timer object fires an action event that upon implementation of the actionPerformed method, the value displayed is incremented with the new value displayed.
Here's the code to this experiment. There is no documentation provided.
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
public class NumberLoop extends JFrame
{
private JPanel panel;
protected JLabel number;
private int currentNumber = 0;
private final int max = 100;
private Timer loopController;
private final short loopDelayLength = 60; // 40 milliseconds
public static void main(String[] args)
{
new NumberLoop();
}
public NumberLoop()
{
setTitle("GUI Number Loop [Experimental]");
setSize(40,70);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
buildPanel();
add(panel, BorderLayout.CENTER);
setVisible(true);
init();
}
public void init()
{
loopController = new Timer(loopDelayLength, new TimerListener());
loopController.start();
}
private void buildPanel()
{
panel = new JPanel();
number = new JLabel(Integer.toString(currentNumber));
panel.add(number);
}
private class TimerListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
currentNumber++;
number.setText(Integer.toString(currentNumber));
if (currentNumber == max)
loopController.stop();
}
}
}
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
public class NumberLoop extends JFrame
{
private JPanel panel;
protected JLabel number;
private int currentNumber = 0;
private final int max = 100;
private Timer loopController;
private final short loopDelayLength = 60; // 40 milliseconds
public static void main(String[] args)
{
new NumberLoop();
}
public NumberLoop()
{
setTitle("GUI Number Loop [Experimental]");
setSize(40,70);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
buildPanel();
add(panel, BorderLayout.CENTER);
setVisible(true);
init();
}
public void init()
{
loopController = new Timer(loopDelayLength, new TimerListener());
loopController.start();
}
private void buildPanel()
{
panel = new JPanel();
number = new JLabel(Integer.toString(currentNumber));
panel.add(number);
}
private class TimerListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
currentNumber++;
number.setText(Integer.toString(currentNumber));
if (currentNumber == max)
loopController.stop();
}
}
}