[英]Load java applet inside java application
是否可以在Java應用程序窗口中顯示Web小程序?
如果你能提供一個示例代碼,那將是更好的選擇,因為我對java很新。
看看Javadocs:
extended by java.awt.Container
extended by java.awt.Panel
extended by java.applet.Applet
小程序是面板,因此您應該能夠將它們添加到框架中。 但是,它們在Frame(或JFrame)中的初始化和調用是不同的。 這是,順便說一下。 JApplet版本:
java.lang.Object
extended by java.awt.Component
extended by java.awt.Container
extended by java.awt.Panel
extended by java.applet.Applet
extended by javax.swing.JApplet
如果Applet的代碼是您的,我會將所有有趣的內容放入JPanel,並在JApplet中使用此JPanel,或將其放入JFrame,並將其用作應用程序。
如果它不是你的(J)Applet,並且你沒有代碼,我會測試將它們添加到(J)框架中的(J)面板。
如果你真的想這樣做,你可以簡單地將applet添加到一個框架,因為Applet是Component的子類。
JFrame frame = new JFrame();
// Create your applet to add to the frame
Applet comp = new YourApplet() {{
// this calls the method that initializes the applet. usually the browser calls it
init();
}};
// Add the component to the frame's content pane;
// by default, the content pane has a border layout
frame.getContentPane().add(comp, BorderLayout.CENTER);
// Show the frame
frame.pack();
frame.setVisible(true);
請注意,這是一種不好的做法,只有在您無法訪問和更改applet源代碼時才能使用它! 你可能會遇到問題,我不能保證這會有效。
更好的方法是創建一個包含整個視圖的主面板,並將其添加到applet或框架中,具體取決於客戶端的類型(Web或獨立)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.