我在“Mycheckbox”类扩展时遇到错误Frame实现了ItemListener:
Mycheckbox类型必须实现继承的抽象方法 ItemListener.itemStateChanged(一个ItemEvent)
我正在使用AWT在我的程序中编写GUI:
import java.io.*;
import java.awt.Checkbox;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.*;
class Mycheckbox extends Frame implements ItemListener {
String msg="";
Checkbox c1,c2,c3;
Mycheckbox(){
setLayout(new FlowLayout());
c1=new Checkbox("Bold",true);
c2=new Checkbox("Italic");
c3=new Checkbox("Underline");
add(c1);
add(c2);
add(c3);
c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
}
public void ItemStateChanged(ItemEvent ie){
repaint();
}
public void paint(Graphics g){
g.drawString("Current state",10,100);
msg="Bold:" +c1.getState();
g.drawString(msg,10,100);
msg="Italic:" +c2.getState();
g.drawString(msg,10,100);
msg="Underline:" +c3.getState();
g.drawString(msg,10,160);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Mycheckbox mc=new Mycheckbox();
mc.setSize(400,400);
mc.setTitle("check box");
mc.setVisible(true);
}
}