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

本文档描述了在Java项目中导入自定义类时遇到的问题以及解决方法。作者展示了如何在同一个包下导入`Navigator`和`NavEventHandler`类,以及在命令行中编译和运行这些类时的常见错误。通过正确编译所有相关文件并指定正确的主类,可以成功运行程序。
摘要由CSDN通过智能技术生成

bookpirate/

-Window.java

-Navigator.java

-NavEventHandler.java

所有文件都有

“包裹盗版”

以下是我如何导入它们的:

导航器类:

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)

{

}

}

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>

Main-Class: Window

我不能在文件中添加.mf

我尽量简洁

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值