所以我有一段看起来像这样的代码......
public IPGUI() {
setTitle("IP Extractor");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 250, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JButton btnConvertDocuments = new JButton("1. Convert Documents");
btnConvertDocuments.setAlignmentX(Component.CENTER_ALIGNMENT);
btnConvertDocuments.setMaximumSize(new Dimension(160, 0));
btnConvertDocuments.setPreferredSize(new Dimension(0, 50));
panel.add(btnConvertDocuments);
btnConvertDocuments.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//FileConverter fc = new FileConverter();
//Why wont the above method work?
}
});
JSeparator separator_3 = new JSeparator();
panel.add(separator_3);
当我点击按钮时,这就是产生的错误:
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems:
FileConverter cannot be resolved to a type
FileConverter cannot be resolved to a type
at IPGUI$2.actionPerformed(IPGUI.java:60)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
所以我实现了监听器和动作执行事件,但我不明白为什么我不能在另一个类中调用该方法?有人能解释我哪里错了吗?提前感谢您的任何意见。
答案 0 :(得分:2)
看起来你正在使用Java Swing。不要忘记在类签名的末尾添加“implements ActionListener”。
试试这个:
public class IPGUI extends JFrame implements ActionListener {
...
public IPGUI() {
setTitle("IP Extractor");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 250, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JButton btnConvertDocuments = new JButton("1. Convert Documents");
btnConvertDocuments.setAlignmentX(Component.CENTER_ALIGNMENT);
btnConvertDocuments.setMaximumSize(new Dimension(160, 0));
btnConvertDocuments.setPreferredSize(new Dimension(0, 50));
panel.add(btnConvertDocuments);
//btnConvertDocuments.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent e) {
// //FileConverter fc = new FileConverter();
// //Why wont the above method work?
// }
//});
btnConvertDocuments.setActionCommand("x");
btnConvertDocuments.addActionListener(this);
JSeparator separator_3 = new JSeparator();
panel.add(separator_3);
}
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command.equals("x")) {
//FileConverter fc = new FileConverter();
}
}
...
}
答案 1 :(得分:1)
您是否在开始时导入了其他课程?
import FileConverter;
答案 2 :(得分:1)
检查文件的开头是否有类似
的内容 import bla.bla.FileConverter;
如果不是,则需要添加它并确保FileConverter类实现是在类传递上。将jar与它一起添加到项目的类路径中,使用什么IDE?
答案 3 :(得分:0)
到底是什么? Eclipse让我感到困惑。我试图在这里遵循每个人的建议,但它没有奏效。但是当我重新启动eclipse时......一切正常。
奇怪:/
感谢大家的投入,我真的很感激!