gpt4 book ai didi

java - 如何在 Java Button (GUI) 中调用外部方法

转载 作者:行者123 更新时间:2023-11-29 03:55:10 25 4
gpt4 key购买 nike

所以我有一段代码看起来像这样......

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)

所以我实现了监听器和执行的操作事件,但我不明白为什么我不能在另一个类中调用该方法?有人可以解释我哪里出错了吗?预先感谢您的任何意见。

最佳答案

看来您正在使用 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();
}
}
...

}

关于java - 如何在 Java Button (GUI) 中调用外部方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6823091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com