我一直在关注 Dietel 的 Java 如何编程,即使我逐字复制程序并尝试导入(Ctrl-Shift-O),它仍然无法将 ButtonHandler 识别为类型。这是我的代码:
import java.awt.FlowLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
public class ButtonTest1 extends JFrame{
private JButton plainB;
private JButton fancyB;
public ButtonTest1(){ //Constructer
super("Testing buttons");
setLayout(new FlowLayout());
plainB = new JButton("Plain Button");
add(plainB); //Add to frame
Icon bug1 = new ImageIcon(getClass().getResource("bug1.png"));
Icon bug2 = new ImageIcon(getClass().getResource("bug1.png"));
fancyB = new JButton ("Fancy Button", bug1); //set image
fancyB.setRolloverIcon(bug1); //set rollover image
add(fancyB);
ButtonHandler handler = new ButtonHandler(); // <--- errors are here
fancyB.addActionListener(handler);
}
}
我认为你有人实施了
ButtonHandler
课程。它不是来自 Swing 库
使用
ActionListener
代替或创建自定义:
public class ButtonHandler implements ActionListener{
...
}
ButtonHandler 不是标准的 swing 类。您需要获取其源代码,或者如果您知道它的功能,则可以自行编写。 你知道这个来自哪个图书馆吗?