控件提示信息 setToolTipText(Java component )换行显示的详细实现(源代码)

 

  根据项目的需要,特点学习研究了下Java component  setToolTipText多行显示的问题,最后找到方法是

HTML格式控制的方法, 一语中的就是用了HTML格式控制符号"<br>"特写此文,供大家交流学习详细测试实现方法件下

 

  一:NEW一个HTML格式控制类,代码如下:

//START
package Com;

public class HtmlMultiLineControl {
   
    private String strDest;
    //上文
    private final String strHtmlBegin =
           "<html>"
              + "<bgcolor color=#000080>"
                  +"<font color=#0000cd >"
                  +"<b>";
    //下文
    private final String strHtmlEnd =
                  "</b>"
                 +"</font>"
             +"</bgcolor>"
          +"</html>";
   
    public String CovertDestionString(String strDest){
       
        this.strDest = strHtmlBegin + strDest + strHtmlEnd;
        return this.strDest;
    }
}
 
//END

 

 二:New一个测试界面类,代码如下:


//START
package GUI;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import Com.HtmlMultiLineControl;

public class ToolTipDemo extends JFrame {

    private JLabel lblTest;
    private JTextField textTest;
    private JButton btnTest;
    private String[] tipsArray = {"第一行","第二行","第三行"};
    
    /**构造方法*/
    public ToolTipDemo(){
       
        CreateContence();
    }
    
    /*创建控件*/
    public void CreateContence(){
       
        String strDestTips = new String();
       
        setSize(450, 300);
        this.setLocation(this.getToolkit().getScreenSize().width / 2- this.getWidth() / 2,
                         this.getToolkit().getScreenSize().height/ 2 - this.getHeight() / 2);
       
        this.setLayout(null);
       
        lblTest = new JLabel("This is a ToolTipsText Wrap Test Label");
        lblTest.setBounds(new Rectangle(100,50, 260,30));
        lblTest.setToolTipText(getTips());//最终要显示提示信息
        getContentPane().add(lblTest);
       
    
        textTest = new JTextField("This is a ToolTipsText Wrap Test Text");
        textTest.setBounds(new Rectangle(100,100, 260,30));
        textTest.setToolTipText(getTips());
        getContentPane().add(textTest);
       
        btnTest = new JButton("This is a ToolTipsText Wrap Test Button");
        btnTest.setBounds(new Rectangle(100,150, 260,30));
        btnTest.setToolTipText(getTips());
        getContentPane().add(btnTest);
       
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
    }
    
    /*获取提示信息*/
    public String getTips(){
       
        HtmlMultiLineControl hmlc = new HtmlMultiLineControl();
        String strtemp = new String();
       
        for(int i=0; i<tipsArray.length; i++){
           
            if(i!= 0){
                strtemp = strtemp + "<br>" ;//br:换行(HTML)
            }
           
            strtemp = strtemp + tipsArray[i];
        }
       
        //添加HTML格式控制
        strtemp = hmlc.CovertDestionString(strtemp);
       
        return strtemp;
    }
    public static void main(String[] args) {
       
       
        ToolTipDemo tooltip = new ToolTipDemo();
    }

}
//End

 

三:运行结果

 

鼠标Over控件按上面效果


 

  效果明显。我只截了在Button上面的效果,其它LABEl,TEXT上面的效果请有兴趣者研究。

四:小节

  当当然我这里只是要改变字体相关的效果,所以用HTML格式控制可以达到要求,

  只要有的朋友需要更多的要求,建议可以研究下重写setToolTipText().

五:本文结束。


 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值