我制作了一个加载器小程序,它向用户打招呼,当用户单击该小程序上显示的按钮时,它会启动主小程序,加载程序小程序被销毁。
但是点击另一个小程序没有启动!
加载程序小程序:
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
public class Loader extends JApplet implements ActionListener{
Display secondApplet;
Button button;
@Override
public void init() {
setSize(800,600);
}
@Override
public void start() {
setLayout(new FlowLayout());
button = new Button ("Click me !!");
add(button);
button.addActionListener(this);
}
@Override
public void paint(Graphics g) {
}
@Override
public void actionPerformed(ActionEvent e) {
secondApplet = (Display)getAppletContext().getApplet("Display");
if (secondApplet != null) {
secondApplet.init();
secondApplet.start();
}
else {
System.out.println("Not Running\n");
}
}
}
显示小程序:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JApplet;
public class Display extends JApplet {
@Override
public void init() {
setSize(600,400);
}
@Override
public void paint(Graphics g) {
g.fillRect(0, 0, this.getWidth(), this.getHeight());
}
}
如何创建另一个 Applet 的实例并销毁当前 Applet !