Download java applet inside java application

Can I display a web applet inside a Java application window?

It would be preferable if you could give a code example, since I'm pretty new to java.

+4
source share
2 answers

Take a look at Javadocs:

extended by java.awt.Container extended by java.awt.Panel extended by java.applet.Applet 

An applet is a panel, so you can add them to the frame. However, their initialization and invocation in Frame (or JFrame) are different. Here, by the way. JApplet version:

 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 

If the applet code is yours, I would put all the interesting content in a JPanel and either use that JPanel in a JApplet or put it in a JFrame and use it as an application.

If this is not your (J) applet and you have no code, I would try adding them to the (J) panel in the (J) frame.

+4
source

If you really want to do this, you can simply add the applet to the frame, because the Applet is a subclass of 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 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); 

Please note that this is bad practice and should only be used if you cannot access and modify the source code of the applets! You may run into problems, and I cannot guarantee that this will work.

It’s best to create a main panel containing the entire view and add it to the applet or frame, depending on the type of client (web or offline).

0
source

Source: https://habr.com/ru/post/1379037/


All Articles