当前位置: 技术问答>java相关
怎样在applet中打开新的网页-- 下程序为何出错??
来源: 互联网 发布时间:2015-02-08
本文导语: import java.awt.Graphics; import java.util.StringTokenizer; import java.awt.Image; import java.net.URL; import java.net.MalformedURLException; import java.applet.Applet; import java.awt.Image; import java.awt.Graphics; import java.awt.Rectangle; import java.util.StringTokeni...
import java.awt.Graphics;
import java.util.StringTokenizer;
import java.awt.Image;
import java.net.URL;
import java.net.MalformedURLException;
import java.applet.Applet;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.Hashtable;
import java.net.URL;
import java.awt.image.*;
import java.net.MalformedURLException;
import java.awt.Graphics;
import java.awt.Graphics;
import java.applet.AudioClip;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.StringTokenizer;
import java.awt.Graphics;
import java.net.URL;
import java.net.MalformedURLException;
import java.applet.Applet;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.Hashtable;
import java.net.URL;
import java.awt.image.*;
import java.net.MalformedURLException;
import java.awt.Graphics;
import java.awt.Image;
import java.net.URL;
import java.net.MalformedURLException;
import java.applet.Applet;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.MediaTracker;
import java.awt.event.*;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.Hashtable;
import java.net.URL;
import java.awt.image.ImageProducer;
import java.awt.image.ImageFilter;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.net.MalformedURLException;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.*;
import java.util.StringTokenizer;
import java.net.URL;
import java.net.MalformedURLException;
import java.awt.Graphics;
import java.net.URL;
import java.net.MalformedURLException;
import java.awt.Graphics;
import java.awt.Graphics;
import java.applet.AudioClip;
import java.net.URL;
import java.net.MalformedURLException;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.util.StringTokenizer;
import java.applet.Applet;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.Hashtable;
import java.net.URL;
import java.awt.image.*;
import java.net.MalformedURLException;
import java.net.*;
import java.io.*;
import java.awt.Graphics;
import java.awt.Image;
import java.net.URL;
import java.net.MalformedURLException;
import java.applet.Applet;
import java.awt.*;
import java.net.URL;
import java.awt.image.*;
import java.awt.event.*;
public class Applet1 extends Applet {
// the size of the window
URL url3;
int h1,h2,h3,h4;
Dimension windowSize;
// the find01 image information
Image image;
int imageHeight = 0;
int imageWidth = 0;
boolean imageError = false;
// use the following image observer to determine
// when the image load operation is complete
ImageObserver observer = new Observer();
/**
* Set up the image to be loaded by the new
* thread.
*/
public void init()
{try{
url3=new URL("http://100.100.100.25:8080");
}
catch(MalformedURLException me)
{
System.out.println("dddd");
}
catch(IOException me)
{
System.out.println("dddd");
}
showDocument(url3,"_blank");///这条语句出错
showStatus("fff");
h1=1;
// set the background color to white
this.setBackground(Color.white);
// first calculate the size of the applet window
windowSize = this.getSize();
// get the image
URL url = this.getCodeBase();
image = this.getImage(url, "find01.gif");
}
class Observer implements ImageObserver
{
public boolean imageUpdate(Image dummy,int status,int x,int y,int width,int height)
{
if (status ==ImageObserver.ALLBITS)
repaint();
boolean returnVal = true;
if ((status & ImageObserver.HEIGHT) != 0)
{
imageHeight = height;
}
if ((status & ImageObserver.WIDTH) != 0)
{
imageWidth = width;
}
if ((status & ImageObserver.ALLBITS) ==
ImageObserver.ALLBITS)
{
returnVal = false;
}
if ((status & ImageObserver.ABORT) != 0)
{
imageError = true;
returnVal = false;
}
//////// repaint();
return returnVal;
}
}
/**
* If the image has been loaded, display it;
* otherwise, display an error message.
*/
Dimension offset = new Dimension();
Button button1 = new Button();
Button button2 = new Button();
Button button3 = new Button();
public void paint(Graphics g)
{
g.drawString("ffffffff",10,10);
// if an image error has occurred...
if (imageError)
{
// output an error message
String s = "Image load failed";
FontMetrics fm = g.getFontMetrics();
offset.width = (windowSize.width - fm.stringWidth(s))/2;
offset.height= windowSize.height/2;
g.drawString(s, offset.width, offset.height);
return;
}
/////////////g.drawImage(image,(windowSize.width - imageWidth)/2,(windowSize.height - imageHeight)/2,observer);
///g.drawImage(image,0,0,observer);
g.drawImage(image,0,60,400,460,0,0,200/h1,200/h1,observer);
}
public Applet1() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
button1.setLabel("放大 +");
button1.setBounds(new Rectangle(1, 0, 75, 31));
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button1_actionPerformed(e);
}
});
this.setLayout(null);
button2.setLabel("button2");
button2.setBounds(new Rectangle(76, 0, 75, 31));
button2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
button2_mouseClicked(e);
}
});
button2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button2_actionPerformed(e);
}
});
this.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
this_mouseClicked(e);
}
});
this.setBackground(Color.orange);
this.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
this_mouseClicked(e);
}
});
button3.setLabel("button3");
button3.setBounds(new Rectangle(152, 0, 75, 33));
button3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button3_actionPerformed(e);
}
});
this.add(button1, null);
this.add(button2, null);
this.add(button3, null);
}
void button1_actionPerformed(ActionEvent e) {
h1=h1*2;
repaint();
}
void button2_actionPerformed(ActionEvent e) {
if(h1>=2)
h1=h1/2;
repaint();
}
void this_mouseClicked(MouseEvent e) {
h1=h1*2;
repaint();
}
void button2_mouseClicked(MouseEvent e) {
}
void button3_actionPerformed(ActionEvent e) {
showStatus("fff");
}
}
///
"Applet1.java": Error #: 300 : method showDocument(java.net.URL, java.lang.String) not found in class Applet1 at line 142, column 1
import java.util.StringTokenizer;
import java.awt.Image;
import java.net.URL;
import java.net.MalformedURLException;
import java.applet.Applet;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.Hashtable;
import java.net.URL;
import java.awt.image.*;
import java.net.MalformedURLException;
import java.awt.Graphics;
import java.awt.Graphics;
import java.applet.AudioClip;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.StringTokenizer;
import java.awt.Graphics;
import java.net.URL;
import java.net.MalformedURLException;
import java.applet.Applet;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.Hashtable;
import java.net.URL;
import java.awt.image.*;
import java.net.MalformedURLException;
import java.awt.Graphics;
import java.awt.Image;
import java.net.URL;
import java.net.MalformedURLException;
import java.applet.Applet;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.MediaTracker;
import java.awt.event.*;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.Hashtable;
import java.net.URL;
import java.awt.image.ImageProducer;
import java.awt.image.ImageFilter;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.net.MalformedURLException;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.*;
import java.util.StringTokenizer;
import java.net.URL;
import java.net.MalformedURLException;
import java.awt.Graphics;
import java.net.URL;
import java.net.MalformedURLException;
import java.awt.Graphics;
import java.awt.Graphics;
import java.applet.AudioClip;
import java.net.URL;
import java.net.MalformedURLException;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.util.StringTokenizer;
import java.applet.Applet;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.Hashtable;
import java.net.URL;
import java.awt.image.*;
import java.net.MalformedURLException;
import java.net.*;
import java.io.*;
import java.awt.Graphics;
import java.awt.Image;
import java.net.URL;
import java.net.MalformedURLException;
import java.applet.Applet;
import java.awt.*;
import java.net.URL;
import java.awt.image.*;
import java.awt.event.*;
public class Applet1 extends Applet {
// the size of the window
URL url3;
int h1,h2,h3,h4;
Dimension windowSize;
// the find01 image information
Image image;
int imageHeight = 0;
int imageWidth = 0;
boolean imageError = false;
// use the following image observer to determine
// when the image load operation is complete
ImageObserver observer = new Observer();
/**
* Set up the image to be loaded by the new
* thread.
*/
public void init()
{try{
url3=new URL("http://100.100.100.25:8080");
}
catch(MalformedURLException me)
{
System.out.println("dddd");
}
catch(IOException me)
{
System.out.println("dddd");
}
showDocument(url3,"_blank");///这条语句出错
showStatus("fff");
h1=1;
// set the background color to white
this.setBackground(Color.white);
// first calculate the size of the applet window
windowSize = this.getSize();
// get the image
URL url = this.getCodeBase();
image = this.getImage(url, "find01.gif");
}
class Observer implements ImageObserver
{
public boolean imageUpdate(Image dummy,int status,int x,int y,int width,int height)
{
if (status ==ImageObserver.ALLBITS)
repaint();
boolean returnVal = true;
if ((status & ImageObserver.HEIGHT) != 0)
{
imageHeight = height;
}
if ((status & ImageObserver.WIDTH) != 0)
{
imageWidth = width;
}
if ((status & ImageObserver.ALLBITS) ==
ImageObserver.ALLBITS)
{
returnVal = false;
}
if ((status & ImageObserver.ABORT) != 0)
{
imageError = true;
returnVal = false;
}
//////// repaint();
return returnVal;
}
}
/**
* If the image has been loaded, display it;
* otherwise, display an error message.
*/
Dimension offset = new Dimension();
Button button1 = new Button();
Button button2 = new Button();
Button button3 = new Button();
public void paint(Graphics g)
{
g.drawString("ffffffff",10,10);
// if an image error has occurred...
if (imageError)
{
// output an error message
String s = "Image load failed";
FontMetrics fm = g.getFontMetrics();
offset.width = (windowSize.width - fm.stringWidth(s))/2;
offset.height= windowSize.height/2;
g.drawString(s, offset.width, offset.height);
return;
}
/////////////g.drawImage(image,(windowSize.width - imageWidth)/2,(windowSize.height - imageHeight)/2,observer);
///g.drawImage(image,0,0,observer);
g.drawImage(image,0,60,400,460,0,0,200/h1,200/h1,observer);
}
public Applet1() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
button1.setLabel("放大 +");
button1.setBounds(new Rectangle(1, 0, 75, 31));
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button1_actionPerformed(e);
}
});
this.setLayout(null);
button2.setLabel("button2");
button2.setBounds(new Rectangle(76, 0, 75, 31));
button2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
button2_mouseClicked(e);
}
});
button2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button2_actionPerformed(e);
}
});
this.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
this_mouseClicked(e);
}
});
this.setBackground(Color.orange);
this.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
this_mouseClicked(e);
}
});
button3.setLabel("button3");
button3.setBounds(new Rectangle(152, 0, 75, 33));
button3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button3_actionPerformed(e);
}
});
this.add(button1, null);
this.add(button2, null);
this.add(button3, null);
}
void button1_actionPerformed(ActionEvent e) {
h1=h1*2;
repaint();
}
void button2_actionPerformed(ActionEvent e) {
if(h1>=2)
h1=h1/2;
repaint();
}
void this_mouseClicked(MouseEvent e) {
h1=h1*2;
repaint();
}
void button2_mouseClicked(MouseEvent e) {
}
void button3_actionPerformed(ActionEvent e) {
showStatus("fff");
}
}
///
"Applet1.java": Error #: 300 : method showDocument(java.net.URL, java.lang.String) not found in class Applet1 at line 142, column 1
|
applet中打开新的网页用getAppletContext().showDocument(URL)不就可以了么