Reputation: 272
I have a runtime error when I press the tick button. I am trying to use javax.swing.Timer when the tick button is pressed to fire an event So if anyone have any suggestions I would appreciate them. I am currently using Eclipse and there is a runtime error when the tick button is pressed.
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: The type ClockPanel must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent) at ClockPanel.actionPerformed(ClockPanel.java:10)
Here is the code I am using (3 classes).
import java.util.Calendar;
import java.util.GregorianCalendar;
class Clock
{
int hour;
int minute;
int second;
Calendar gregcal = new GregorianCalendar();
int h = gregcal.get(Calendar.HOUR_OF_DAY);
int m = gregcal.get(Calendar.MINUTE);
int s = gregcal.get(Calendar.SECOND);
//default constructor
Clock()
{
hour = h;
minute = m;
second = s;
}
//parameterized constructor
Clock(int hour, int minute, int second)
{
this.hour = hour;
this.minute = minute;
this.second = second;
}
public int getHour()
{
return hour;
}
public int getMinute()
{
return minute;
}
public int getSecond()
{
return second;
}
public String toString()
{
return "The time is: " + hour + ":" + minute + ":" + second ;
}
public void tick()
{
second += 1;
if(second > 60)
{
second = 0;
minute++;
}
if(minute > 60)
{
minute =0;
hour++;
}
if(hour > 24)
{
hour = 0;
}
System.out.println(this.toString());
}
}
// next class
import java.awt.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.*;
public class TestClock
{
public static void main(String[] args)
{
Calendar gregcal = new GregorianCalendar();
int h = gregcal.get(Calendar.HOUR_OF_DAY);
int m = gregcal.get(Calendar.MINUTE);
int s = gregcal.get(Calendar.SECOND);
Clock c = new Clock(h, m, s);
JFrame application = new JFrame("Clock");
ClockPanel panel = new ClockPanel(c);
application.add(panel);
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.setSize(160,80);
application.setVisible(true);
}
}
// the last class here is the problem
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
class ClockPanel extends JPanel implements ActionListener
{
Clock myck = new Clock();
private javax.swing.Timer timer;
JTextField txt1, txt2, txt3;
JLabel jl1, jl2;
JButton tick;
JPanel top, buttom;
ClockPanel(Clock ck)
{
this.setLayout(new GridLayout(2,1));
top = new JPanel();
top.setLayout(new GridLayout(1,5));
String hour = "" + ck.getHour(); // easier way
txt1 = new JTextField(hour);
top.add(txt1);
jl1 = new JLabel(" : ");
top.add(jl1);
String minute = Integer.toString(ck.getMinute()); // harder convertion
txt2 = new JTextField(minute);
top.add(txt2);
jl2 = new JLabel(" : ");
top.add(jl2);
String second = Integer.toString(ck.getSecond());
txt3 = new JTextField(second);
top.add(txt3);
this.add(top);
buttom = new JPanel(new GridLayout(1,1));
tick = new JButton("tick");
buttom.add(tick);
tick.addActionListener(this);
this.add(buttom);
}
javax.swing.Timer t = new javax.swing.Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource().equals(tick))
{
myck.tick();
txt1.setText(Integer.toString(myck.getHour()));
txt2.setText(Integer.toString(myck.getMinute()));
txt3.setText(Integer.toString(myck.getSecond()));
}
}
});
public void start()
{
t.start();
}
}
Upvotes: 0
Views: 383
Reputation: 285403
As per my comments, your problem is that you must give your ClockPanel class an actionPerformed method that is in the scope of this class. You shouldn't even be trying to run a class that doesn't compile, but rather should fix all compilation errors first before trying to run the code.
Upvotes: 5