【问题标题】:JApplet not running: < No main classes found >JApplet 未运行:< 未找到主要类 >
【发布时间】:2014-06-20 08:38:53
【问题描述】:

当 DrawOvalInputs.html 运行并调用 DrawOvalInputs 的类文件时,我的 JApplet 产生了错误。到目前为止,我只能将它作为一个实际的应用程序运行(这就是 main 在块引用中的原因)。

我对这个程序的目标是能够运行一个 .html 文件以在 Java 控制台上以中等安全设置启动 JApplet,但无论我做什么,它都无法运行。

在来到这里之前,我已经浏览了很多页面并进行了搜索。遗憾的是,我无法弄清楚这个 JApplet,所以如果有人能指导我正确的方向,我将不胜感激!

我的代码如下:

DrawOvalInputs.java

package drawovalapplet;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Graphics;
import javax.swing.JApplet;
import javax.swing.JOptionPane;

/**
 * This applet inputs a number of values, and then computes the size of
 * an oval with those given values. 
 *
 * @author  [Redacted]
 * @version 2014-05-02, [Redacted]
 */
public class DrawOvalInputs extends JApplet
{
    private static int x;               //left edge of the oval
    private static int y;               //top edge of the oval
    private static int width;           //width of oval
    private static int height;          //height of oval
    private static int windowWidth;     //Holds necessary width of window.
    private static int windowHeight;    //Holds necessary height of window.
    private static int windowBox;       //Holds box form of window.


@Override
public void init()
{    
    try 
    {
        /** Collect the input values. */    
        Input.inputAll();

        /** creates dimensions for box */
        windowWidth = width + x + 50;
        windowHeight = height + y + 50;

        /** 
         * If... else... function to gather data to create
         * a fitting box for the oval
         */
        if(windowWidth > windowHeight)
            windowBox = windowWidth;
        else
            windowBox = windowHeight;

    } catch (Input.CanceledException ex) {
        System.exit(1);
    }
} 

@Override
public void paint(Graphics g)
{
    super.paint(g);      
    g.drawOval(x, y, width, height);
} // end method pain


/**
 * Main entry point.
 * <p> Execute:
 * <pre>java drawovalapplet.DrawOvalInputs</pre>
 *
 * @param args not used.
 */
/*public static void main(String args[])
{
Frame frame = new Frame("DrawOvalInputs");
DrawOvalInputs drawOval = new DrawOvalInputs();
drawOval.init();
drawOval.start();
frame.add(drawOval);
frame.setSize(drawOval.windowBox, drawOval.windowBox);
frame.setVisible(true);
}*/

//------------------------------- Nested Classes --------------------------------

/**
 * Enumeration of the name and value of the input values.
 */
private enum Input
{
/**
 * Message for the entering the x coordinate.
 */
XVALUE("Enter the argument for the x coordinate of the upper left corner of the oval to be drawn:"),

/**
 * Message for the entering the y coordinate.
 */
YVALUE("Enter the argument for the y coordinate of the upper left corner of the oval to be drawn:"),

/**
 * Message for entering the width.
 */
WIDTHVALUE("Enter the desired width of the oval to be drawn:"),

/**
 * Message for entering the height.
 */
HEIGHTVALUE("Enter the desired height of the oval:");

/**
 * String to use in messages (from the constructor).
 */
protected String invitation;

/**
 * String to use for error messages.
 */
protected String error = "Not an integer value--please re-enter:";

/**
 * Value of this {@literal <variable>}.
 */
protected int value;

/**
 * @param label string to use in messages
 */
Input(String invitation)
{
    this.invitation = invitation;
}

public static void inputAll() throws CanceledException
{
    /* Decide which input value is currently being used. */
    int count = 0; 

    /* Collect the input numbers. */
    for(Input input : Input.values())
    {
        /* Set up the invitation to enter each number. */
        String message = input.invitation;

        /* Loop until the user inputs an acceptably formatted number. */
        while(true)                         // repetition environment
        {
            String response;
            /* Null return from the JOptionPane indicates CANCEL was pressed. */
            if( (response = JOptionPane.showInputDialog(message)) == null)
                throw new CanceledException();
            message = input.error;              // just in case
            try
            {
                input.value = Integer.parseInt(response);
                break;                          // success in acquiring value
            }
            catch(NumberFormatException nfe) {}// ignore all, and try again
        }
        count++;
        if(count == 1)
            x = input.value;
        else if(count == 2)
            y = input.value;
        else if (count == 3)
            width = input.value;
        else if (count == 4)
            height = input.value;
        else
            System.out.println("Error. Revise.");   
    }
}

@SuppressWarnings("serial")
public static class CanceledException extends Exception {}
}
}

DrawOvalInputs.html 我已经用 DrawOvalInputs.java 和下面的那个以及 .class 来运行它。

<html>
  <body>
      <applet code=drawovalapplet.DrawOvalInputs.java width=400 height=400>
      </applet>
  </body>
</html>

谢谢!

【问题讨论】:

  • 更新: 在查看了用于创建小程序的 Netbeans 页面(如下)后,我尝试了使用 JDK 1.8 和 1.7 以 JApplet 和 Applet 格式呈现的所有格式考虑到可能有什么不对劲或损坏。两个 JDK 都重新安装以防万一,但仍然没有任何功能。 netbeans.org/kb/docs/web/applets.html
  • 你为什么评论/*public static void main(String args[])
  • 我相信 JApplet 应该能够在浏览器中运行,并被初始化为 JApplet 而不是常规桌面应用程序。或者至少,从它们被访问和运行的两种不同形式中。如果我理解正确,它会通过浏览器如下所述的覆盖方法运行。 public void init();public void start();public void stop();public void init();public void destroy();
  • public void paint(); 而不是第二个初始化,对不起。
  • “我对这个程序的目标是能够运行一个 .html 文件来启动 JApplet..” 对于部署 Java 桌面应用程序,最好的选择是通常安装应用程序。使用Java Web Start。 JWS 适用于 Windows、OS X 和 *nix。

标签: java html netbeans applet japplet


【解决方案1】:

你必须用正确的路径调用它

如果你的路径是这样的

DrawOvalApplet\build\classes\drawovalapplet\DrawOvalInputs.class

你的 .html 在

DrawOvalApplet\build\DrawOvalInputs.html

叫它

 ...
      <applet code=classes.drawovalapplet.DrawOvalInputs.class width=400 height=400>
      </applet>
 ...

好多了你的 .html 在

DrawOvalApplet\build\classes\DrawOvalInputs.html

叫它

 ...
      <applet code=drawovalapplet.DrawOvalInputs.class width=400 height=400>
      </applet>
 ...

结果:

小程序运行

您可以拨打htmlconverter,让我们为您服务

java -jar htmlconverter.jar -gui

结果DrawOvalInputs.html

<html>
  <body>
      <!--"CONVERTED_APPLET"-->
<!-- HTML CONVERTER -->
<object
    classid = "clsid:CAFEEFAC-0017-0000-0051-ABCDEFFEDCBA"
    codebase = "http://java.sun.com/update/1.7.0/jinstall-7u51-windows-i586.cab#Version=7,0,510,13"
    WIDTH = 400 HEIGHT = 400 >
    <PARAM NAME = CODE VALUE = drawovalapplet.DrawOvalInputs.class >
    <param name = "type" value = "application/x-java-applet;jpi-version=1.7.0_51">
    <param name = "scriptable" value = "false">

    <comment>
    <embed
            type = "application/x-java-applet;jpi-version=1.7.0_51" \
            CODE = drawovalapplet.DrawOvalInputs.class \
            WIDTH = 400 \
            HEIGHT = 400
        scriptable = false
        pluginspage = "http://java.sun.com/products/plugin/index.html#download">
        <noembed>

            </noembed>
    </embed>
    </comment>
</object>

<!--
<APPLET CODE = drawovalapplet.DrawOvalInputs.class WIDTH = 400 HEIGHT = 400>


</APPLET>
-->
<!--"END_CONVERTED_APPLET"-->

  </body>
</html>

【讨论】:

  • 所以它是代码的路径参数,或者在某些格式中,代码库和代码。令我惊讶的是,这些事情竟然如此简单。非常感谢您的全面回答,@moskito-x,我非常感激 =)
  • @Kelenth:不客气。我什至经常偶然发现错误的路径分配。
  • &lt;!-- HTML CONVERTER --&gt; 部署 JWS 应用程序的最佳方式。或者小程序是使用Deployment Toolkit Script
【解决方案2】:

使用类文件和实际的文件路径和名称。例如:

 <applet code=DrawOvalInputs.class width=400 height=400>
  </applet>

如果类文件与您的 HTML 文件位于同一文件夹中。

【讨论】:

  • 感谢您的回复。我只是尝试使用 DrawOvalInputs.class 从 DrawOvalInputs.html 文件运行它,但是 chrome 有一个 Major.minor 52 错误,所以我需要非常快地解决这个问题!您知道直接从 Netbeans IDE 8.0 执行此操作的方法吗?我只能在源包中找到我的 .java 和 Html,而不是类。
  • 我建议你看看netbeans.org/kb/docs/web/applets.html,它几乎涵盖了整个过程。我认为它还会为您提供一个有效的 HTML 页面。
  • 我真的不知道出了什么问题。我一步一步地做了 Netbeans IDE 上的说明,它仍然出现同样的问题?有什么想法吗?
  • 主要/次要问题是另一个问题。检查stackoverflow.com/questions/10382929/…
猜你喜欢
  • 1970-01-01
  • 2021-02-20
  • 2015-05-31
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-15
  • 2014-06-13
相关资源
最近更新 更多