按下勾选按钮时出现运行错误。我正在尝试使用javax.swing.Timer按下勾选按钮来触发事件所以如果有人有任何建议我会很感激他们。我目前正在使用Eclipse,按下勾选按钮时会出现运行时错误。
线程“AWT-EventQueue-0”中的异常java.lang.Error:未解析 编译问题:类型ClockPanel必须实现继承 抽象方法ActionListener.actionPerformed(ActionEvent)at 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();
}
}
答案 0 :(得分:5)
根据我的评论,您的问题是您必须为ClockPanel类提供一个actionPerformed方法,该方法属于此类的范围。您甚至不应该尝试运行不编译的类,而应该在尝试运行代码之前先修复所有编译错误。