我正在尝试运行一个Java项目,这是我一年前为一个类创建的,但是我遇到了一些问题。
当我尝试运行这个java项目时,在eclipse中没有将其作为java应用程序运行的选项。相反,它只允许我选择主函数,这会在选择时抛出错误:Unable to find Ant file to run.我的代码包含一个主函数,因此问题出现了:为什么我的代码不只是运行主函数?
注意:我不想发布我的整个代码,因为它几乎有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();
}
}发布于 2013-01-09 00:30:42
在编辑器中打开带有主类的Java文件,然后右键单击并从上下文菜单中选择'run as ... Java Application‘。
发布于 2013-01-09 00:29:42
因为你的类有正确的main(..),所以它应该在上下文菜单中真正显示"Run As application“。也许Eclipse项目在某种程度上被破坏了。获取最新版本的Eclipse,创建一个新的、空的、简单的Java项目,并将文件复制到新项目的源文件夹中。
发布于 2014-03-31 21:40:10
您的java文件是否在构建路径中?如果不是,则eclicpse图标有一个带轮廓的'J‘,如the icons of eclipse helios所示。
https://stackoverflow.com/questions/14219541
复制相似问题