java - 第一个小程序 - 浏览器中出现空点异常?

标签 java swing applet

我知道这个问题可能被问了很多次,但据我了解,这是相当具体的情况,而且到目前为止我尝试过的任何方法都不起作用。

我正在制作一个基本的小程序,只需要一个图像和一个简单的功能。当用户点击按钮时,我的特别是随机引用“办公室”中的德怀特(稍后他们的文本输入将对如何确定引用有某种发言权)。

我的问题是小程序在我的 JGrasp 中运行,但不在浏览器中从本地文件运行。代码基本上已经完成了,但是谁能帮我找出哪里出了问题吗?我只是不太明白它如何在我的小程序查看器中工作,而不是在我的浏览器中工作,这很奇怪。我尝试将 SWING 切换到 AWT,但似乎没有帮助,因此我使用了旧版本。

当小程序失败时,我将发布我的代码、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>

最佳答案

您已指定程序类,但未提供应用程序存档,这意味着对 Class#getResource 的任何调用将会失败,因为小程序的类加载器不知道这些资源可能存储在哪里,相反,请考虑尝试类似...

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

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

自从我完成任何小程序程序以来已经(非常)长时间了,所以这可能有点不对劲......

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

根据反馈进行更新

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

图像需要存储在与类文件相同的位置,例如,如果 AskDwight.class不属于一个包,它们将驻留在同一位置(在驱动器上)。如果AskDwight.class属于com.foo.bar包,图像文件将位于 com 上面的目录中

关于java - 第一个小程序 - 浏览器中出现空点异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24175232/

相关文章:

java - 为什么缺少一条消息?如何修复它?

java - `java.lang.ClassFormatError` 错误代码列表

java - 由 JButton 组成的 ArrayList。但没有按预期工作

java - 如果我们可以使用代码库,小程序中的主机参数是什么?

java - 自签名小程序..只有套接字权限?

java - 无法在浏览器中运行Java Applet

java - Material 设计在 android studio 中不起作用

java程序要求用户输入第二大和最大的数字

java - Maven 构建后无法打开 PDF

JAVA 将 Socket 包装为多屏 Swing 应用程序中的 Singleton 类