JAVA İLE TİMER KULLANIMI
Eclipse IDE sinde Java ile Windows uygulamasında timer kullanımı. 2 adet timer ile saat yapmak. Timerlerin buttonlarla kontrolü. Konu anlatımı ve örnek program.
Bu uygulamada Java Windows form uygulaması ile 2 adet timer kullanılacak ve her biri birer fonksiyona atanacaktır. Timerlere farklı değerler verilerek sayıcıların farklı hızda çalışması anlatılacaktır. İstenirse her timer buttonlar ile durdurulacak ve tekrar çalıştırılabilecektir. Bu uygulamanın daha gelişmiş bir formu olan resimleri timer ile hareket ettirme konusunun linki sayfa sonunda yer almaktadır.
x, y,z, x2, y2 ve z2 değişkenleri timerin aralık değeri müddetince sayacaklardır. Timerlerin biri her 1000 ms de bir sayarken diğeri 100 ms de bir saymaktadır. Ayrıca x ve y değerlerinin belli bir sayıdan sonra sıfırlanarak tekrar başa dönmesi sağlanmıştır. Böylece saat yapımı ve gelişmiş sayıcı yapımları hakkında da bir fikir verilmiştir. Uygulamayı siz istediğiniz gibi editleyebilir, geliştirebilirsiniz.
Uygulamaya gelince timer nesneleri başlangıçta global olarak oluşturuluyor.
static Timer timer1;
static Timer timer2;
Daha sonra ana fonksiyon içinde timer nesnesi tanımlanıyor.
timer1 = new Timer(100, new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
Motion();
}
});
Timerin çalışma sıklığı 100 ms olarak belirleniyor ve eventi oluşturuluyor. Biz eventini burada kalabalık tutmamak için farklı bir fonksiyonda işlemleri yaptık ve o fonksiyonu buradan çağırdık.

Windows form kontrolleri ve resim hareketi ile ilgili örneklerin linki sayfa sonundadır.
Kolay gelsin.
Tam Program Kodları
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Timex
{
static int x;
static int y;
static int z;
static int x2;
static int y2;
static int z2;
static JFrame Window1;
static JLabel picture1;
static JLabel etiket1;
static JLabel etiket2;
static JButton button1;
static JButton button2;
static JButton button3;
static JButton button4;
static Timer timer1;
static Timer timer2;
public static void main(String[] Args)
{
Window1=new JFrame("Timer Kullanımı");
Window1.setSize(1024,768);
Window1.setExtendedState(JFrame.MAXIMIZED_BOTH);
Window1.setLocation(5,5);
Window1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Controllers();
Window1.setLayout(null);
Window1.setVisible(true);
timer1 = new Timer(100, new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
Motion();
}
});
timer2=new Timer(1000,new ActionListener() {
public void actionPerformed(ActionEvent e)
{
Motion2();
}
});
}
public static void Controllers()
{
etiket1=new JLabel();
etiket1.setSize(250,60);
etiket1.setLocation(210,250);
etiket1.setText("NESNELERİN HAREKETİ1");
etiket1.setForeground(Color.red);
etiket1.setFont(new Font("Roman",Font.PLAIN,18));
Window1.add(etiket1);
etiket2=new JLabel();
etiket2.setSize(250,60);
etiket2.setLocation(620,250);
etiket2.setText("NESNELERİN HAREKETİ2");
etiket2.setForeground(Color.red);
etiket2.setFont(new Font("Roman",Font.PLAIN,18));
Window1.add(etiket2);
button1=new JButton();
button1.setText("BAŞLAT-1");
button1.setSize(150,50);
button1.setLocation(220,380);
button2=new JButton();
button2.setText("DURDUR-1");
button2.setSize(150,50);
button2.setLocation(220,450);
button3=new JButton();
button3.setText("BAŞLAT-2");
button3.setSize(150,50);
button3.setLocation(625,380);
button4=new JButton();
button4.setText("DURDUR-2");
button4.setSize(150,50);
button4.setLocation(625,450);
button1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Button1_Click();
}
}
);
button2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Button2_Click();
}
}
);
button3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Button3_Click();
}
}
);
button4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Button4_Click();
}
}
);
Window1.add(button1);
Window1.add(button2);
Window1.add(button3);
Window1.add(button4);
}
public static void Motion()
{
x++;
if(x==99)
{
x=0;
y++;
}
if(y==59)
{
y=0;
z++;
}
if(z==59)
{
x=0;
y=0;
z=0;
}
etiket1.setText(z+" : "+y+" : "+x);
}
public static void Motion2()
{
x2++;
if(x2==59)
{
x2=0;
y2++;
}
if(y2==59)
{
y2=0;
z2++;
}
if(z2==59)
{
x2=0;
y2=0;
z2=0;
}
etiket2.setText(z2+" : "+y2+" : "+x2);
}
private static void Button1_Click()
{
timer1.start();
}
private static void Button2_Click()
{
timer1.stop();
}
private static void Button3_Click()
{
timer2.start();
}
private static void Button4_Click()
{
timer2.stop();
}
}
Java İle Windows Form Oluşturma
Java İle Resim Hareket Ettirme
SANATSAL BİLGİ
22/02/2019