//When you want to do a process every one second
//you can use javax.swing.timer. You need to add
//actionLisenter for this like JButton
//the number n in the following will incremented
//and displayed in the JTextField after the timer
//has started for every one second
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class timertest extends JFrame implements ActionListener
{
JTextField jtf1;
JPanel pane;
int n=1;
javax.swing.Timer t1;
public timertest()
{
super("Timer test");
setSize(200,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jtf1=new JTextField(n+"",10);
t1=new javax.swing.Timer(1000,this);
t1.start();
pane=new JPanel();
pane.add(jtf1);
setContentPane(pane);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==t1)
{
n=n+1;
jtf1.setText(""+n);
}
}
public static void main(String args[])
{
timertest one=new timertest();
}
}
//you can use javax.swing.timer. You need to add
//actionLisenter for this like JButton
//the number n in the following will incremented
//and displayed in the JTextField after the timer
//has started for every one second
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class timertest extends JFrame implements ActionListener
{
JTextField jtf1;
JPanel pane;
int n=1;
javax.swing.Timer t1;
public timertest()
{
super("Timer test");
setSize(200,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jtf1=new JTextField(n+"",10);
t1=new javax.swing.Timer(1000,this);
t1.start();
pane=new JPanel();
pane.add(jtf1);
setContentPane(pane);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==t1)
{
n=n+1;
jtf1.setText(""+n);
}
}
public static void main(String args[])
{
timertest one=new timertest();
}
}
No comments:
Post a Comment