Affichage des articles dont le libellé est Timer In Java. Afficher tous les articles
Affichage des articles dont le libellé est Timer In Java. Afficher tous les articles

Java Timer In Eclipse

How To Use Timer Class In Java Using Eclipse

timer in java



In this Java Tutorial we will see How To Use Timer Class (Start, Stop, Delay, Action)
In Java Eclipse .




Project Source Code:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class Java_Timer extends JFrame {

private JPanel contentPane;
Timer tm;
int i = 0;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Java_Timer frame = new Java_Timer();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public Java_Timer() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 524, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnStart = new JButton("Start");
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
tm.start();
}
});
btnStart.setBounds(29, 87, 89, 23);
contentPane.add(btnStart);
JButton buttonStop = new JButton("Stop");
buttonStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tm.stop();
}
});
buttonStop.setBounds(380, 87, 89, 23);
contentPane.add(buttonStop);
JLabel label = new JLabel(".........");
label.setFont(new Font("Tahoma", Font.BOLD, 16));
label.setBounds(220, 73, 76, 50);
contentPane.add(label);
tm = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText(Integer.toString(i));
i++;
}
});
}
}





JAVA - How To Set A Timer In Java

JAVA - How To Set A Timer In Java NetBeans

In this java Tutorial we will see How To Make A Timer In Java NetBeans .



Project Source Code:

package JavaDB_001;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;


public class Test extends JFrame {
    Timer tm1,tm2;
    JButton up,down;
    JPanel panel;
    public Test(){
        super("Timer");
        up = new JButton("up");
        down = new JButton("down");
        up.setBounds(150, 400, 100, 20);
        down.setBounds(260, 400, 100, 20);
        panel = new JPanel();
        panel.setBackground(Color.decode("#3a5795"));
        panel.setBounds(0, 0, 500, 0);
        
        tm1 = new Timer(50,new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
            
                if(panel.getHeight() != 350){
                    up.setEnabled(false);
                    down.setEnabled(false);
                    panel.setBounds(0, 0, 500, panel.getHeight()+5);
                    if(panel.getHeight() == 350){
                        tm1.stop();
                    up.setEnabled(true);
                    down.setEnabled(true); 
                    }
                }
            }
        });
        
        tm2 = new Timer(50, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                
                if(panel.getHeight() !=0){
                         up.setEnabled(false);
                    down.setEnabled(false);
                        
                    panel.setBounds(0, 0, 500, panel.getHeight()-5);
                    if(panel.getHeight() == 0){
                        tm2.stop();
                       up.setEnabled(true);
                    down.setEnabled(true);
                    }
                }
            }
        });
        
        //Timer2 Start
        up.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                tm2.start();
            }
        });
                 //Timer1 Start
        down.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                tm1.start();
            }
        });
        
        
        
        add(up);
        add(down);
        add(panel);
        setLayout(null);
        setSize(500, 500);
        getContentPane().setBackground(Color.decode("#bdb76b"));
        setLocationRelativeTo(null);
        setVisible(true);
    }
  
 
 public static void main(String[] args){
 
         new Test();
    }
}

Also See :
Slideshow In Java Swing