ButtonHandler handler = new ButtonHandler();这句话为什么出错呀?

解决方案 »

  1.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class JButtonTest extends JFrame{
    private JButton Button1,Button2;
    private JTextField field;
    private JCheckBox bold,italic;
    public JButtonTest(){
    super("Testing Buttons:");
    Container container = getContentPane();
    container.setLayout(new FlowLayout());
    Button1 = new JButton("Button1");
    container.add(Button1);
    Button2 = new JButton("Button2");
    container.add(Button2);
    ButtonHandler handler = new ButtonHandler();
    Button1.addActionListener(handler);
    Button2.addActionListener( handler);
    field = new JTextField("Watch the font style change",20);
    field.setFont( new Font("Serif",Font.PLAIN ,14));
    container.add(field);
    bold = new JCheckBox("Bold");
    container.add(bold);
    italic = new JCheckBox("Italic");
    container.add(italic);
    CheckBoxHandler handler1 = new CheckBoxHandler();
    bold.addItemListener( handler1);
    italic.addItemListener( handler1);
    setSize(275,180);
    setVisible(true);
    // CheckBoxHandler handler ;
    }
    public static void main(String args[])
    {
    JButtonTest application = new JButtonTest();
    application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
    }
    private class ButtonHandler implements ActionListener{
    public void actionPerformed(ActionEvent event){
    JOptionPane.showMessageDialog(null,"You pressed:" + event.getActionCommand());
    }
    }
    private class CheckBoxHandler implements ItemListener{
    private int valBold = Font.PLAIN ;
    private int valItalic = Font.PLAIN ;
    public void itemStateChange(ItemEvent event){
    if(event.getSource() == bold)
    if(event.getStateChange()== ItemEvent.SELECTED)
    valBold = Font.BOLD ;
    else
    valBold = Font.PLAIN ;
    if(event.getSource() == italic)
    if(event.getStateChange()==ItemEvent.SELECTED )
    valItalic= Font.ITALIC ;
    else
    valItalic = Font.PLAIN ;
    else
    valItalic = Font.PLAIN ;
    field.setFont( new Font("serif",valBold + valItalic,14));
    }
    }
    }CheckBoxHandler这一行有错误.不知道为什么.麻烦大家帮我看一下,我是新手