首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Java程序不能作为Java应用程序运行

Java程序不能作为Java应用程序运行
EN

Stack Overflow用户
提问于 2013-01-09 00:25:42
回答 3查看 2.6K关注 0票数 0

我正在尝试运行一个Java项目,这是我一年前为一个类创建的,但是我遇到了一些问题。

当我尝试运行这个java项目时,在eclipse中没有将其作为java应用程序运行的选项。相反,它只允许我选择主函数,这会在选择时抛出错误:Unable to find Ant file to run.我的代码包含一个主函数,因此问题出现了:为什么我的代码不只是运行主函数?

注意:我不想发布我的整个代码,因为它几乎有1000行长,并分为6个类,但是如果我得到一个请求完整的评论,我会的。包含的只是主类。

我注意到其他类的顶部包含行package edu.truman.cs260.talpersP3;。我只是从我的电子邮件收件箱下载了这些java文件,所以我需要以某种方式将它们打包吗?

我的主类:

代码语言:javascript
运行
复制
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();
}
}
EN

回答 3

Stack Overflow用户

发布于 2013-01-09 00:30:42

在编辑器中打开带有主类的Java文件,然后右键单击并从上下文菜单中选择'run as ... Java Application‘。

票数 1
EN

Stack Overflow用户

发布于 2013-01-09 00:29:42

因为你的类有正确的main(..),所以它应该在上下文菜单中真正显示"Run As application“。也许Eclipse项目在某种程度上被破坏了。获取最新版本的Eclipse,创建一个新的、空的、简单的Java项目,并将文件复制到新项目的源文件夹中。

票数 0
EN

Stack Overflow用户

发布于 2014-03-31 21:40:10

您的java文件是否在构建路径中?如果不是,则eclicpse图标有一个带轮廓的'J‘,如the icons of eclipse helios所示。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14219541

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档