我正在尝试运行一年前为类(class)制作的 Java 项目,但是遇到了一些问题。
当我尝试运行这个java项目时,eclipse中没有选项将其作为java应用程序运行。相反,它只允许我选择 Ant Build
,选择后会抛出错误:无法找到要运行的 Ant 文件。
我的代码包含一个 main 函数,因此问题出现了:为什么我的代码不只运行 main 函数?
注意:我不想发布我的整个代码,因为它有近千行长并分为 6 个类,但是如果我收到要求完整发布的评论,我会的。包含的只是主类。
我注意到其他类的顶部包含行package edu.truman.cs260.talpersP3;
。我只是从我的电子邮件收件箱下载了这些 java 文件,所以我需要以某种方式打包它们吗?
我的主要类(class):
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();
}
}
最佳答案
在编辑器中打开包含主类的 Java 文件,然后右键单击并从上下文菜单中选择“运行为... Java 应用程序”。
关于Java程序无法作为Java应用程序运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14219541/