如何从自身 运行 生成一个 JApplet?

How do I run a JApplet from itself?

基本上我需要为学校做这件事,我浏览了各种关于这个的帖子,每个人都只是说 "why'd you wanna do that?" 而没有回答。所以很多人需要这方面的帮助,总有一天你的回答会得到很多人的喜欢

所以这是我的 class - 我需要向 main 添加几行代码才能使这个 JApplet 弹出并将积木绘制成 JApplet window?

public class Wall extends JApplet {
ArrayList<Brick> bricks = new ArrayList<Brick>();
Color[] colors = {Color.decode("#1abc9c"), Color.decode("#f1c40f"), Color.decode("#d35400"), Color.decode("#e74c3c"), Color.decode("#2ecc71"), Color.decode("#3498db"), Color.decode("#9b59b6"), Color.decode("#34495e")};
ArrayList<Integer> usedInts = new ArrayList<Integer>();

public void makeBricks(){
    int xPos = 20;
    int yPos = 50;
    int height = 50;
    int width = 60;
    for(int i=0; i<8;i++){
        Brick b = new Brick();
        b.setxPosition(xPos);
        xPos =+60;
        b.setyPosition(yPos);
        if (xPos == 200){
            yPos+=50;
        }
        b.setColor(randomColor());
        b.setHeight(height);
        b.setWidth(width);
        bricks.add(b);

    }
}
public Color randomColor(){
    Random r = new Random(System.currentTimeMillis());
    boolean allAssigned = false;
    while(!allAssigned){
        int newInt = r.nextInt(8);
        if(!usedInts.contains(newInt)){
            usedInts.add(newInt);
            return colors[newInt];
        }
        if(usedInts.size()>7){
            usedInts.clear();
        }
    }
    return Color.BLACK;
}

public void draw(Graphics g) {
    for(Brick b: bricks){
        b.draw(g);
    }

}
@Override
public void paint(Graphics g){
    draw(g);
}


public static void main(String[] args) {
//these lines do not work
Wall wall = new Wall();    
wall.makeBricks();
wall.draw();

}

}

JApplet 没有自己的 window,它们是由浏览器嵌入到网页中的。可以使用小程序查看器来显示它们,但您需要从命令行执行此操作

首先创建一个自定义 class 扩展自 JPanel,覆盖它 paintComponent 并在那里执行自定义绘制。有关详细信息,请参阅 Performing Custom Painting

在您的 main 方法中,创建一个新的 JFrame 并将您的 "game panel" 添加到其中...

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            }

            JFrame frame = new JFrame("Testing");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new GamePane());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
}

如果您必须有一个小程序,您现在也可以向其中添加 "game panel"

有关详细信息,请参阅 How to Make Frames (Main Windows) and Using Top-Level Containers

正如其他人所提到的,小程序通常没有独立的 window。另一方面,至少有 3 种方法可以将小程序包含在自由浮动 window:

  • 运行小程序查看器中的小程序1.1。应该认真考虑这一点,因为小程序查看器是为显示小程序而设计的。更好的是,它将在网页中重新创建小程序的大部分环境,包括创建小程序上下文(从中获取文档或代码库,或小程序参数)和安全沙箱。
  • 如果你的意思是'free floating for the end user'。使用 Java Web Start 启动小程序自由浮动。 JWS(再次)使用小程序查看器来显示小程序。
  • 一个混合application/applet1.2。这更像你描述的那样,一个带有 main(String[]) 的小程序,它为小程序创建包装器框架并调用 init() 方法等。这对于测试更简单的(无参数等)小程序很方便,其中安全沙箱只是阻碍。

我还有一个提供 Appleteer 的网站,类似于小程序查看器,除了它会在单个 HTML 文档中启动多个小程序,而小程序查看器会将它们拆分为单独的自由浮动 windows(和其他细微差别 - Appleteer 没有安全沙箱..)。不幸的是,我的网站免费托管停止了 运行 业务的网络托管方面!

  1. This answer to How to call a paint method inside Applet extended class?
    1. 更新 1 有一个在小程序查看器中启动小程序的示例。
    2. 更新 2 有一个创建混合 application/applet.
    3. 的示例