1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Thermometre extends JFrame
{
Timer timer;
JLabel lblTemp, lblDate, lblTime;
RandomAccessFile rd;
public Thermometre()
{
lblDate = new JLabel();
lblTime = new JLabel();
lblTemp = new JLabel();
//L'action qui sera exécutée par le timer
ActionListener actionListener = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
try{
String val[] = getValues();
lblDate.setText(val[0]);
lblTime.setText(val[1]);
lblTemp.setText(val[2]);
} catch (Exception e) {
e.printStackTrace();
}
Thermometre.this.repaint();
}
};
int delay = 30000; //30000 millisecondes = 1/2 minute
timer = new Timer(delay, actionListener);
timer.start();
setLayout(new FlowLayout());
add(lblDate);
add(lblTime);
add(lblTemp);
setSize(300, 80);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
String[] getValues() throws Exception
{
rd = new RandomAccessFile("c:\\file.txt", "r");
//on se positionne à la dernière ligne
rd.seek(rd.length()-26);
String vals[] = rd.readLine().split(";");
rd.close();
return vals;
}
public static void main(String[] args)
{
new Thermometre();
}
} |
Partager