当前位置: 技术问答>java相关
一个非常简单的问题!!!:)
来源: 互联网 发布时间:2015-03-04
本文导语: 我定义了一个类,用到showDocument来显示网页 怎么编译的时候说出错,没这方法? class ButtonListener implements ActionListener{ public void actionPerformed(ActionEvent e){ Button button1=(Button)e.getSource(); if(button1==sohuButton) { AppletCo...
我定义了一个类,用到showDocument来显示网页
怎么编译的时候说出错,没这方法?
class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Button button1=(Button)e.getSource();
if(button1==sohuButton)
{
AppletContext.showDocument("www.sohu.com");
}
}
}
怎么编译的时候说出错,没这方法?
class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Button button1=(Button)e.getSource();
if(button1==sohuButton)
{
AppletContext.showDocument("www.sohu.com");
}
}
}
|
Goto to a new URL from an Applet
You have to use getAppletContext().showDocument
(new URL("http://www.whatever.com"));
or
getAppletContext().showDocument
(new URL("http://www.whatever.com"),"HTML frame ID");
NOTE: If "HTML frame ID" do not exists then a new browser window will be opened.
For example, we want to display lowres.html page if resolution is 640x480 else the hires.html is used.
import java.applet.*;
import java.awt.*;
import java.net.*;
public class whatres extends Applet {
public void init() {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
if (dim.width==640 && dim.height==480) {
try {
getAppletContext().showDocument
(new URL(getCodeBase()+"lowres.html"),"_top");
}
catch (Exception ex) {}
}
else {
try {
getAppletContext().showDocument
(new URL(getCodeBase()+"hires.html"),"_top");
}
catch (Exception ex) {}
}
}
}
NOTE: The previous example works only so long as the document was retrieved without specifying an actual document name, since getDocumentBase() returns the full URL including the name of the document. If the document name was specified, you should try something like this or specify the complete URL (thanks to Rob Judd): :
try {
String docString = getDocumentBase().toString();
if (docString.endsWith("/")) {
getAppletContext().showDocument
(new URL(getDocumentBase()+"lowres.html"), "_top");
}
else {
getAppletContext().showDocument
(new URL(getDocumentBase()+"/../lowres.html"), "_top");
}
}
catch (Exception e) {}
Another example, type a new URL in a textfield, and press a button to go to that page.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class GotoURLButton extends Applet implements
ActionListener {
Button b;
TextField t;
public void init() {
t = new TextField(20);
add(t);
b = new Button("Go to this URL");
add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == b) {
try {
getAppletContext().showDocument(new URL(t.getText()));
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
You have to use getAppletContext().showDocument
(new URL("http://www.whatever.com"));
or
getAppletContext().showDocument
(new URL("http://www.whatever.com"),"HTML frame ID");
NOTE: If "HTML frame ID" do not exists then a new browser window will be opened.
For example, we want to display lowres.html page if resolution is 640x480 else the hires.html is used.
import java.applet.*;
import java.awt.*;
import java.net.*;
public class whatres extends Applet {
public void init() {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
if (dim.width==640 && dim.height==480) {
try {
getAppletContext().showDocument
(new URL(getCodeBase()+"lowres.html"),"_top");
}
catch (Exception ex) {}
}
else {
try {
getAppletContext().showDocument
(new URL(getCodeBase()+"hires.html"),"_top");
}
catch (Exception ex) {}
}
}
}
NOTE: The previous example works only so long as the document was retrieved without specifying an actual document name, since getDocumentBase() returns the full URL including the name of the document. If the document name was specified, you should try something like this or specify the complete URL (thanks to Rob Judd): :
try {
String docString = getDocumentBase().toString();
if (docString.endsWith("/")) {
getAppletContext().showDocument
(new URL(getDocumentBase()+"lowres.html"), "_top");
}
else {
getAppletContext().showDocument
(new URL(getDocumentBase()+"/../lowres.html"), "_top");
}
}
catch (Exception e) {}
Another example, type a new URL in a textfield, and press a button to go to that page.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class GotoURLButton extends Applet implements
ActionListener {
Button b;
TextField t;
public void init() {
t = new TextField(20);
add(t);
b = new Button("Go to this URL");
add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == b) {
try {
getAppletContext().showDocument(new URL(t.getText()));
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。