当前位置: 技术问答>java相关
请大家帮我看段程序
来源: 互联网 发布时间:2015-08-17
本文导语: import javax.swing.JApplet; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; public class ApplicantApplet extends JApplet { static JPanel panel; Container content; JButton butto...
import javax.swing.JApplet;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class ApplicantApplet extends JApplet
{
static JPanel panel;
Container content;
JButton buttonAccept;
static ApplicantApplet applicant;//声明为静态变量
GridBagLayout gl;
GridBagConstraints gbc;
public void init()
{ //初始化按钮
gl=new GridBagLayout();
gbc=new GridBagConstraints();
content = getContentPane();
content.setLayout(gl);
buttonAccept = new JButton("Accept");
gbc.anchor=GridBagConstraints.NORTHEAST;
gbc.gridx=8;
gbc.gridy=11;
gl.setConstraints(buttonAccept, gbc);
content.add(buttonAccept);
validateAction validateButton = new validateAction( );
buttonAccept.addActionListener(validateButton);
}
class validateAction implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
Object obj = evt.getSource();
if(obj == buttonAccept)
{
try
{
URL docurl=new URL("http","127.0.0.1",1002,"D:/MYDOCUMENT/Applet.html");
applicant.getAppletContext().showDocument(docurl,"_self");
//执行时编译器报错,说是NullPointerException
catch (MalformedURLException e)
{
System.out.println("Can't open the URL ");
}
}
}
}
}
执行时编译器报错,说是NullPointerException ,getAppletContext( ).showDocument()是用来从服务器端下载页面的。是不是不能将applicant声明为static呢?请赐教。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class ApplicantApplet extends JApplet
{
static JPanel panel;
Container content;
JButton buttonAccept;
static ApplicantApplet applicant;//声明为静态变量
GridBagLayout gl;
GridBagConstraints gbc;
public void init()
{ //初始化按钮
gl=new GridBagLayout();
gbc=new GridBagConstraints();
content = getContentPane();
content.setLayout(gl);
buttonAccept = new JButton("Accept");
gbc.anchor=GridBagConstraints.NORTHEAST;
gbc.gridx=8;
gbc.gridy=11;
gl.setConstraints(buttonAccept, gbc);
content.add(buttonAccept);
validateAction validateButton = new validateAction( );
buttonAccept.addActionListener(validateButton);
}
class validateAction implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
Object obj = evt.getSource();
if(obj == buttonAccept)
{
try
{
URL docurl=new URL("http","127.0.0.1",1002,"D:/MYDOCUMENT/Applet.html");
applicant.getAppletContext().showDocument(docurl,"_self");
//执行时编译器报错,说是NullPointerException
catch (MalformedURLException e)
{
System.out.println("Can't open the URL ");
}
}
}
}
}
执行时编译器报错,说是NullPointerException ,getAppletContext( ).showDocument()是用来从服务器端下载页面的。是不是不能将applicant声明为static呢?请赐教。
|
static ApplicantApplet applicant;一个对象的声明,如果没有new,则被默认为null,所以这句执行后applicant is null;在后面你也没有声明,所以在执行applicant.getAppletContext().showDocument(docurl,"_self");时,它还是null,执行时,编译器报错肯定说是NullPointerException
你可以改为getAppletContext().showDocument(docurl,"_self");就OK了。
你可以改为getAppletContext().showDocument(docurl,"_self");就OK了。
|
static ApplicantApplet applicant;//声明为静态变量
申明为静态变量由什么用??
没有new,根本都用不了。。。
申明为静态变量由什么用??
没有new,根本都用不了。。。
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。