Topic: 求一个特性类似Windows的IP地址输入框组件 |
Print this page |
1.求一个特性类似Windows的IP地址输入框组件 | Copy to clipboard |
Posted by: jfml Posted on: 2003-04-19 19:39 RT |
2.Re:求一个特性类似Windows的IP地址输入框组件 [Re: CrazyJavar] | Copy to clipboard |
Posted by: jfml Posted on: 2003-04-19 20:21 找到一个,做得很好,发出来给大家共享,不知道能不能加分呢:) public class IPField extends JTextField { final private static int RADII = 2; final private static int SPACEINTERVAL = 2; /** * 判断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; } public static void main(String[] args) { JFrame f = new JFrame("IP Field Tester"); IPField ipf = new IPField(); f.getContentPane().add(ipf, BorderLayout.NORTH); f.setBounds(200, 300, 200, 160); f.setVisible(true); } private Color DefaultColor_NoSelected = Color.black; private Color DefaultColor_Selected = new Color(255, 0, 0); private String st1 = ""; private String st2 = ""; private String st3 = ""; private String st4 = ""; private JTextIPSpace t1; private JTextIPSpace t2; private JTextIPSpace t3; private JTextIPSpace t4; public IPField() { this(false); } public IPField(boolean isSelected) { this(isSelected, ""); } 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 componentHidden(ComponentEvent e) { } public void componentMoved(ComponentEvent e) { } public void componentResized(ComponentEvent e) { changeChildComponent(); } public void componentShown(ComponentEvent e) { } }); setChildComponent(true); if (colorSelect != null) { DefaultColor_Selected = colorSelect; } if (colorNoSelect != null) { DefaultColor_NoSelected = colorNoSelect; } setSelectEdge(isSelected, DefaultColor_Selected, DefaultColor_NoSelected); } public IPField(String ipAddress) { this(false, ipAddress); } 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 = ""; } } /** * 将显示的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; } 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(); } /** * 得到Field内的IP. * @return 返回IP(字符串形式) */ public String getIP() { return availIP(); } /** * 得到IP的值. * @return 返回IP的值 */ public int getIPValue() { return isIPaddress(getIP()); } 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; } public String getText() { return getIP(); } 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); } 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); } private void setChildComponent(boolean isVisible) { t1.setVisible(isVisible); t2.setVisible(isVisible); t3.setVisible(isVisible); t4.setVisible(isVisible); repaint(); } /** * 设置显示的IP. * @param ip 所显示的IP(字符串形式) */ public void setIP(String ip) { setText(ip); } /** * 设置Field内的IP值. * @param ip IP的值 */ public void setIPValue(int ip) { } public void setNoSelected() { DefaultColor_NoSelected = Color.black; } public void setNoSelected(Color color) { DefaultColor_NoSelected = color; } public void setSelected() { DefaultColor_Selected = Color.blue; } //设置选中或未选中的颜色 public void setSelected(Color color) { DefaultColor_Selected = color; } 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); } /** * 重载setText(String text). */ public void setText(String text) { st1 = ""; st2 = ""; st3 = ""; st4 = ""; analyzeStr(text); setTextIPField(); } private void setTextIPField() { t1.setText(st1); t2.setText(st2); t3.setText(st3); t4.setText(st4); } } class JTextIPSpace extends JTextField { protected class IPDocument extends PlainDocument { public JTextField jtext; public IPDocument(JTextField jtext) { this.jtext = jtext; } public void insertString(int offset, String str, AttributeSet a) throws BadLocationException { char[] insertChars = str.toCharArray(); boolean valid = true; if (getLength() >= 3) { valid = false; return; } for (int i = 0; i < Math.min(insertChars.length, 3); i++) { if (!(Character.isDigit(insertChars[i]))) { valid = false; break; } else { valid = true; break; } } if (valid) super.insertString(offset, str, a); if (getLength() > 3) super.remove(offset + str.length(), getLength() - 2); } } public boolean bFirstLostFocus = true; public boolean isCanFocus = false; public int maxValue = 255; public int minValue = 0; public JTextField nextComponent; public JTextField prevComponent; public JTextIPSpace textipspace; public JTextIPSpace() { this(null, 255, false); } public JTextIPSpace(int maxValue, boolean isCanFocus) { this(null, maxValue, isCanFocus); } public JTextIPSpace(String str) { this(str, 255, true); } public JTextIPSpace(String str, int maxValue, boolean isCanFocus) { super(str); setNoEdge(); setMidHorizontal(); addLostFocus(); addKeySet(); addComponentChange(); textipspace = this; this.maxValue = maxValue; this.isCanFocus = isCanFocus; } private void addComponentChange() { ComponentListener cl = new ComponentListener() { public void componentHidden(ComponentEvent e) { } public void componentMoved(ComponentEvent e) { } 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 componentShown(ComponentEvent e) { } }; addComponentListener(cl); } private void addKeySet() { KeyAdapter ka = new KeyAdapter() { public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_LEFT) { int pos = getCaretPosition(); if (pos == 0) { gotoComponent(prevComponent); } } else if (e.getKeyCode() == KeyEvent.VK_UP) { if (prevComponent != null) prevComponent.selectAll(); gotoComponent(prevComponent); } else if (e.getKeyCode() == KeyEvent.VK_DOWN) { if (nextComponent != null) nextComponent.selectAll(); gotoComponent(nextComponent); } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) { int pos = getCaretPosition(); int maxPos = getText().trim().length(); if (pos == maxPos || maxPos == 0) { gotoComponent(nextComponent); } } else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) { int pos = getCaretPosition(); if (pos == 0) { gotoComponent(prevComponent); } } else if (e.getKeyChar() == '.') { int maxPos = getText().trim().length(); if (maxPos > 0) { if (getSelectedText() == null) { if (nextComponent != null) nextComponent.selectAll(); gotoComponent(nextComponent); } } } } public void keyReleased(KeyEvent e) { if (Character.isDigit(e.getKeyChar())) { int pos = getCaretPosition(); //int maxPos = getText().trim().length(); if (pos >= 3) { gotoComponent(nextComponent); } } } }; addKeyListener(ka); } private void addLostFocus() { FocusListener fl = new FocusListener() { public void focusGained(FocusEvent e) { bFirstLostFocus = true; repaint(); } 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(); } } }; addFocusListener(fl); } protected Document createDefaultModel() { return new IPDocument(this); } public void gotoComponent(JComponent jc) { if (jc != null) jc.requestFocus(); } public boolean isFocusTraversable() { return isCanFocus; } public void setMidHorizontal() { setHorizontalAlignment(JTextField.CENTER); } 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; } |
3.Re:求一个特性类似Windows的IP地址输入框组件 [Re: CrazyJavar] | Copy to clipboard |
Posted by: floater Posted on: 2003-04-19 23:06 http://www.cjsdn.com/post/view?bid=1&id=10915&tpg=1&ppg=1&sty=1&age=0#10915 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |