第一个applet - 浏览器中的Null Point异常?

时间:2014-06-12 01:39:16

标签: java swing applet

我知道这个问题可能是TON问的问题,但据我所知,这个问题非常具有情境性,到目前为止我没有尝试过任何东西。

我正在制作一个基本的小程序,只需要一个图像和一个简单的功能。当用户点击按钮时,我特别会从“办公室”中随机引用Dwight(稍后他们的文本输入将对如何确定报价有一些说法。)

我的问题虽然是applet在我的JGrasp中运行,但是不能在浏览器中从本地文件运行。代码基本上是完整的,但任何人都可以帮我确定哪里出错了?我只是不明白它如何在我的applet查看器中工作,而不是我的浏览器,这很奇怪。我尝试将我的SWING切换到AWT,但它似乎没有帮助,所以我使用了我的旧版本。

当applet失败时,我正在发布我的代码,HTML文件和来自java控制台的结果。目前我还在试图弄清楚从java控制台打印到底是什么,但是我想在此期间发布这个内容。感谢您的帮助!

// Imports
import java.awt.*;  
import java.awt.event.*;    
import javax.swing.*;   
import java.util.*;
import java.io.*;
import java 

public class AskDwight extends JApplet {

   // initialize GUI components
   private JPanel panel;
   private JLabel questionLabel, responseLabel;
   private JButton askButton;
   private JTextField questionField;

   // Constructor
   public void init()
   {
      buildGUI();
      // Add panel to content pane
      add(panel);  
   }

   // buildPanel method adds components to GUI
   private void buildGUI()
   {
      questionLabel = new JLabel("Question :");
      questionField = new JTextField(35);
      askButton = new JButton("Ask!");
      java.net.URL imageURL = AskDwight.class.getResource("dwight.PNG");
      JLabel imageLabel = new JLabel();
      imageLabel.setIcon(new ImageIcon(imageURL));
      askButton.setPreferredSize(new Dimension(350, 30));

      // Response label builder
      responseLabel = new JLabel("");
      responseLabel.setPreferredSize(new Dimension(350, 200));
      responseLabel.setHorizontalAlignment(JLabel.CENTER);
      responseLabel.setVerticalAlignment(JLabel.CENTER);

      // Add an action listener to the button
      askButton.addActionListener(new AskButtonListener());

      // Create panels
      panel = new JPanel(new FlowLayout());

      // Add components to the panels
      panel.add(questionLabel);
      panel.add(questionField);
      panel.add(askButton);
      panel.add(imageLabel);
      panel.add(responseLabel);   
   }

   // Action Listener class for the ask button
   private class AskButtonListener implements ActionListener
   {
      // Ask method exectutes when the button is clicked
      public void actionPerformed(ActionEvent e)
      {
         String inputQuestion;                 // Holds user's input question
         String[] response = new String[10];  // Holds Dwight's random responses
         String randomResponse;

         // Dwight Responses - contained in String array
         response[0] = "<html>Fact: You can use the molten goose grease and save "
                        + "it in the refrigerator.</html>";
         response[1] = "<html>False: Bears do not eat beats.</html>";
         response[2] = "<html>Question: Do you ever stop asking stupid questions?</html>";
         response[3] = "<html>Fact: Bears can climb faster than they can run.</html>";
         response[4] = "<html>What are you even asking? I order you to Cease and "
                       + "desist right now, as third in command</html>.";
         response[5] = "<html>You think that's funny? Millions suffer every year at "
                       + "the expense of your jokes.</html>.";
         response[6] = "<html>MICHAEL!";
         response[7] = "<html>A day on Schrute's beet farm would shut your mouth.</html>";
         response[8] = "<html>False: Nothing you say is important.</html>";
         response[9] = "<html>I always keep concealed pepper spray just for an "
                       + "occasion such as this.</html>";

         // inputQuestion gets the user's entered string
         inputQuestion = questionField.getText();

         // When button is pressed, the following code will select
         // a random response from the string array
         Random rand = new Random();
         randomResponse = response [rand.nextInt(response.length)];
         responseLabel.setText(randomResponse);

      }
   }  

}

这是HTML代码

<HTML>
<HEAD>
<TITLE>Ask Dwight Schrute</TITLE>
</HEAD>
<BODY>

<applet code="AskDwight.class" width="420" height ="500">
</applet>

</BODY>
</HTML>

1 个答案:

答案 0 :(得分:1)

您已经指定了程序类,但没有提供应用程序存档,这意味着对Class#getResource的任何调用都将失败,因为applet的类加载器没有关于这些资源的位置的概念可能存储,而是考虑尝试类似......

<applet code = 'AskDwight' 
    archive = 'AskDwight.jar'
    width = 420
    height = 500>
</applet>

假设您已将项目构建到Jar并将其部署在服务器上......

自从我完成任何applet程序以来已经很长时间了,所以这可能会稍微偏离......

仔细查看Deploying an Applet了解更多详情

根据反馈更新

如果您无法使用Jar文件,那么您需要使用类似Image img = getImage(getCodeBase(), "dwight.PNG");的内容来加载图片......

图片需要存储在与您的类文件相同的位置,例如,如果AskDwight.class不属于某个包,则它们将位于同一位置(位于驱动器上)。如果AskDwight.class属于com.foo.bar包,则图片文件将位于com

上方的目录中