javax.swing.Timer 的困难
当我按下勾选按钮时出现运行时错误。我正在尝试在按下勾选按钮时使用 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();
}
}
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据我的评论,您的问题是您必须为 ClockPanel 类提供一个位于此类范围内的 actionPerformed 方法。您甚至不应该尝试运行无法编译的类,而应该在尝试运行代码之前先修复所有编译错误。
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.