转载

Swing使用Substance外观包异常问题

问题一: 今天更新我的Java版QQ,在网上找到了Substance外观包,效果不错,直接用了,可是设置水印问题时就出现问题,网上有现成的例子

  1. JFrame.setDefaultLookAndFeelDecorated(true);  
  2. JDialog.setDefaultLookAndFeelDecorated(true);  
  3. try  {  
  4.             SubstanceImageWatermark watermark  =   new  SubstanceImageWatermark(LoginFrame. class .getResourceAsStream( " /demo/001.jpg " ));  
  5.             watermark.setKind(ImageWatermarkKind.SCREEN_CENTER_SCALE);  
  6.             SubstanceSkin skin  =   new  OfficeBlue2007Skin().withWatermark(watermark);   //初始化有水印的皮肤  
  7.  
  8.             UIManager.setLookAndFeel( new  SubstanceOfficeBlue2007LookAndFeel());  
  9.             SubstanceLookAndFeel.setSkin(skin);  //设置皮肤  
  10.              
  11.         }  catch  (UnsupportedLookAndFeelException ex) {  
  12.             Logger.getLogger(LoginFrame. class .getName()).log(Level.SEVERE,  null , ex);  
  13.         } 

可是在我导入的包里死活包withWatermark方法不存在,于是就Google,最后看到一哥们说,自从subtance6.0以后就去掉了 withWatermark设置水印方法,原因貌似是不安全,可是想用水印的效果呢?那你就换成substance5.0版本吧!换了之后果然能用了效果图:北京比较淡,水印不明显。

Swing使用Substance外观包异常问题

问题二:使用subtance出现异常UIDefaults.getUI() failed:org.jvnet.substance.api.UiThreadingViolationException,有一前辈说是包不能识别啥的,就我遇到的问题,只是换了皮肤之后框架布局变了,不在于能不能用的问题,关键还是在代码上,我遇到的这个问题就在于我的代码设置问题,先把我的部分代码贴出来分析一下:

  1. package com.client.frm;  
  2.  
  3. import java.awt.AWTException;  
  4. import java.awt.Color;  
  5. import java.awt.Component;  
  6. import java.awt.Dimension;  
  7. import java.awt.FlowLayout;  
  8. import java.awt.Image;  
  9. import java.awt.Insets;  
  10. import java.awt.MenuItem;  
  11. import java.awt.PopupMenu;  
  12. import java.awt.SystemTray;  
  13. import java.awt.TrayIcon;  
  14. import java.awt.event.ActionEvent;  
  15. import java.awt.event.ActionListener;  
  16. import java.awt.event.MouseAdapter;  
  17. import java.awt.event.MouseEvent;  
  18. import java.awt.event.WindowAdapter;  
  19. import java.awt.event.WindowEvent;  
  20. import java.io.IOException;  
  21. import java.io.ObjectInputStream;  
  22. import java.io.ObjectOutputStream;  
  23. import java.util.HashMap;  
  24. import java.util.List;  
  25. import java.util.Vector;  
  26. import java.util.logging.Level;  
  27. import java.util.logging.Logger;  
  28.  
  29.  
  30. import javax.swing.DefaultListCellRenderer;  
  31. import javax.swing.DefaultListModel;  
  32. import javax.swing.ImageIcon;  
  33. import javax.swing.JButton;  
  34. import javax.swing.JComboBox;  
  35. import javax.swing.JDialog;  
  36. import javax.swing.JFrame;  
  37. import javax.swing.JLabel;  
  38. import javax.swing.JList;  
  39. import javax.swing.JMenuItem;  
  40. import javax.swing.JOptionPane;  
  41. import javax.swing.JPanel;  
  42. import javax.swing.JPopupMenu;  
  43. import javax.swing.JScrollPane;  
  44. import javax.swing.JTextArea;  
  45. import javax.swing.SwingConstants;  
  46. import javax.swing.SwingUtilities;  
  47. import javax.swing.UIManager;  
  48. import javax.swing.UnsupportedLookAndFeelException;  
  49. import javax.swing.border.LineBorder;  
  50.  
  51. import org.jvnet.substance.SubstanceLookAndFeel;  
  52. import org.jvnet.substance.api.SubstanceSkin;  
  53. import org.jvnet.substance.api.SubstanceConstants.ImageWatermarkKind;  
  54. import org.jvnet.substance.skin.OfficeBlue2007Skin;  
  55. import org.jvnet.substance.skin.SubstanceOfficeBlue2007LookAndFeel;  
  56. import org.jvnet.substance.watermark.SubstanceImageWatermark;  
  57.  
  58. import sun.audio.AudioPlayer;  
  59. import sun.audio.AudioStream;  
  60.  
  61. import tools.PicturePane;  
  62.  
  63. import data.ChatMessage;  
  64. import data.FriendUser;  
  65. import data.Portrait;  
  66. import data.QQMessage;  
  67. import data.UserState;  
  68.  
  69.  
  70. public class UserMainView extends JFrame implements ActionListener {  
  71.       
  72.     /**  
  73.      * @author Mackalli  
  74.  × 2011/10/3  
  75.      */ 
  76.     private static final long serialVersionUID = 1L;  
  77.     private JButton btnPhoto;  
  78.     private JComboBox jcbState;  
  79.     private JLabel lblNickName;  
  80.     private JTextArea areaSignature;  
  81.  
  82.     private JLabel lblLoginBar = new JLabel();  
  83.     private JButton btnLoginCancel;  
  84.     private JPanel fillWidth = new FillWidth(300200,false);  
  85.  
  86.     private JButton btnZone;  
  87.     private JButton btnEmail;  
  88.     private JButton btnNews;  
  89.     private JButton btnShop;  
  90.     private JButton btnSkin;  
  91.  
  92.     //Color bgColor = new Color(211, 234, 224);  
  93.  
  94.     private JList listFriend;  
  95.     private DefaultListModel listModel = null;  
  96.  
  97.     // Integer存入的类型(K-此映射所维护的键的类型),ChatView对应映射(V - 所映射值的类型)  
  98.     private HashMap<Integer, ChatView> chat;  
  99.     FriendUser friendUser = null;  
  100.     private FriendUser selfUser = null;  
  101.     private Vector<FriendUser> v;  
  102.  
  103.     private ObjectInputStream ois;  
  104.     private ObjectOutputStream oos;  
  105.  
  106.     String QQ_NickName;  
  107.     String QQ_Num;  
  108.     Integer photo;  
  109.     Integer QQ_State;  
  110.     String signature;  
  111.  
  112.     JPopupMenu popupMenu;  
  113.     JMenuItem itemChat;  
  114.     JMenuItem itemDelete;  
  115.     JMenuItem itemFriendInfo;  
  116.       
  117.     private JButton btnFind;  
  118.     private JButton btnHelp;  
  119.       
  120.     //private SystemTray systemTray=null;  
  121.     private SystemTray tray=null;   
  122.     static TrayIcon trayIcon=null;    // 静态托盘图标,收到信息闪动提醒  
  123.     private Image trayImage=null;  
  124.       
  125.     private PopupMenu popupTray=new PopupMenu();  //托盘列表  
  126.     private MenuItem itemExit=new MenuItem("退出");  
  127.     private MenuItem itemSetSound=new MenuItem("关闭声音");  
  128.       
  129.       
  130.     private PicturePane bgPane=new PicturePane();  
  131.       
  132.     private static SearchView searchView;  
  133.     private TrayThread trayThread=null;  
  134.       
  135.     private static boolean isAvailable=true;  //设置声音默认开启,在托盘中设置,就两种形式,故用static  
  136.     //ChatView isVisible  
  137.     private boolean isShow=false;  
  138.     private boolean isOpenMsg=false;  
  139.     //if message is new  
  140.     private boolean isNewMessage=false;  
  141.       
  142.     private ChatView chatView;  
  143.       
  144.     public UserMainView(ObjectOutputStream oos, ObjectInputStream ois,  
  145.             Vector<FriendUser> v) {  
  146.         JFrame.setDefaultLookAndFeelDecorated(true);  
  147.         JDialog.setDefaultLookAndFeelDecorated(true);  
  148.         try {  
  149.             SubstanceImageWatermark watermark = new SubstanceImageWatermark(  
  150.                     UserMainView.class 
  151.                             .getResourceAsStream("/client/images/backImages/2.jpg "));  
  152.             watermark.setKind(ImageWatermarkKind.SCREEN_CENTER_SCALE);  
  153.             SubstanceSkin skin = new OfficeBlue2007Skin()  
  154.                     .withWatermark(watermark); // 初始化有水印的皮肤  
  155.  
  156.             UIManager.setLookAndFeel(new SubstanceOfficeBlue2007LookAndFeel());  
  157.             SubstanceLookAndFeel.setSkin(skin); // 设置皮肤  
  158.  
  159.         } catch (UnsupportedLookAndFeelException ex) {  
  160.             Logger.getLogger(UserMainView.class.getName()).log(Level.SEVERE,  
  161.                     null, ex);  
  162.         }  
  163.  
  164.         this.oos = oos;  
  165.         this.ois = ois;  
  166.         this.v = v;  
  167.  
  168.         setTitle("QQ_li");  
  169.         setSize(280630);  
  170.         setResizable(false);  
  171.         // getContentPane().setBackground(bgColor);  
  172.         // this.setContentPane(bgPane);  
  173.         setLocation(92060);  
  174.  
  175.         this.addWindowListener(new WindowAdapter() {  
  176.             public void windowIconified(WindowEvent e) {  
  177.                 setVisible(false);  
  178.             }  
  179.         });  
  180.         addTray();  
  181.  
  182.         initMainView(v);  
  183.         setVisible(true);  
  184.  
  185.         this.addWindowListener(new WindowAdapter() {  
  186.             public void windowClosing(WindowEvent e) {  
  187.                 QQMessage message = new QQMessage();  
  188.                 message.setType(77);  
  189.                 message.setObj(selfUser);  
  190.                 new WriteThread(message).start();  
  191.                 System.out.println(selfUser.getNickName() + "退出了");  
  192.                 chat = null;  
  193.                 UserMainView.this.dispose();  
  194.                 System.exit(0);  
  195.             }  
  196.         });  
  197.     } 

这是一个类,时刻要记得UIManager设置前提,放在构造方法的最前面,调用时要用到Swing工作线程 SwingUtilities.invokeLater()来调用,我在LoginView中调用的方法:

  1. //userMainView = new UserMainView(oos, ois, v); // 加载用户信<br> SwingUtilities.invokeLater(new Runnable() {<br> public void run() {   
  2.     userMainView = new UserMainView(oos, ois, v); // 加载用户信息  
  3.  userMainView.setVisible(true);   
  4.      }   
  5.      });   
  6.     LoginView.this.setVisible(false);<br>} 

当你出现上述异常时是不是直接

  1. userMainView = new UserMainView(oos, ois, v); // 加载用户信息 

没有少量一句,SwingUtilities.invokeLater(new Runnable()}这是Swing工作形式,我的最后出来结果了,和以前系统默认的就是大小比例的不同,不再抛出异常,我认为是代码的问题,这是今天的收获写出来大家分享一下,希望对一些困惑的朋友有所帮助,我们这一行很多问题就要自己解决,太多的差异网上的东西只能是参考!Good luck,friends!最后感谢Java.net的精英们制作出来这么漂亮的Swing外观来! GNU-UP UP!!!!!!对你有所帮助就收藏一下,大家一起分享程序的快乐!

正文到此结束
Loading...