按对勾按钮时出现运行时错误。我试图在按下对勾按钮以触发事件时使用javax.swing.Timer。因此,如果有人有任何建议,我将不胜感激。我当前正在使用Eclipse,并且按下对勾按钮时出现运行时错误。
线程“ AWT-EventQueue-0”中的异常java.lang.Error:尚未解决
编译问题:ClockPanel类型必须实现继承的
抽象方法ActionListener.actionPerformed(ActionEvent)在
ClockPanel.actionPerformed(ClockPanel.java:10)
这是我正在使用的代码(3个类)。
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();
}
}
最佳答案
根据我的评论,您的问题是您必须给ClockPanel类一个actionPerformed方法,该方法在此类的范围内。您甚至不应该尝试运行无法编译的类,而应该在尝试运行代码之前首先修复所有编译错误。