对于使用ItemListener和ItemEvent的行,我收到此错误。我把**放在我得到它的地方。
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);
}
}
}
你能帮我理解为什么我会收到这个错误吗?领导该教程的人不会遇到这些错误。
编辑:
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);
}
}
}
答案 0 :(得分:1)
您必须导入ItemEvent类。
import java.awt.event.ItemEvent
答案 1 :(得分:1)
您应该将ItemListener
更改为ActionListener
,将ItemEvent
更改为ActionEvent
。
它应该是这样的:
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);
}
}
}
答案 2 :(得分:0)
您的代码有2个错误。第一个是您应该导入java.awt.event.ItemListener & ItemEvent
而不是ActionListener/ActionEvent
。因此,在您的HandlerClass
声明中,您应该已实现ItemListener
,并且事件对象的类型必须为ItemEvent
(在您的itemStateChanged
方法中)
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);
}
}
}
这段代码是TheNewBoston在他的第65篇Java编程教程中写的。