Java程序不能作为Java应用程序运行
我正在尝试运行一年前为一个类创建的Java项目,但是遇到了一些问题 当我尝试运行这个java项目时,eclipse中没有将其作为java应用程序运行的选项。相反,它只允许我选择Java程序不能作为Java应用程序运行,java,eclipse,swing,ant,Java,Eclipse,Swing,Ant,我正在尝试运行一年前为一个类创建的Java项目,但是遇到了一些问题 当我尝试运行这个java项目时,eclipse中没有将其作为java应用程序运行的选项。相反,它只允许我选择Ant Build,选择后会抛出错误:无法找到要运行的Ant文件。我的代码包含一个主函数,因此问题出现了:为什么我的代码不只是运行主函数 注意:我不想发布我的全部代码,因为它有将近1000行长,分为6个类,但是如果我收到一条要求完整的注释,我会的。包含的只是主要类 我注意到其他类的顶部包括行package edu.trum
Ant Build
,选择后会抛出错误:无法找到要运行的Ant文件。
我的代码包含一个主函数,因此问题出现了:为什么我的代码不只是运行主函数
注意:我不想发布我的全部代码,因为它有将近1000行长,分为6个类,但是如果我收到一条要求完整的注释,我会的。包含的只是主要类
我注意到其他类的顶部包括行package edu.truman.cs260.talpersP3代码>。我只是从我的电子邮件收件箱下载了这些java文件,所以我是否需要对它们进行打包
我的主要班级:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import edu.truman.cs260.talpersP3.*;
public class TalpersProject3
{
private static final int FRAMES_PER_SECOND = 24;
private static final int ICON_WIDTH = 500;
private static final int ICON_HEIGHT = 500;
private static final int DELAY = 1000 / FRAMES_PER_SECOND;
public static void main(String[] args)
{
//constructs the AnimationComponent
final AnimationComponent a = new AnimationComponent();
//creates frame and buttonpanel
JFrame frame = new JFrame();
JPanel buttonpanel;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//declares the two buttons
JButton squarebutton = new JButton("Square");
JButton circlebutton = new JButton("Circle");
//button implementation
squarebutton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
a.add(new BouncySquare(50, 50, 100));
a.repaint();
}
});
circlebutton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
a.add(new BouncyCircle(50, 50, 50));
a.repaint();
}
});
//sets the size of the AnimationComponent
a.setSize(ICON_WIDTH,ICON_HEIGHT);
//constructs the buttonpanel
buttonpanel = new JPanel();
//adds the 2 buttons to the panel
buttonpanel.add(squarebutton);
buttonpanel.add(circlebutton);
//frame layout and formatting
frame.setLayout(new BorderLayout());
frame.add(buttonpanel, BorderLayout.SOUTH);
frame.add(a, BorderLayout.CENTER);
frame.setSize(ICON_WIDTH, ICON_HEIGHT+100);
frame.setVisible(true);
//construction of the timer
Timer t = new Timer(DELAY, new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
a.bounceCall(); //checks bounds and translates
a.repaint();
}
});
//timer starts
t.start();
}
}
由于您的类具有适当的main(..),它应该在上下文菜单中真正显示“作为应用程序运行”。也许Eclipse项目以某种方式被破坏了。获取最新版本的Eclipse,创建一个新的、空的、简单的Java项目,并将文件复制到新项目的源文件夹中 在编辑器中打开带有主类的Java文件,然后右键单击并选择“运行方式…”。。。Java应用程序。您的Java文件是否位于生成路径上?如果不是,eclicpse图标有一个轮廓“J”,如所示。您不是从Ant模板开始创建了一个项目吗?尝试使用默认Java模板创建一个新项目,并复制源代码。我猜这并不是我做错了什么,应该去申请一张故障单。Run->Run As->(不适用)它是灰色的。我如何检查以确保这是真的?我创建了一个新的java项目,将.java文件粘贴到该项目的/src文件夹中,并将文件拖到IDE中使其打开,然后尝试运行。是否正确?在编辑器中的Java文件中单击鼠标右键,然后从上下文菜单中选择“运行方式…”,而不是从顶部栏菜单中选择。它表示选择不包含主类型。
编辑器中似乎没有列出这6个类文件。有没有比创建6个新文件并将旧文件的内容粘贴到新文件中更快的方法?您可以将它们复制到Eclipse工作区下的project src目录中。按照package语句创建原始目录结构,在您的示例中,在src/edu/truman/cs260/talpersP3/下创建一个目录树,并将文件放在那里。之后需要刷新Eclipse浏览器。