How to call external method in Java Button (GUI)
so I have a section of code that looks like this...
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);
When I click on the button, this is the error that is produced:
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)
So I implemented the listener and the action performed event, but I don't understand why I can't make the call to that method in another class? Can someone explain where I am going wrong? Thank you in advance for any input.
It appears you're using Java Swing. Don't forget to add "implements ActionListener" on the end of your class signature.
Try this:
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();
}
}
...
}
Did you import the other class at the befinning?
import FileConverter;
check if in the begining of the file you have something like
import bla.bla.FileConverter;
if not you need to add it and be sure that FileConverter class implementation is on class pass. ad a jar with it to classpath of the project what ever IDE you use
What the hell? Eclipse baffles the hell out of me. I tried to follow everyone's advice here, but it didn't work. But when I go to restart eclipse...everything works fine.
Strange :/
Thanks everyone for the input though, I really appreciate it!
精彩评论