簡體   English   中英

測試Java代碼是否在applet中運行的最佳方法是什么?

[英]what is the best way to test whether java code is running in an applet or not?

我想要在Java代碼中的某個地方:

if(inAnApplet()) {
    initForApplet();
} else {
    initForApp();
}
public class MyApplet extends JApplet {

    boolean isapplet = true;


    public MyApplet() {
        this(true);
    }

    public MyApplet(boolean isapplet) {
        this.isapplet = isapplet;
    }

    public static final void main(String[] argv) {
        // is an app
        new MyApplet(false);
    }
}

假設您在某個地方有一個擴展JApplet的視圖,並且可以從擁有if的代碼中訪問它,則可以編寫:

if(mainView instanceof JApplet) {
     initForApplet();
} else {
     initForApp();
}

就像jli所說的那樣,也許您可​​以在類要訪問的某個地方創建一個初始化的public (static) boolean isApplet = true ,並在您的main方法中將其設置為false,如public static void main(String[] args)在Applet中未調用。

這樣,您的支票就可以簡單地從if(isApplet)開始!

高溫超導

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM