【问题标题】:Java Applet code works in browser but not in AppletViewerJava Applet 代码在浏览器中有效,但在 AppletViewer 中无效
【发布时间】:2012-09-21 10:13:11
【问题描述】:

当我在浏览器中使用此小程序时,它可以正常工作,但为什么不能在小程序查看器中使用?

我曾尝试同时使用 jGRASP 和 Eclipse 来查看小程序,但无论我做什么,我都得到了这个:

java.lang.NumberFormatException: null on this line of code

int paramCount = Integer.parseInt( getParameter( "count" ) );

我不明白它为什么这样做。

//file: AppletParameters.java

import javax.swing.JApplet;
import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.GridLayout;

public class AppletParameters extends JApplet
{
    private JPanel panel; // panel to display pictures

    public void init( )
    {
        // get the parameter count from the html 'count' parameter
        int paramCount = Integer.parseInt( getParameter( "count" ) );
        // create an array
        ImageIcon [] image = new ImageIcon[paramCount];
        // get each file name from the html 'file' parameter and put into array
        for ( int k=0; k<paramCount; k++ )
            image[k] = new ImageIcon( getImage( getDocumentBase( ), getParameter( "file"+k ) ) );
        // build a new JPanel with GridLayout
        panel = new JPanel( new GridLayout( 2, 5 ) );
        // add images to the panel
        for ( int k=0; k<paramCount; k++ )
            panel.add( new JLabel( image[k] ) );
        // add panel to me (this applet object)
        add( panel );
    }  // end init method
}  // end class

【问题讨论】:

    标签: java applet viewer appletviewer


    【解决方案1】:

    您是否通过 AppletViewer 将参数传递给小程序?我怀疑你是。

    在 Eclipse 中,您可以通过 Run 菜单 Run Configurations... 子菜单项,然后是 Parameters 选项卡来执行此操作。

    【讨论】:

    • 我会传递哪些参数?我找到了可以通过它们的地方,但不确定会去那里。我会在那里传递“计数”和图像文件吗?
    • @biff:您的代码准确地告诉您需要哪些参数。您在代码中使用了哪些参数?有两个。
    • @biff:甚至代码 cmets 都会告诉您需要哪些参数。但是是的,您需要count 来获取图像数量,file 来获取图像文件的基本名称。试一试,因为你没有什么可失去的。
    • @biff:没什么大不了的。我只是在拉你的腿。
    猜你喜欢
    • 1970-01-01
    • 2020-11-10
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    相关资源
    最近更新 更多