我正在尝试创建一个 GUI,我可以在其中根据 Applet 中的内容在 Applet 的“上方”绘制。为了测试这一点,我试图通过使用 JLayeredPane 创建一个带有填充背景的面板来填充一个简单的矩形,以显示在小程序上。我无法让它工作,但我尝试了它。
import java.applet.Applet;
import java.awt.Color;
import java.awt.GridLayout;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
public class GUI {
private static JFrame frame;
private static JLayeredPane layers;
private static JPanel panel;
private static Applet applet;
private static final String JAR_PATH = "file:C:/Applet.jar";
public static void main(String args[]) throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException{
ClassLoader clientClassLoader = new URLClassLoader(new URL[]{new URL(JAR_PATH)});
Class<?> client = clientClassLoader.loadClass("Loader");
frame = new JFrame("JLayeredPane Testing");
frame.setSize(400, 400);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout());
applet = (Applet) client.newInstance();
layers = new JLayeredPane();
panel = new JPanel();
panel.setSize(200, 200);
panel.setOpaque(true);
panel.setBackground(Color.BLACK);
layers.add(panel, JLayeredPane.PALETTE_LAYER);
layers.add(applet, JLayeredPane.DRAG_LAYER);
frame.setContentPane(layers);
frame.setVisible(true);
applet.init();
}
}
当我运行我的程序时,小程序不显示或初始化。