Yritän edelleen saada aikaseksi kelloa joka pyörisi kokoajan. Tämmösen olen tehnyt, mutta se ei toimi. Voisiko joku korjata tuon. KIITÄN SUURESTI!!!
//Tästä alkaa
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Calendar;
import java.util.*;
import javax.swing.Timer;
public class Timeri extends JFrame{
public Timeri() {
super("Timer testi");
JButton J = new JButton("käynnistä");
J.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Timer t = new Timer (1000, new
ActionListener (){
public void actionPerformed (ActionEvent event)
{
String currentTime = getTime();
JLabel time = new JLabel("Kello: ");
JLabel current = new JLabel(currentTime);
add(time);
add(current);
}
String getTime() {
String Kello;
Calendar now = Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int month = now.get(Calendar.MONTH) 1;
int day = now.get(Calendar.DAY_OF_MONTH);
int year = now.get(Calendar.YEAR);
int second = now.get(Calendar.SECOND);
String monthName = "";
switch (month) {
case (1):
monthName = "Tammikuu";
break;
case (2):
monthName = "Helmikuu";
break;
case (3):
monthName = "Maaliskuu";
break;
case (4):
monthName = "Huhtikuu";
break;
case (5):
monthName = "Toukokuu";
break;
case (6):
monthName = "Kesäkuu";
break;
case (7):
monthName = "Heinäkuu";
break;
case (8):
monthName = "Huhtikuu";
break;
case (9):
monthName = "Syyskyy";
break;
case (10):
monthName = "Lokakuu";
break;
case (11):
monthName = "Marraskuu";
break;
case (12):
monthName = "Joulukuu";
}
JPanel paneli = new JPanel();
JLabel teksti = new JLabel( " " day ", " year " "
hour ":" minute ":" second);
paneli.add(teksti);
t.start();
}
});
}
// asetuksia ikkunaan
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(700,700);
this.setVisible(true);
}
public static void main(String[] args) {
new Timeri();
}
}
//Tästä alkaa
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Calendar;
import java.util.*;
import javax.swing.Timer;
public class Timeri extends JFrame{
public Timeri() {
super("Timer testi");
JButton J = new JButton("käynnistä");
J.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Timer t = new Timer (1000, new
ActionListener (){
public void actionPerformed (ActionEvent event)
{
String currentTime = getTime();
JLabel time = new JLabel("Kello: ");
JLabel current = new JLabel(currentTime);
add(time);
add(current);
}
String getTime() {
String Kello;
Calendar now = Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int month = now.get(Calendar.MONTH) 1;
int day = now.get(Calendar.DAY_OF_MONTH);
int year = now.get(Calendar.YEAR);
int second = now.get(Calendar.SECOND);
String monthName = "";
switch (month) {
case (1):
monthName = "Tammikuu";
break;
case (2):
monthName = "Helmikuu";
break;
case (3):
monthName = "Maaliskuu";
break;
case (4):
monthName = "Huhtikuu";
break;
case (5):
monthName = "Toukokuu";
break;
case (6):
monthName = "Kesäkuu";
break;
case (7):
monthName = "Heinäkuu";
break;
case (8):
monthName = "Huhtikuu";
break;
case (9):
monthName = "Syyskyy";
break;
case (10):
monthName = "Lokakuu";
break;
case (11):
monthName = "Marraskuu";
break;
case (12):
monthName = "Joulukuu";
}
JPanel paneli = new JPanel();
JLabel teksti = new JLabel( " " day ", " year " "
hour ":" minute ":" second);
paneli.add(teksti);
t.start();
}
});
}
// asetuksia ikkunaan
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(700,700);
this.setVisible(true);
}
public static void main(String[] args) {
new Timeri();
}
}
Vastaukset 1
Vanhimmat
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class Timeri extends JFrame implements ActionListener {
private Timer timerSekuntti;
private JLabel labelAika;
public Timeri() {
super("Timer testi");
labelAika = new JLabel();
labelAika.setHorizontalAlignment(JLabel.CENTER);
getContentPane().add(labelAika);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(700, 700);
setVisible(true);
timerSekuntti = new Timer(1000, this);
timerSekuntti.start();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(timerSekuntti)) {
Date date = Calendar.getInstance().getTime();
labelAika.setText(date.toString());
}
}
public static void main(String[] args) {
new Timeri();
}
}