2013-04-11, 14:21
#1
Tjena! Jag har ett problem med min kod som jag ej kan lsa. Jag vill att programmet ska visa "minuter : sekunder : hundradelar" men jag fr den aldrig att visa just det. Tacksam fr all hjlp !
**Koden:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TimerAdam extends JFrame implements ActionListener {
// Variabler
JPanel panelUp, panelDown;
JLabel labelTime;
JButton buttonPlay, buttonPaus, buttonStop;
Timer timerTime;
int time = 0;
int min;
int sec;
int hund;
// Konstruktr
public TimerAdam(){
setSize(300,200);
setLocation(300,300);
setTitle("Timer");
panelUp = new JPanel(); // Skapar element
panelDown = new JPanel();
labelTime = new JLabel(min + " : " + sec + " : " + hund);
buttonPlay = new JButton("Play");
buttonPaus = new JButton("Paus");
buttonStop = new JButton("Stop");
timerTime = new Timer(1000, this);
setLayout(new GridLayout(2,1));
panelUp.setLayout(new GridLayout(1,1));
panelDown.setLayout(new GridLayout(1,3));
panelUp.setBackground(Color.cyan);
labelTime.setHorizontalAlignment(SwingConstants.CE NTER);
labelTime.setFont(new Font("Arial", Font.BOLD, 25));
buttonPlay.addActionListener(this);
buttonPaus.addActionListener(this);
buttonStop.addActionListener(this);
panelUp.add(labelTime);
panelDown.add(buttonPlay);
panelDown.add(buttonPaus);
panelDown.add(buttonStop);
this.add(panelUp);
this.add(panelDown);
setDefaultCloseOperation(TimerAdam.EXIT_ON_CLOSE);
setVisible(true);
}
// Metoder
public static void main(String[] args){
new TimerAdam();
}
@Override
public void actionPerformed(ActionEvent e) {
int sec = time;
int min = time%60;
int hund = time%100;
if(time == 60){
min = time%60;
sec = 0;
time = 0;
}
if(e.getSource() == buttonPlay){
timerTime.start();
labelTime.setText((Integer.toString(min) + " : " + Integer.toString(sec) + " : " + Integer.toString(hund)));
}
if(e.getSource() == buttonPaus){
timerTime.stop();
}
if(e.getSource() == buttonStop){
timerTime.stop();
time = 0;
labelTime.setText(Integer.toString(time));
}
if(e.getSource() == timerTime){
labelTime.setText((Integer.toString(min) + " : " + Integer.toString(sec) + " : " + Integer.toString(hund)));
time++;
}
}
}
**Koden:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TimerAdam extends JFrame implements ActionListener {
// Variabler
JPanel panelUp, panelDown;
JLabel labelTime;
JButton buttonPlay, buttonPaus, buttonStop;
Timer timerTime;
int time = 0;
int min;
int sec;
int hund;
// Konstruktr
public TimerAdam(){
setSize(300,200);
setLocation(300,300);
setTitle("Timer");
panelUp = new JPanel(); // Skapar element
panelDown = new JPanel();
labelTime = new JLabel(min + " : " + sec + " : " + hund);
buttonPlay = new JButton("Play");
buttonPaus = new JButton("Paus");
buttonStop = new JButton("Stop");
timerTime = new Timer(1000, this);
setLayout(new GridLayout(2,1));
panelUp.setLayout(new GridLayout(1,1));
panelDown.setLayout(new GridLayout(1,3));
panelUp.setBackground(Color.cyan);
labelTime.setHorizontalAlignment(SwingConstants.CE NTER);
labelTime.setFont(new Font("Arial", Font.BOLD, 25));
buttonPlay.addActionListener(this);
buttonPaus.addActionListener(this);
buttonStop.addActionListener(this);
panelUp.add(labelTime);
panelDown.add(buttonPlay);
panelDown.add(buttonPaus);
panelDown.add(buttonStop);
this.add(panelUp);
this.add(panelDown);
setDefaultCloseOperation(TimerAdam.EXIT_ON_CLOSE);
setVisible(true);
}
// Metoder
public static void main(String[] args){
new TimerAdam();
}
@Override
public void actionPerformed(ActionEvent e) {
int sec = time;
int min = time%60;
int hund = time%100;
if(time == 60){
min = time%60;
sec = 0;
time = 0;
}
if(e.getSource() == buttonPlay){
timerTime.start();
labelTime.setText((Integer.toString(min) + " : " + Integer.toString(sec) + " : " + Integer.toString(hund)));
}
if(e.getSource() == buttonPaus){
timerTime.stop();
}
if(e.getSource() == buttonStop){
timerTime.stop();
time = 0;
labelTime.setText(Integer.toString(time));
}
if(e.getSource() == timerTime){
labelTime.setText((Integer.toString(min) + " : " + Integer.toString(sec) + " : " + Integer.toString(hund)));
time++;
}
}
}