in tema di copiature
codice:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* Creo il cronometro
*
*/
public class Cronometro {
private JFrame frame;
private JLabel labelTime;
private JPanel panelButtons;
private JButton buttonStart;
private JButton buttonStop;
private Timer timer;
private long startTime;
/**
* Avvia il cronometro
*/
public void avviaCronometro(){
frame = new JFrame("Cronometro");
frame.setBounds(600, 270, 300, 140);
// frame.setResizable(false);
labelTime = new JLabel("0:00:00.000");
labelTime.setFont (new Font("SansSerif", Font.BOLD, 50));
labelTime.setHorizontalAlignment(JLabel.CENTER);
buttonStart = new JButton("Start");
buttonStop = new JButton("Stop");
buttonStop.setEnabled(false);
panelButtons = new JPanel(new GridLayout(1, 2));
panelButtons.add(buttonStart);
panelButtons.add(buttonStop);
frame.add(labelTime, BorderLayout.CENTER);
frame.add(panelButtons, BorderLayout.SOUTH);
timer = new Timer(100, new ActionListener() {
public void actionPerformed(ActionEvent e) {
long diffTime = System.currentTimeMillis() - startTime;
int milliseconds = (int)(diffTime % 1000);
int seconds = (int)(diffTime / 1000 % 60);
int minutes = (int)(diffTime / 60000 % 60);
int hours = (int)(diffTime / 3600000);
String s = String.format("%d:%02d:%02d.%03d", hours, minutes, seconds, milliseconds);
labelTime.setText(s);
}
});
buttonStart.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e) {
startTime = System.currentTimeMillis();
timer.start();
buttonStart.setEnabled(false);
buttonStop.setEnabled(true);
}
});
buttonStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
timer.stop();
buttonStart.setEnabled(true);
buttonStop.setEnabled(false);
}
});
frame.setVisible (true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main (String[] args) {
SwingUtilities.invokeLater(new Runnable () {
public void run() {
new Cronometro().avviaCronometro();
}
});
}
}
dovresti davvero acoltare i consigli di Lele però.