简体   繁体   中英

Cannot be resolved to a type?

I'm getting this error for lines with ItemListener and ItemEvent. I put ** where I'm getting it.

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class gui extends JFrame {

private JTextField tf;
private JCheckBox boldbox;
private JCheckBox italicbox;

public gui() {
    super("The title");
    setLayout(new FlowLayout());

    tf = new JTextField("This is a sentence", 20);
    tf.setFont(new Font("Serif", Font.PLAIN, 14));
    add(tf);

    boldbox = new JCheckBox("bold");
    italicbox = new JCheckBox("italic");
    add(boldbox);
    add(italicbox);

    HandlerClass handler = new HandlerClass();
    boldbox.addItemListener(handler);
    italicbox.addItemListener(handler);
}

private class HandlerClass implements *ActionListener* {
    public void itemStateChanged(*ActionEvent* event) {
        Font font = null;

        if (boldbox.isSelected()&& italicbox.isSelected()) 
            font = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
        else if(boldbox.isSelected()) 
                font = new Font("Serif", Font.BOLD, 14);
        else if(italicbox.isSelected())
                    font = new Font("Serif", Font.ITALIC, 14);
        else font = new Font("Serif", Font.PLAIN, 14);

                        tf.setFont(font);
      }
   }            
}

Would you help me understand why I'm getting that error ? Person who leads that tutorial don't get these errors.

EDIT:

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTextField;




public class gui extends JFrame {

private JTextField tf;
private JCheckBox boldbox;
private JCheckBox italicbox;

public gui() {
    super("The title");
    setLayout(new FlowLayout());

    tf = new JTextField("This is a sentence", 20);
    tf.setFont(new Font("Serif", Font.PLAIN, 14));
    add(tf);

    boldbox = new JCheckBox("bold");
    italicbox = new JCheckBox("italic");
    add(boldbox);
    add(italicbox);

    HandlerClass handler = new HandlerClass();
    boldbox.addActionListener(handler);
    italicbox.addActionListener(handler);
}

private class HandlerClass implements ActionListener {
    @Override
    public void ActionPerformed(ActionEvent event) {
        Font font = null;

        if (boldbox.isSelected()&& italicbox.isSelected()) 
            font = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
        else if(boldbox.isSelected()) 
                font = new Font("Serif", Font.BOLD, 14);
        else if(italicbox.isSelected())
                    font = new Font("Serif", Font.ITALIC, 14);
        else font = new Font("Serif", Font.PLAIN, 14);

                        tf.setFont(font);
    }
}           
}

You have to import ItemEvent class.

import java.awt.event.ItemEvent

You should change ItemListener to ActionListener and ItemEvent to ActionEvent .

It should look like this:

private class HandlerClass implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent event) {
        Font font = null;
        if (boldbox.isSelected() && italicbox.isSelected()) 
            font = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
        else if(boldbox.isSelected()) 
            font = new Font("Serif", Font.BOLD, 14);
        else if(italicbox.isSelected())
            font = new Font("Serif", Font.ITALIC, 14);
        else font = new Font("Serif", Font.PLAIN, 14);
            tf.setFont(font);
      }
   }            
}

There were 2 Mistakes at your code. 1st is that you should've imported java.awt.event.ItemListener & ItemEvent instead of ActionListener/ActionEvent . Therefore in your HandlerClass declaration you should've implemented ItemListener and your event object must be of type ItemEvent (at your itemStateChanged method)

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class gui extends JFrame {

private JTextField tf;
private JCheckBox boldbox;
private JCheckBox italicbox;

public gui() {
    super("The title");
    setLayout(new FlowLayout());

    tf = new JTextField("This is a sentence", 20);
    tf.setFont(new Font("Serif", Font.PLAIN, 14));
    add(tf);

    boldbox = new JCheckBox("bold");
    italicbox = new JCheckBox("italic");
    add(boldbox);
    add(italicbox);

    HandlerClass handler = new HandlerClass();
    boldbox.addItemListener(handler);
    italicbox.addItemListener(handler);
}

private class HandlerClass implements ItemListener {
    public void itemStateChanged(ItemEvent event) {
        Font font = null;

        if (boldbox.isSelected()&& italicbox.isSelected()) 
            font = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
        else if(boldbox.isSelected()) 
                font = new Font("Serif", Font.BOLD, 14);
        else if(italicbox.isSelected())
                    font = new Font("Serif", Font.ITALIC, 14);
        else font = new Font("Serif", Font.PLAIN, 14);

                        tf.setFont(font);
      }
   }            
}

This code is what TheNewBoston wrote in his 65th tutorial of Java programming.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM