`
tntxia
  • 浏览: 1485986 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

看Swing杂记

阅读更多

这几天,闲时看了一下Swing。

Swing常用的两个类,一个是JFrame窗体类,一个是JApplet小应用程序类。其中JApplet依赖浏览器来执行。很多浏览器都可以支持JApplet,包括了IE,Firefox等。不过如何用IE6来执行的话,IE6会有脚本警告。

 

JAppletJFrame都是只包含一个组件的容器,这个组件是JRootPane的一个实例。所以JApplet和JFrame之间是互相通用的。如以下代码:

java 代码
  1. package com.tntxia.test.swing.jApplet;   
  2.   
  3. import java.awt.BorderLayout;   
  4. import java.awt.Container;   
  5. import java.awt.FlowLayout;   
  6.   
  7. import javax.swing.ImageIcon;   
  8. import javax.swing.JApplet;   
  9. import javax.swing.JFrame;   
  10. import javax.swing.JLabel;   
  11. import javax.swing.SwingConstants;   
  12.   
  13. public class HelloWorld extends JApplet {   
  14.   
  15.     /**  
  16.      *   
  17.      */  
  18.     private static final long serialVersionUID = 52493594634447013L;   
  19.   
  20.     public void init() {   
  21.         Container contentPane = getContentPane();   
  22.            
  23. //      原文如不修改,不能正常显示图标    
  24.     //修改后--------   
  25.     java.net.URL codebase=getClass().getResource("loli.jpg");   
  26.     JLabel label = new JLabel(new ImageIcon(codebase));   
  27.     //----------修改后的程序有个毛病,既作为applet使用时,不能刷新,一旦刷新图就没了。   
  28. contentPane.setLayout(new FlowLayout());    
  29.     contentPane.add(label);   
  30.   
  31.     }   
  32.        
  33.     public static void main(String[] args){   
  34.         JFrame f = new JFrame();   
  35.         JApplet applet = new HelloWorld();   
  36.         applet.init();   
  37.         f.setContentPane(applet.getContentPane());   
  38.         f.setBounds(100,100,400,400);   
  39.         f.setVisible(true);   
  40.            
  41.     }   
  42.   
  43. }  

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics