Swing


          天行健 君子以自強(qiáng)不息

          posts - 69, comments - 215, trackbacks - 0, articles - 16
             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          控制Swing中ToolTip的顯示與隱藏

          Posted on 2008-05-22 22:42 zht 閱讀(4940) 評論(4)  編輯  收藏 所屬分類: Swing

           

          先給出兩個控制方法
          1、顯示tooltip
          public static void postToolTip(JComponent comp) {
            Action action = comp.getActionMap().get("postTip");
            if (action == null) // no tooltip
             return;
            ActionEvent ae = new ActionEvent(comp, ActionEvent.ACTION_PERFORMED, "postTip", EventQueue.getMostRecentEventTime(), 0);
            action.actionPerformed(ae);
           }

          2、隱藏tooltip
           public static void hideToolTip(JComponent comp) {
            Action action = comp.getActionMap().get("hideTip");
            if (action == null) // no tooltip
             return;
            ActionEvent ae = new ActionEvent(comp, ActionEvent.ACTION_PERFORMED, "hideTip", EventQueue.getMostRecentEventTime(), 0);
            action.actionPerformed(ae);
           }

          tooltip的顯示與隱藏都是通過ToolTipManager這個類來控制的
          當(dāng)componet.setToolTipText的時候java的內(nèi)部處理為
          public void setToolTipText(String text) {
                  String oldText = getToolTipText();
                  putClientProperty(TOOL_TIP_TEXT_KEY, text);
                  ToolTipManager toolTipManager = ToolTipManager.sharedInstance();
                  if (text != null) {
               if (oldText == null) {
                          toolTipManager.registerComponent(this);
               }
                  } else {
                      toolTipManager.unregisterComponent(this);
                  }
              }

          而在ToolTipManager 的registerComponent方法中將兩個Action添加到了組件的actionMap 中,
          具體的acton實現(xiàn)可以參考ToolTipManager 。

               ActionMap actionMap = component.getActionMap();
               if (inputMap != null && actionMap != null) {
                 actionMap.put("postTip", postTipAction);
                 actionMap.put("hideTip", hideTipAction);


          上述兩個方法就是取得這兩個Action,然后調(diào)用相應(yīng)的Action方法來控制tooltip的顯示與隱藏。

          對于tooltip的顯示時間可以通過ToolTipManager中的set**的方法進(jìn)行設(shè)置。


          以下是測試代碼

              public static void main(String[] args) {
                  
          final JPanel panel = new JPanel();
                  
          final JButton btn = new JButton("button");
                  btn.setToolTipText(
          "button_tooltip");
                  panel.add(btn);
                  
          final JButton btn1 = new JButton("button1");
                  panel.add(btn1);
                  btn1.setToolTipText(
          "button1_tooltip");
                  btn.addActionListener(
          new ActionListener() {
                      
          public void actionPerformed(ActionEvent e) {
                          postToolTip(btn1);

                      }
                  });
                  btn1.addActionListener(
          new ActionListener() {
                      
          public void actionPerformed(ActionEvent e) {
                          postToolTip(btn);
                      }
                  });
           JFrame frame = new JFrame();
            frame.setContentPane(panel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(500, 400);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
              }



          注:以上有些內(nèi)容引用自網(wǎng)絡(luò),我加了一些個人理解。

          下一篇將介紹一下對tooltip顯示的控制,ex)換行,顏色以及其它屬性

          主站蜘蛛池模板: 板桥市| 安平县| 广灵县| 临颍县| 石首市| 民乐县| 新兴县| 荃湾区| 德惠市| 石泉县| 和政县| 武安市| 青阳县| 鹰潭市| 文山县| 唐河县| 诸城市| 平阳县| 盱眙县| 富阳市| 中宁县| 琼海市| 三亚市| 柘荣县| 黔东| 永平县| 十堰市| 花莲县| 海南省| 迁安市| 鹿泉市| 元阳县| 绥芬河市| 永宁县| 获嘉县| 镇雄县| 海兴县| 钟祥市| 马尔康县| 清水县| 玉田县|