gpt4 book ai didi

java - 在 Java 中导入自定义类,使用命令行

转载 作者:行者123 更新时间:2023-12-01 22:55:40 25 4
gpt4 key购买 nike

我在同一个文件夹中编写了几个类,这是层次结构:

bookpirate/

-Window.java

-Navigator.java

-NavEventHandler.java

所有文件开头都有“package bookpirate;”

这是我导入它们的方法:

导航器类:

package bookpirate;

import javax.swing.*;
import java.awt.*;
import bookpirate.NavEventHandler;

public class Navigator
{
private JPanel panel;

public JTextField page;

public JButton pageBack;
public JButton addBefore;
public JButton addAfter;
public JButton pageForward;
public JButton removePage;
public JButton undo;

public JTextField getPage() { return page; }

public JButton getPageBack() { return pageBack; }
public JButton getAddBefore() { return addBefore; }
public JButton getAddAfter() { return addAfter; }
public JButton getPageForward() { return pageForward; }
public JButton getRemovePage() { return removePage; }
public JButton getUndo() { return undo; }

public int nOfPages;
public int getNOfPages() { return nOfPages; }

public static void main(String[] args)
{
new Navigator();
}

public Navigator()
{
setListeners();
System.out.println("Created NavEventHandler");
}

private void setListeners()
{
NavEventHandler handler = new NavEventHandler(this);

page.addActionListener(handler);

pageBack.addActionListener(handler);
addBefore.addActionListener(handler);
addAfter.addActionListener(handler);
pageForward.addActionListener(handler);
removePage.addActionListener(handler);
undo.addActionListener(handler);

}
}

NavEventHandler 类:

package bookpirate;

import java.awt.event.*;
import javax.swing.*;
import bookpirate.Navigator;

public class NavEventHandler implements ActionListener
{
Navigator navigator;
public NavEventHandler(Navigator nav)
{
navigator = nav;
System.out.println("Created NavEventHandler");
}

public void actionPerformed(ActionEvent event)
{
String string = "actionPerformed";
//if(event.getSource() instanceof Navigator)
//{
if(event.getSource() == navigator.getPageBack())
{ string = "pageBack";}
/*else if(event.getSource() == Navigator.getAddBefore())
{ string = "addBefore";}
else if(event.getSource() == Navigator.getAddAfter())
{ string = "addAfter";}
else if(event.getSource() == Navigator.getPageForward())
{ string = "pageForward";}
else if(event.getSource() == Navigator.getRemovePage())
{ string = "removePage";}
else if(event.getSource() == Navigator.getUndo())
{ string = "undo";}
else if(event.getSource() == Navigator.getPage())
{ string = "page " + event.getActionCommand();}*/
string += " gotSource";
//}
System.out.println(string);
}

public static void main(String[] args)
{

}
}

这是我编译 NavEnventHandler 时得到的结果:

C:\Users\...\bookpirate>javac NavEventHandler.java
NavEventHandler.java:5: error: cannot find symbol
import bookpirate.Navigator;
^
symbol: class Navigator
location: package bookpirate
NavEventHandler.java:9: error: cannot find symbol
Navigator navigator;
^

尽管我可以将它们毫无错误地编译在一起

C:\Users\Falangrater\Desktop\Files\Programming\bookpirate>javac NavEventHandler.java Navigator.java Window.java

C:\Users\Falangrater\Desktop\Files\Programming\bookpirate>

但我无法一起或单独运行它们

C:\Users\Falangrater\Desktop\Files\Programming\bookpirate>java Window Navigator NavEventHandler
Error: Could not find or load main class Window

C:\Users\Falangrater\Desktop\Files\Programming\bookpirate>

我尝试过制作一个manifest.txt文件

Main-Class: Window


我无法将 .mf 添加到文件

我尝试尽可能简洁

最佳答案

您不必从同一个包中导入类。 Why don't I have to import a class I just made to use it in my main class? (Java)

您的任何类中都没有 main 方法,因此您无法以这种方式运行它。更多:Can we execute a java program without a main() method?

编辑:您只需移动到父目录并这样调用它:

java bookpirate.Navigatorjava bookpirate/Navigator

关于java - 在 Java 中导入自定义类,使用命令行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58441586/

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