java swing MenuItem乱码

用java开发一个带有托盘图标的程序, 
其它模块的中文显示都是正常的,比如: 
 
就只有托盘中点击小图标时弹出的菜单中的中文是方框(中文方块),如下: 
 

按照一般的经验,以为是字体没设置好,就用以下代码设置字体: 
Java代码  收藏代码
  1. Font f = new Font("u5faeu8f6fu96c5u9ed1", Font.PLAIN, 11);//宋体  
  2. UIManager.put("Label.font",f);  
  3.         UIManager.put("Label.foreground",Color.black);  
  4.         UIManager.put("Button.font",f);  
  5.         UIManager.put("Menu.font",f);  
  6.         UIManager.put("MenuItem.font",f);  
  7.         UIManager.put("List.font",f);  
  8.         UIManager.put("CheckBox.font",f);  
  9.         UIManager.put("RadioButton.font",f);  
  10.         UIManager.put("ComboBox.font",f);  
  11.         UIManager.put("TextArea.font",f);  
  12.         UIManager.put("EditorPane.font",f);  
  13.         UIManager.put("ScrollPane.font",f);  
  14.         UIManager.put("ToolTip.font",f);  
  15.         UIManager.put("TextField.font",f);  
  16.         UIManager.put("TableHeader.font",f);  
  17.         UIManager.put("Table.font",f);  

完了中文还是方块,那就用以下代码打印字体: 
Java代码  收藏代码
  1. GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();  
  2. String script[] = ge.getAvailableFontFamilyNames();  
  3. for(String s:script){  
  4. System.out.print(s+",");  
  5. }  

发现明明写着有宋体。这下郁闷了,试了一下午,还是没有搞定。 
终于在网上找到了救星(参考:http://scnjl.iteye.com/blog/1282172)。 
原来是myeclipse的运行参数问题,真想骂人啊我。。。 
下面说说解决: 
第一: 
在你的具有main函数的类也即你应用运行的主类上点击右键,选择Run As中的Run Configurations,如下图: 


第二,在Arguments标签下的VM arguments中添加下面这行参数代码,然后点击应用。 
      
      -Dfile.encoding=GB18030 


在次运行 

 
原文地址:https://www.cnblogs.com/hzcya1995/p/13317815.html