java jtextfield 提示_带提示的JTextField

1 /**2 * @(#)MyJTextField.java  0.1.1  2007-9-83 */4 packageruislan;5 6 importjava.awt.BorderLayout;7 importjava.awt.Color;8 importjava.awt.Component;9 importjava.awt.Container;10 importjava.awt.Font;11 importjava.awt.Point;12 importjava.awt.event.ComponentAdapter;13 importjava.awt.event.ComponentEvent;14 importjava.awt.event.KeyAdapter;15 importjava.awt.event.KeyEvent;16 17 importjavax.swing.Icon;18 importjavax.swing.ImageIcon;19 importjavax.swing.JDialog;20 importjavax.swing.JFrame;21 importjavax.swing.JLabel;22 importjavax.swing.JLayeredPane;23 importjavax.swing.JPanel;24 importjavax.swing.JTextField;25 importjavax.swing.border.EmptyBorder;26 27 importnet.java.balloontip.BalloonBorder;28 29 30 /**31 * Custom JTextField.32 *33 *@version0.1.1, 2007-9-934 *@authorruislan 35 */36 publicclassMyJTextFieldextendsJTextField {37 privatestaticfinalColor TIP_COLOR=newColor(255,255,225);38 privateintlimit=Integer.MAX_VALUE;39 privatebooleannumberOnly;40 privateCoolToolTip numberTip;41 privateCoolToolTip limitTip;42 privateImageIcon tipIcon;43 44 publicMyJTextField() {45 initComponents();46 initEventListeners();47 }48 49 privatevoidinitComponents() {50 tipIcon=newImageIcon(MyJTextField.class.getResource("tip.gif"));51 52 numberTip=newCoolToolTip(this, TIP_COLOR, getColumns(),10);53 numberTip.setText("只能输入数字!");54 numberTip.setIcon(tipIcon);55 numberTip.setIconTextGap(10);56 57 limitTip=newCoolToolTip(this, TIP_COLOR, getColumns(),10);58 limitTip.setIcon(tipIcon);59 limitTip.setIconTextGap(10);60 }61 62 privatevoidinitEventListeners() {63 addKeyListener(newKeyAdapter() {64 @Override65 publicvoidkeyTyped(KeyEvent e) {66 if(getText().length()+1>limit) {67 deleteInputChar(e);68 limitTip.setVisible(true);69 return;70 }else{71 limitTip.setVisible(false);72 }73 if(numberOnly) {74 charinput=e.getKeyChar();75 if(!Character.isDigit(input)) {76 numberTip.setVisible(true);77 deleteInputChar(e);78 }else{79 numberTip.setVisible(false);80 }81 }82 }83 84 privatevoiddeleteInputChar(KeyEvent source) {85 source.setKeyChar((char) KeyEvent.VK_CLEAR);86 }87 });88 }89 90 publicvoidsetMaxTextLength(intlimit) {91 if(limit<0) {92 return;93 }94 this.limit=limit;95 limitTip.setText(String.format("超过最大长度 \"%d\"", limit));96 }97 98 publicintgetMaxTextLength() {99 returnlimit;100 }101 102 publicvoidsetNumberOnly(booleannumberOnly) {103 this.numberOnly=numberOnly;104 }105 106 publicbooleanisNumberOnly() {107 returnthis.numberOnly;108 }109 110 privateclassCoolToolTipextendsJPanel {111 privateJLabel label=newJLabel();112 privatebooleanhaveShowPlace;113 114 privateComponent attachedComponent;115 116 publicCoolToolTip(Component attachedComponent, Color fillColor,117 intborderWidth,intoffset) {118 this.attachedComponent=attachedComponent;119 120 label.setBorder(newEmptyBorder(borderWidth, borderWidth,121 borderWidth, borderWidth));122 label.setBackground(fillColor);123 label.setOpaque(true);124 label.setFont(newFont("system",0,12));125 126 setOpaque(false);127 this.setBorder(newBalloonBorder(fillColor, offset));128 this.setLayout(newBorderLayout());129 add(label);130 131 setVisible(false);132 133 //if the attached component is moved while the balloon tip is134 //visible, we need to move as well135 attachedComponent.addComponentListener(newComponentAdapter() {136 publicvoidcomponentMoved(ComponentEvent e) {137 if(isShowing()) {138 determineAndSetLocation();139 }140 }141 });142 143 }144 145 privatevoiddetermineAndSetLocation() {146 Point location=attachedComponent.getLocation();147 setBounds(location.x, location.y-getPreferredSize().height,148 getPreferredSize().width, getPreferredSize().height);149 }150 151 publicvoidsetText(String text) {152 label.setText(text);153 }154 155 publicvoidsetIcon(Icon icon) {156 label.setIcon(icon);157 }158 159 publicvoidsetIconTextGap(inticonTextGap) {160 label.setIconTextGap(iconTextGap);161 }162 163 publicvoidsetVisible(booleanshow) {164 if(show) {165 determineAndSetLocation();166 findShowPlace();167 }168 super.setVisible(show);169 }170 171 privatevoidfindShowPlace() {172 if(haveShowPlace) {173 return;174 }175 //we use the popup layer of the top level container (frame or176 //dialog) to show the balloon tip177 //first we need to determine the top level container

9b8a8a44dd1c74ae49c20a7cd451974e.png178 Container parent=attachedComponent.getParent();179 JLayeredPane layeredPane;180 while(true) {181 if(parentinstanceofJFrame) {182 layeredPane=((JFrame) parent).getLayeredPane();183 break;184 }elseif(parentinstanceofJDialog) {185 layeredPane=((JDialog) parent).getLayeredPane();186 break;187 }188 parent=parent.getParent();189 }190 layeredPane.add(this, JLayeredPane.POPUP_LAYER);191 haveShowPlace=true;192 }193 }194 }195

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值