指点一下
下面代码 为什么我运行后点击Button键后,不能实现所需要的结果,而是出现错误?import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class demo1 implements ItemListener{
JLabel jlabselected;
JLabel jlabchange;
JCheckBox jcbAlpha;
JCheckBox jcbBeta;
JCheckBox jcbGamma;
demo1(){
JFrame jfrm=new JFrame("Java checkBox demo1");
jfrm.setLayout(new FlowLayout());
jfrm.setSize(280, 120);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jlabselected=new JLabel("");
jlabchange=new JLabel("");
jcbAlpha=new JCheckBox("Alpha");
jcbBeta=new JCheckBox("Beta");
jcbGamma=new JCheckBox("Gamma");
jcbAlpha.addItemListener(this);
jcbBeta.addItemListener(this);
jcbGamma.addItemListener(this);
jfrm.add(jcbAlpha);
jfrm.add(jcbBeta);
jfrm.add(jcbGamma);
jfrm.add(jlabchange);
jfrm.add(jlabselected);
jfrm.setVisible(true);
}
public void itemStateChange(ItemEvent ie){
String str="";
JCheckBox cb=(JCheckBox)ie.getItem();
if(cb.isSelected())
jlabchange.setText(cb.getText());
else
jlabchange.setText(cb.getText()+" was just cleared.");
if(jcbAlpha.isSelected())
str+="Alpha ";
if(jcbBeta.isSelected())
str+="Beta ";
if(jcbGamma.isSelected())
str+="Gamma ";
jlabselected.setText("select checkbox: "+str);
}
public static void main(String[] args){
new demo1();
}
}
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
The type demo1 must implement the inherited abstract method ItemListener.itemStateChanged(ItemEvent)
at demo1.itemStateChanged(demo1.java:5)
at javax.swing.AbstractButton.fireItemStateChanged(Unknown Source)
at javax.swing.AbstractButton$Handler.itemStateChanged(Unknown Source)
at javax.swing.DefaultButtonModel.fireItemStateChanged(Unknown Source)
at javax.swing.JToggleButton$ToggleButtonModel.setSelected(Unknown Source)
at javax.swing.JToggleButton$ToggleButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
----------------解决方案--------------------------------------------------------
程序代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class demo1 implements ItemListener
{
JLabel jlabselected;
JLabel jlabchange;
JCheckBox jcbAlpha;
JCheckBox jcbBeta;
JCheckBox jcbGamma;
demo1()
{
JFrame jfrm=new JFrame("Java checkBox demo1");//
jfrm.setLayout(new FlowLayout());//流式页面设计
jfrm.setSize(280, 120);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jlabselected=new JLabel("");
jlabchange=new JLabel("");
jcbAlpha=new JCheckBox("Alpha");
jcbBeta=new JCheckBox("Beta");
jcbGamma=new JCheckBox("Gamma");
jcbAlpha.addItemListener(this);
jcbBeta.addItemListener(this);
jcbGamma.addItemListener(this);
jfrm.add(jcbAlpha);
jfrm.add(jcbBeta);
jfrm.add(jcbGamma);
jfrm.add(jlabchange);
jfrm.add(jlabselected);
jfrm.setVisible(true);
}
public void itemStateChanged(ItemEvent ie)///////////////////////////////////////////////////////////////////
{
String str="";
JCheckBox cb=(JCheckBox)ie.getItem();
if(cb.isSelected())
jlabchange.setText(cb.getText());
else
jlabchange.setText(cb.getText()+" was just cleared.");
if(jcbAlpha.isSelected())
str+="Alpha ";
if(jcbBeta.isSelected())
str+="Beta ";
if(jcbGamma.isSelected())
str+="Gamma ";
jlabselected.setText("select checkbox: "+str);
}
public static void main(String[] args)
{
new demo1();
}
}
你错的地方确实不应该错!!!public void itemStateChanged(ItemEvent ie)你自己看看是不是不应该错?? import java.awt.event.*;
import javax.swing.*;
class demo1 implements ItemListener
{
JLabel jlabselected;
JLabel jlabchange;
JCheckBox jcbAlpha;
JCheckBox jcbBeta;
JCheckBox jcbGamma;
demo1()
{
JFrame jfrm=new JFrame("Java checkBox demo1");//
jfrm.setLayout(new FlowLayout());//流式页面设计
jfrm.setSize(280, 120);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jlabselected=new JLabel("");
jlabchange=new JLabel("");
jcbAlpha=new JCheckBox("Alpha");
jcbBeta=new JCheckBox("Beta");
jcbGamma=new JCheckBox("Gamma");
jcbAlpha.addItemListener(this);
jcbBeta.addItemListener(this);
jcbGamma.addItemListener(this);
jfrm.add(jcbAlpha);
jfrm.add(jcbBeta);
jfrm.add(jcbGamma);
jfrm.add(jlabchange);
jfrm.add(jlabselected);
jfrm.setVisible(true);
}
public void itemStateChanged(ItemEvent ie)///////////////////////////////////////////////////////////////////
{
String str="";
JCheckBox cb=(JCheckBox)ie.getItem();
if(cb.isSelected())
jlabchange.setText(cb.getText());
else
jlabchange.setText(cb.getText()+" was just cleared.");
if(jcbAlpha.isSelected())
str+="Alpha ";
if(jcbBeta.isSelected())
str+="Beta ";
if(jcbGamma.isSelected())
str+="Gamma ";
jlabselected.setText("select checkbox: "+str);
}
public static void main(String[] args)
{
new demo1();
}
}
----------------解决方案--------------------------------------------------------
函数错误了?
----------------解决方案--------------------------------------------------------