[英]call another applet in an applet
我想從另一個小程序調用(只顯示另一個小程序)一個小程序。 我剛剛在我的第一個小程序上放置了一個按鈕,並且在它的 actionperformed 方法上使用了 getcontextapplet() 方法。 但是沒有顯示第二個小程序。
如何在第一個反應時顯示第二個小程序...
代碼:
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");
}
}
}
如果您仍然在第 57 行看到“非法轉義字符”錯誤,則歸結為您在實例化order
時傳遞的字符串文字:
order =new URL("C:\Documents and Settings\Administrator\Desktop\try\add.html");
Java轉義字符是反斜杠 ( \\
)。 因此,每次使用反斜杠時,編譯器都會認為您試圖轉義它后面的字符。 例如,在字符串中
C:\Documents
...編譯器將\\D
視為單個轉義字符而不是兩個字符。 您看到的編譯器錯誤告訴您它無法識別該字符串中的某些轉義字符( \\D
、 \\A
、 \\t
)。
解決方案是轉義轉義字符,例如在每個反斜杠前加一個黑斜杠:
order =new URL("C:\\Documents and Settings\\Administrator\Desktop\\try\\add.html");
這告訴編譯器將反斜杠視為反斜杠,而不是轉義字符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.