[英]Java components are invisible when swing app is run as APPLET
Respected all, I am making a Swing-application window in ECLIPSE. 尊敬的所有人,我正在ECLIPSE中创建一个Swing应用程序窗口。 When I run the program as 'JAVA-Application' the program functions well.
当我以“ JAVA应用程序”运行程序时,该程序运行良好。 However when I try to run the program as "Java_applet" the components like 'command button', 'textbox' are invisible.
但是,当我尝试以“ Java_applet”形式运行程序时,“命令按钮”,“文本框”等组件是不可见的。
I am entirely new to Java. 我是Java的新手。 I had previously worked on C#.
我以前从事过C#。 Kindly please help me.
请帮助我。
import java.awt.EventQueue;
import java.applet.*;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import java.awt.BorderLayout;
public class sa extends Applet{
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
sa window = new sa();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public sa() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
frame.getContentPane().add(rdbtnNewRadioButton, BorderLayout.CENTER);
}
}
You can't just have your class extend Applet and expect that that is all that is necessary for it to behave like a proper Applet. 您不能仅仅让您的类扩展Applet并期望这足以使其表现得像一个正确的Applet。 You need to give the class a proper init method and build the GUI from within this method.
您需要为该类提供适当的init方法,并从该方法中构建GUI。 But most importantly you will need to read an Applet tutorial, any decent tutorial should do.
但是最重要的是,您需要阅读Applet教程,任何体面的教程都应该这样做。 Myself, I'd have my GUI extend JPanel, build the GUI in its constructor, and then I could use this JPanel in a JApplet or JFrame as needed.
我自己,将GUI扩展JPanel,在其构造函数中构建GUI,然后可以根据需要在JApplet或JFrame中使用此JPanel。
As Andrew aptly notes in comment, 正如安德鲁在评论中恰当地指出的那样,
I think the OP should also dump the entire 'applet' part of it and develop the
JFrame
with an idea to launching it from a link using Java Web Start .我认为OP还应该转储它的整个“小程序”部分,并开发
JFrame
,并提出使用Java Web Start从链接中启动它的想法。 At least then it would have a better chance for working for users of Chrome and FireFox (which are both planning to remove all support for applets).至少到那时,它将有一个更好的机会为Chrome和FireFox(计划都删除对applet的所有支持 )的用户工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.