我想从另一个小程序调用(只显示另一个小程序)一个小程序。我只是在我的第一个applet上放置了一个按钮,并在它的actionperformed方法上使用了getcontextapplet()方法。但是没有显示第二个小程序。
我如何在first的任何反应中显示第二个applet?
代码:
import java.io.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class home extends Applet implements ActionListener
{
Container c1;
Label l1,l2,l3,l4;
TextField t1;
Button b1,b2;
ImageIcon icon;
Panel p1;
URL order;
public void init()
{
// Tell the applet not to use a layout manager.
setLayout(null);
l1=new Label("MINDSOFT CONSULTANTS");
Font fg=new Font("Times new roman",Font.BOLD,50);
add(l1);
l1.setFont(fg);
l1.setBounds(20,20,800,70);
l2=new Label("Strength of 5000 employees");
fg=new Font("Times new roman",Font.BOLD,25);
l2.setFont(fg);
l2.setBounds(180,120,500,30);
add(l2);
l3=new Label("Specialised in IT and computing services");
l3.setFont(fg);
l3.setBounds(90,180,500,30);
add(l3);
l4=new Label("A total of 10 different departments");
l4.setFont(fg);
l4.setBounds(140,240,500,30);
add(l4);
b1=new Button("VIEW DETAIL");
b1.setBounds(150,320,150,40);
add(b1);
b1.addActionListener(this);
b2=new Button("ADD DETAIL");
b2.setBounds(450,320,150,40);
add(b2);
try
{
order =new URL("C:\Documents and Settings\Administrator\Desktop\try\add.html");
}
catch(MalformedURLException e){
System.out.println("HH");
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
getAppletContext().showDocument(order);
System.out.println("HI");
}
}
}
发布于 2009-10-19 14:39:24
如果在第57行仍然看到“非法转义字符”错误,这是由于实例化order
时传递的字符串文字造成的
order =new URL("C:\Documents and Settings\Administrator\Desktop\try\add.html");
Java语言的Escape Character是反斜杠(\
)。因此,每次使用反斜杠时,编译器都会认为您试图转义它后面的字符。例如,在字符串中
C:\Documents
...the编译器将\D
视为单个转义字符,而不是两个字符。您看到的编译器错误告诉您,它无法识别该字符串中的一些转义字符(\D
、\A
、\t
)。
解决方案是对转义字符进行转义,例如在每个反斜杠前面加上一个黑斜杠:
order =new URL("C:\\Documents and Settings\\Administrator\Desktop\\try\\add.html");
这告诉编译器将反斜杠视为反斜杠,而不是转义字符。
发布于 2009-10-19 13:16:50
https://stackoverflow.com/questions/1591186
复制