DOC

3373.docjava

By Audrey Hayes,2014-10-16 21:20
9 views 0
3373.docjava

    由于程序的原因;本文件未被完整保存。 首页 ? 研究文集 ? Java GUI

    如何实现类似于微软的IP输入框的输入框

    xuwanhong 原创 (参与分~119350;专家分~1900) 发表~2002-09-03 13:22 版本~1.0 阅读~3323

该实现采用是个小的文本框;并且把他们放到一个指定的JTextField上;在每个小文本框之间绘制四个小的原点。

    具体实现代码如下~

import java.awt.*;

    import java.awt.event.*;

    import java.beans.*;

    import javax.swing.*;

    import javax.swing.text.*;

    import javax.swing.plaf.*;

    import javax.swing.event.*;

    import javax.accessibility.*;

import javax.swing.event.*;

    import java.util.*;

    import java.io.ObjectOutputStream;

    import java.io.ObjectInputStream; import java.io.IOException;

    import javax.swing.border.*;

    public class IPField extends JTextField

    {

     private String st1 = "";

     private String st2 = "";

     private String st3 = "";

     private String st4 = "";

     private JTextIPSpace t1;

     private JTextIPSpace t2;

     private JTextIPSpace t3;

     private JTextIPSpace t4;

     final private static int RADII = 2;

     final private static int SPACEINTERVAL = 2;

     private Color DefaultColor_Selected = new Color(255,0,0);

     private Color DefaultColor_NoSelected = Color.black;

     public IPField(boolean isSelected)

     {

     this(isSelected,"");

     }

     public IPField(String ipAddress)

     {

     this(false,ipAddress);

     }

     public IPField()

     {

     this(false);

     }

     public IPField(boolean isSelected,Color colorSelect,Color colorNoSelect)

     {

     this(isSelected,"",colorSelect,colorNoSelect);

     }

     public IPField(boolean isSelected,String ipAddress)

     {

     this(isSelected,"",null,null);

     }

     public IPField(boolean isSelected,String ipAddress,Color colorSelect,Color colorNoSelect)

     {

     super();

     analyzeStr(ipAddress);

     InitIPField();

     setChildComponent(false);

     setLayout(null);

     //changeChildComponent();

     t1.setPrevNextComponent(t4,t2);

     t2.setPrevNextComponent(t1,t3);

     t3.setPrevNextComponent(t2,t4);

     t4.setPrevNextComponent(t3,t1);

     add(t1);

     add(t2);

     add(t3);

     add(t4);

     //setSize(getPreferredSize());

     addComponentListener(

     new ComponentListener()

     {

     public void componentResized(ComponentEvent e)

     {

     changeChildComponent();

     }

     public void componentMoved(ComponentEvent e){}

     public void componentShown(ComponentEvent e){}

     public void componentHidden(ComponentEvent e){}

     }

     );

     setChildComponent(true);

     if(colorSelect!=null)

     DefaultColor_Selected = colorSelect;

     if(colorNoSelect!=null)

     DefaultColor_NoSelected = colorNoSelect;

     setSelectEdge(isSelected,DefaultColor_Selected,DefaultColor_NoSelected);

     }

     //设置选中或未选中的颜色

     public void setSelected(Color color)

     {

     DefaultColor_Selected = color;

     }

     public void setSelected()

     {

     DefaultColor_Selected = Color.blue;

     }

     public void setNoSelected(Color color)

     {

     DefaultColor_NoSelected = color;

     }

     public void setNoSelected()

     {

     DefaultColor_NoSelected = Color.black;

     }

     public void setSelectEdge(boolean isSelected,Color selected,Color noSelected)

     {

     Border border;

     if(isSelected)

     border = BorderFactory.createLineBorder(selected.brighter(),1);

     else

     border = BorderFactory.createLineBorder(noSelected,1);

     setBorder(border);

     }

     public Dimension getPreferredSize()

     {

     //FontMetrics fm = getGraphics().getFontMetrics();

     //return new Dimension(fm.charWidth('8')*3 + 5, fm.getHeight()+4);

     Dimension dim = new Dimension(100,30);

     return dim;

     }

     private void InitIPField()

     {

     t1 = new JTextIPSpace(st1,255,true);

     t2 = new JTextIPSpace(st2,255,false);

     t3 = new JTextIPSpace(st3,255,false);

     t4 = new JTextIPSpace(st4,255,false);

     }

     private void setTextIPField()

     {

     t1.setText(st1);

     t2.setText(st2);

     t3.setText(st3);

     t4.setText(st4);

     }

     /**

     * setText(String text).

     */

     public void setText(String text)

     {

     st1 = "";st2 = "";st3 = "";st4 = "";

     analyzeStr(text);

     setTextIPField();

     }

     /**

     * 设置显示的IP.

     * @param ip 所显示的IP(字符串形式)

     */

     public void setIP(String ip)

     {

     setText(ip);

     }

     /**

     * 得到Field内的IP.

     * @return 返回IP(字符串形式)

     */

     public String getIP()

     {

     return availIP();

     }

     public String getText()

     {

     return getIP();

     }

     /**

     * 设置Field内的IP.

     * @param ip IP的值

     */

     public void setIPValue(int ip)

     {

     }

     /**

     * 得到IP的值.

     * @return 返回IP的值

     */

     public int getIPValue()

     {

     return isIPaddress(getIP());

     }

     /**

     * 将显示的IP转换成标准IP,显示的IP中存在' '.

     * @param ip 显示的IP

     * @return 标准的IP

     */

     private String availIP()

     {

     String strTemp[] = {t1.getText().trim(),t2.getText().trim(),

     t3.getText().trim(),t4.getText().trim()};

     String returnIP = "";

     for(int i = 0;i<strTemp.length;i++)

     {

     if(!strTemp[i].equals(""))

     {

     if(!returnIP.equals(""))

     returnIP += "." + strTemp[i];

     else

     returnIP = strTemp[i];

     }

     }

     return returnIP;

     }

     /**

     * 判断IP地址的正确性.

     * @param ipAddress IP字符串

     * @return 如果IP有效返回IP的值

     * 否则返回-1

     */

     public static int isIPaddress(String ipAddress)

     {

     int pointNum = 0, numberNum = 0;

     int numberVal = 0;

     int len = ipAddress.length();

     int m = 0;

     int IPVal = 0;

     char tempchar;

     while(m < len && ((tempchar = ipAddress.charAt(m)) == '.' || Character.isDigit(tempchar)))

     {

     m++;

     if((48 <= tempchar) && (tempchar<= 57)) {

     if(numberNum > 2) return -1;

     numberVal *= 10;

     numberVal += tempchar - '0';

     if(numberVal > 255) return -1;

     numberNum ++;

     }

     else {

     if(numberNum == 0) return -1;

     if(pointNum == 3) return -1;

     pointNum++;

     IPVal *= 256;

     IPVal += numberVal;

     numberNum = 0;

     numberVal = 0;

     }

     }

     if(m != len || pointNum != 3 || numberNum == 0) return -1;

     pointNum++;

     IPVal *= 256;

     IPVal += numberVal;

     return IPVal;

     }

     private void analyzeStr(String str)

     {

     try

     {

     StringTokenizer st = new StringTokenizer(str, ".");

     String tk = "";

     int i = 0;

     while(st.hasMoreTokens() && i<=3)

     {

     i++;

     tk = st.nextToken();

     if(!tk.equals(""))

     {

     switch(i)

     {

     case 1:

     st1 = tk;break;

     case 2:

     st2 = tk;break;

     case 3:

     st3 = tk;break;

     case 4:

     st4 = tk;break;

     }

     }

     tk = "";

     }

     }

     catch(Exception ex)

     {

     st1 = "";

     st2 = "";

     st3 = "";

     st4 = "";

     }

     }

     private void changeChildComponent()

     {

     Dimension sz = getSize();

     int spaceWidth = sz.width/4;

     int spaceHeight = sz.height - 2 * SPACEINTERVAL;

     //int r = Math.min(sz.height,sz.width)/6;

     int r = RADII;

     t1.setBounds(0 + SPACEINTERVAL,0 + SPACEINTERVAL,spaceWidth - r,spaceHeight);

     t2.setBounds(spaceWidth + r,0 + SPACEINTERVAL,spaceWidth - r,spaceHeight);

     t3.setBounds(2*spaceWidth + r,0 + SPACEINTERVAL,spaceWidth - r,spaceHeight);

     t4.setBounds(3*spaceWidth + r,0 + SPACEINTERVAL,spaceWidth - r - SPACEINTERVAL,spaceHeight);

     repaint();

     }

     private void setChildComponent(boolean isVisible)

     {

     t1.setVisible(isVisible);

     t2.setVisible(isVisible);

     t3.setVisible(isVisible);

     t4.setVisible(isVisible);

     repaint();

     }

     public void paintComponent(Graphics g)

     {

     Dimension sz = getSize();

     g.setColor(Color.white);

     g.fillRect(0,0,sz.width,sz.height);

     int spaceWidth = sz.width/4;

     int spaceHeight = sz.height;

     //int r = Math.min(sz.height,sz.width)/6;

     int r = RADII;

     g.setColor(Color.black);

     for(int i=1;i<=3;i++)

     {

     g.fillOval(i * spaceWidth,spaceHeight-r,r,r);

     }

     //super.paintComponents(g);

     }

    }

    class JTextIPSpace extends JTextField {

     public int minValue = 0;

     public int maxValue =255;

     public JTextIPSpace textipspace;

     public boolean bFirstLostFocus = true;

     public boolean isCanFocus = false;

     public JTextField prevComponent;

     public JTextField nextComponent;

     public JTextIPSpace()

     {

     this(null,255,false);

     }

     public JTextIPSpace(String str)

     {

     this(str,255,true);

     }

     public JTextIPSpace(int maxValue,boolean isCanFocus)

     {

     this(null,maxValue,isCanFocus);

     }

     public JTextIPSpace(String str,int maxValue,boolean isCanFocus)

     {

     super(str);

     setNoEdge();

     setMidHorizontal();

     addLostFocus();

     addKeySet();

     addComponentChange();

     textipspace = this;

     this.maxValue = maxValue;

     this.isCanFocus = isCanFocus;

     }

     public void setNoEdge()

     {

     //Border border;

     //border = BorderFactory.createEmptyBorder();

     //setBorder(border1);

     setBorder(null);

     setOpaque(true);

     }

     public void setPrevNextComponent(JTextField prev,JTextField next)

     {

     this.prevComponent = prev;

     this.nextComponent = next;

     }

     private void addComponentChange()

     {

     ComponentListener cl = new ComponentListener()

     {

     public void componentResized(ComponentEvent e)

     {

     /*

     Dimension sz = getSize();

     int fontheight = sz.height;

     int fontwidth = sz.width/2;

     int fontsize = Math.min(fontheight,fontwidth);

     setFont(new Font("Dialog", 0, fontsize));

     */

     }

     public void componentMoved(ComponentEvent e){}

     public void componentShown(ComponentEvent e){}

     public void componentHidden(ComponentEvent e){}

     };

     addComponentListener(cl);

     }

     private void addLostFocus()

     {

     FocusListener fl = new FocusListener()

     {

     public void focusLost(FocusEvent e)

     {

     String tmp = getText().trim();

     if(tmp.equals("")) return;

     if(Integer.parseInt(tmp)>maxValue && bFirstLostFocus == true)

     {

     bFirstLostFocus = false;

     JOptionPane.showConfirmDialog(textipspace,

     tmp + "不是一个合法的项目;请输入" + minValue + "?

    " + maxValue + "之间的数值:",

     "提示",JOptionPane.CLOSED_OPTION);

     setText(String.valueOf(maxValue));

     requestFocus();

     repaint();

     }

     }

     public void focusGained(FocusEvent e)

     {

     bFirstLostFocus = true;

     repaint();

     }

     };

     addFocusListener(fl);

     }

     private void addKeySet()

     {

     KeyAdapter ka = new KeyAdapter()

     {

     public void keyPressed(KeyEvent e)

     {

     if (e.getKeyCode() == KeyEvent.VK_LEFT)

     {

     int pos = getCaretPosition();

     if(pos==0)

     {

Report this document

For any questions or suggestions please email
cust-service@docsford.com