尽管java applet不再流行,但让java程序以多种方式运行会更有意思。
DisplayLabel.html 代码
<!DOCTYPE HTML> <html> <head> <title>Java Applet Demo</title> </head> <body> <applet code="DisplayLabel.class" width=250 height=50> </applet> </body> </html>
DisplayLabel.java 代码
import javax.swing.*; public class DisplayLabel extends JApplet{ public DisplayLabel(){ add(new JLabel("Hello World!", JLabel.CENTER)); } public static void main(String[] args){ // Create a frame JFrame frame = new JFrame("Applet is in the frame"); // Create an instance of the applet DisplayLabel applet = new DisplayLabel(); // Add the applet to the frame frame.add(applet); // Display the frame frame.setSize(300,100); frame.setLocationRelativeTo(null); // Center the frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
时间: 2024-12-05 11:16:51