package picture;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JCheckDemo {
JCheckBox CB1,CB2,CB4,CB8;
StringBuffer choices;
JLabel display;
JPanel checkPanel;
public JCheckDemo(){
CB1=new JCheckBox("1");
CB1.setMnemonic(KeyEvent.VK_4);
CB1.setSelected(true);
CB2=new JCheckBox("2");
CB2.setMnemonic(KeyEvent.VK_3);
CB2.setSelected(true);
CB4=new JCheckBox("4");
CB4.setMnemonic(KeyEvent.VK_2);
CB4.setSelected(true);
CB8=new JCheckBox("8");
CB8.setMnemonic(KeyEvent.VK_1);
CB8.setSelected(true);
CheckBoxListener myListener=new CheckBoxListener();
CB1.addItemListener(myListener);
CB2.addItemListener(myListener);
CB4.addItemListener(myListener);
CB8.addItemListener(myListener);
choices=new StringBuffer("8421");
display=new JLabel(choices.toString());
checkPanel=new JPanel();
checkPanel.setLayout(new GridLayout(0,1));
checkPanel.add(CB1);
checkPanel.add(CB2);
checkPanel.add(CB4);
checkPanel.add(CB8);
}
class CheckBoxListener implements ItemListener
{
public void itenStateChanged(ItemEvent e){
int index=0;
char c='-';
JCheckBox src=(JCheckBox)e.getItemSelectable();
if(src==CB8){index=0;c='8';}
if(src==CB4){index=1;c='4';}
if(src==CB2){index=2;c='2';}
if(src==CB1){index=3;c='1';}
if(e.getStateChange()==ItemEvent.DESELECTED)
c='-';
choices.setCharAt(index ,c);
display.setText(choices.toString());
}
public void itemStateChanged(ItemEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}//这段代码是 编译器自动生成的,说什么抽象类,本来书上的程序没有这段
}
public static void main(String[]args){
JCheckDemo JCD=new JCheckDemo();
JFrame Frame=new JFrame("JCheckBox使用实例");
Frame.getContentPane().setLayout(new FlowLayout());
Frame.getContentPane().add(JCD.checkPanel);
Frame.getContentPane().add(JCD.display);
Frame.pack();
Frame.setVisible(true);
}
}
功能简介:
开始窗口显示“8421”按下一个 复选按钮,相应的数字变为“-”
这是书上的介绍,但我运行,功能不显示。
本人刚学JAVA菜鸟,用netbeans编译器
不胜感谢
------解决方案--------------------
你原来的拼写错了,itenStateChanged 改成 itemStateChanged然后把那个自动生成的代码注解掉
- Java code
import javax.swing.*;import java.awt.*;import java.awt.event.*;public class JCheckDemo { JCheckBox CB1,CB2,CB4,CB8; StringBuffer choices; JLabel display; JPanel checkPanel; public JCheckDemo(){ CB1=new JCheckBox("1"); CB1.setMnemonic(KeyEvent.VK_4); CB1.setSelected(true); CB2=new JCheckBox("2"); CB2.setMnemonic(KeyEvent.VK_3); CB2.setSelected(true); CB4=new JCheckBox("4"); CB4.setMnemonic(KeyEvent.VK_2); CB4.setSelected(true); CB8=new JCheckBox("8"); CB8.setMnemonic(KeyEvent.VK_1); CB8.setSelected(true); CheckBoxListener myListener=new CheckBoxListener(); CB1.addItemListener(myListener); CB2.addItemListener(myListener); CB4.addItemListener(myListener); CB8.addItemListener(myListener); choices=new StringBuffer("8421"); display=new JLabel(choices.toString()); checkPanel=new JPanel(); checkPanel.setLayout(new GridLayout(0,1)); checkPanel.add(CB1); checkPanel.add(CB2); checkPanel.add(CB4); checkPanel.add(CB8); } class CheckBoxListener implements ItemListener { public void itemStateChanged(ItemEvent e){ //你原來的拼寫有問題 int index=0; char c='-'; JCheckBox src=(JCheckBox)e.getItemSelectable(); if(src==CB8){index=0;c='8';} if(src==CB4){index=1;c='4';} if(src==CB2){index=2;c='2';} if(src==CB1){index=3;c='1';} if(e.getStateChange()==ItemEvent.DESELECTED) c='-'; choices.setCharAt(index ,c); display.setText(choices.toString()); }// public void itemStateChanged(ItemEvent e) {// throw new UnsupportedOperationException("Not supported yet.");// }//这段代码是 编译器自动生成的,说什么抽象类,本来书上的程序没有这段 } public static void main(String[]args){ JCheckDemo JCD=new JCheckDemo(); JFrame frame=new JFrame("JCheckBox使用实例"); frame.getContentPane().setLayout(new FlowLayout()); frame.getContentPane().add(JCD.checkPanel); frame.getContentPane().add(JCD.display); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}