zoukankan      html  css  js  c++  java
  • java 菜单 中文 乱码问题解决方案(使用idea/eclipse) swing MenuItem乱码

     

    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 


    在次运行 

     
  • 相关阅读:
    LeetCode分类专题(五)——动态规划1-子序列
    LeetCode分类专题(四)——双指针和滑动窗口1
    LeetCode分类专题(三)——二分查找1
    消息队列(一)——Kafka概述
    Java多线程(五)——synchronized关键字原理
    Java多线程(四)——volatile关键字原理
    Redis(五)——主从复制、哨兵
    Redis(四)——过期、持久化、事件
    Redis(三)——底层数据结构
    MySQL知识点
  • 原文地址:https://www.cnblogs.com/duanweishi/p/8592928.html
Copyright © 2011-2022 走看看