java tooltip显示_Java开发网 - tooltip问题,请高手支招

我来支一招。

以下demo在JB8中已运行通过。功能:当在JTextField中输入是tool时显示ToolTip,内容为JTextField 中内容。若输入tool后继续输入或者内容不是tool,则立即不显示Tooltip。

说明:

我个人感到这个需求是一个“病”需求,(因为不用ToolTip,用其它方案会更好,不知为什么一定要紧抓ToolTip来做。)故这个demo虽能达到功能,却也是一个“病”方案。程序也就不优化了。抛砖引玉吧。(用JB8写的,有多余代码,也就不删了,一笑了知吧。)

package untitled2;

import javax.swing.UIManager;

import java.awt.*;

public class Application1 {

public Application1() {

Frame1 frame = new Frame1();

//Center the window

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

Dimension frameSize = frame.getSize();

if (frameSize.height > screenSize.height) {

frameSize.height = screenSize.height;

}

if (frameSize.width > screenSize.width) {

frameSize.width = screenSize.width;

}

frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);

frame.setVisible(true);

}

//Main method

public static void main(String[] args) {

new Application1();

}

}

package untitled2;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.text.*;

public class Frame1 extends JFrame {

JPanel contentPane;

JTextField jTextField1 = new JTextField();

BorderLayout borderLayout1 = new BorderLayout();

//Construct the frame

public Frame1() {

enableEvents(AWTEvent.WINDOW_EVENT_MASK);

try {

jbInit();

}

catch(Exception e) {

e.printStackTrace();

}

}

//Component initialization

private void jbInit() throws Exception {

contentPane = (JPanel) this.getContentPane();

contentPane.setLayout(borderLayout1);

this.setSize(new Dimension(400, 300));

this.setTitle("Frame Title");

jTextField1.setToolTipText("NO TOOLTIPS");

jTextField1.setSelectionStart(0);

jTextField1.setText("");

jTextField1.setDocument(new mydt(jTextField1)) ;

contentPane.add(jTextField1, BorderLayout.NORTH);

}

//Overridden so we can exit when window is closed

protected void processWindowEvent(WindowEvent e) {

super.processWindowEvent

envelope_smile.gif;

if (e.getID() == WindowEvent.WINDOW_CLOSING) {

System.exit(0);

}

}

}

class mydt extends PlainDocument

{

private JTextComponent tc;

private String cc;

private int flag=0;

public mydt (JTextComponent c){tc=c;}

public void insertString(int offs, String str, AttributeSet a) throws javax.swing.text.BadLocationException {

/**@todo Override this javax.swing.text.PlainDocument method*/

super.insertString(offs, str, a);

cc=this.getText(0,this.getLength() );

ToolTipManager.sharedInstance().setDismissDelay(1000) ;

ToolTipManager.sharedInstance().setInitialDelay(10) ;

if(cc.compareTo("tool")!=0 )

{

try{

Robot rt=new Robot();

//rt.mousePress(InputEvent.BUTTON3_MASK ) ;

if(flag==1){ flag=0;

rt.mouseMove(400,400) ;

// rt.mousePress(InputEvent.BUTTON1_MASK ) ;

}

}catch(Exception e1){System.out .print(e1) ;}

}

else {

tc.setToolTipText(cc) ;

flag=1;

try{

Robot rt=new Robot();

rt.mouseMove(tc.getLocationOnScreen() .x+12,

tc.getLocationOnScreen() .y+4) ;

// rt.mousePress(InputEvent.BUTTON1_MASK ) ;

}catch(Exception e1){System.out .print(e1) ;}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值