又一个用来输入IP地址的类控件。共2个文件
文件一:
package textField;import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.border.*;public class IPAddressEditor extends JPanel {
private static final Dimension fixedDimension = new Dimension(148,26);
private static final int fixedWidth = 148;
private static final int fixedHeight = 26;private String ipAddress;DigitalText digitalText1 = new DigitalText();
DigitalText digitalText2 = new DigitalText();
DigitalText digitalText3 = new DigitalText();
DigitalText digitalText4 = new DigitalText();JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();Border border1;public IPAddressEditor() {
try {
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
}private void jbInit() throws Exception {
border1 = BorderFactory.createEmptyBorder();this.setLayout(null);
this.setBorder(BorderFactory.createEtchedBorder());
this.addComponentListener(new java.awt.event.ComponentAdapter() {public void componentResized(ComponentEvent e) {
this_componentResized(e);
}
});this.setSize(fixedWidth,fixedHeight);
this.setMaximumSize(fixedDimension);
this.setMinimumSize(fixedDimension);
this.setPreferredSize(fixedDimension);
//this.setRequestFocusEnabled(false);
digitalText1.setMaxLength(3);
digitalText1.setMaxValue(255);
digitalText1.setValue(255);
digitalText1.setBorder(border1);
digitalText1.setHorizontalAlignment(SwingConstants.CENTER);
digitalText1.setBounds(new Rectangle(2, 2, 30, 22));
digitalText1.addKeyListener(new java.awt.event.KeyAdapter() {public void keyReleased(KeyEvent e) {
digitalText1_keyReleased(e);
}
});digitalText2.setBounds(new Rectangle(40, 2, 30, 22));
digitalText2.addKeyListener(new java.awt.event.KeyAdapter() {public void keyReleased(KeyEvent e) {
digitalText2_keyReleased(e);
}
});
digitalText2.setBorder(border1);
digitalText2.setHorizontalAlignment(SwingConstants.CENTER);
digitalText2.setMaxLength(3);
digitalText2.setMaxValue(255);
digitalText2.setValue(255);digitalText3.setBounds(new Rectangle(78, 2, 30, 22));
digitalText3.addKeyListener(new java.awt.event.KeyAdapter() {public void keyReleased(KeyEvent e) {
digitalText3_keyReleased(e);
}
});
digitalText3.setBorder(border1);
digitalText3.setHorizontalAlignment(SwingConstants.CENTER);
digitalText3.setMaxLength(3);
digitalText3.setMaxValue(255);
digitalText3.setValue(255);digitalText4.setBounds(new Rectangle(116, 2, 30, 22));
digitalText4.addKeyListener(new java.awt.event.KeyAdapter() {public void keyReleased(KeyEvent e) {
digitalText4_keyReleased(e);
}
});
digitalText4.setBorder(border1);
digitalText4.setHorizontalAlignment(SwingConstants.CENTER);
digitalText4.setMaxLength(3);
digitalText4.setMaxValue(255);
digitalText4.setValue(255);jLabel1.setBackground(Color.white);
jLabel1.setFont(new java.awt.Font("Dialog", 1, 14));
jLabel1.setForeground(Color.blue);
jLabel1.setOpaque(true);
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setText(".");
jLabel1.setBounds(new Rectangle(32, 2, 8, 22));jLabel2.setBounds(new Rectangle(70, 2, 8, 22));
jLabel2.setText(".");
jLabel2.setForeground(Color.blue);
jLabel2.setOpaque(true);
jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
jLabel2.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel2.setFont(new java.awt.Font("Dialog", 1, 14));
jLabel2.setBackground(Color.white);jLabel3.setBounds(new Rectangle(108, 2, 8, 22));
jLabel3.setText(".");
jLabel3.setForeground(Color.blue);
jLabel3.setOpaque(true);
jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
jLabel3.setFont(new java.awt.Font("Dialog", 1, 14));
jLabel3.setBackground(Color.white);
this.add(digitalText1, null);
this.add(jLabel1, null);
this.add(digitalText2, null);
this.add(jLabel2, null);
this.add(digitalText3, null);
this.add(jLabel3, null);
this.add(digitalText4, null);this.setIpAddress("127.0.0.0");
}public void setIpAddress(String newIpAddress) {//以"/"为分割符
StringTokenizer strToken=new StringTokenizer(newIpAddress,".");
String[] strIPAddress=new String[strToken.countTokens()];if(strToken.countTokens()!=4)
return;//divide the newIpAddress up three strings
int k=0;
while(strToken.hasMoreTokens())
{
strIPAddress[k]=strToken.nextToken();
k++;
}//set value for each TextField
try{
digitalText1.setValue(Integer.parseInt(strIPAddress[0]));
digitalText2.setValue(Integer.parseInt(strIPAddress[1]));
digitalText3.setValue(Integer.parseInt(strIPAddress[2]));
digitalText4.setValue(Integer.parseInt(strIPAddress[3]));ipAddress = getIpAddress();
}
catch(Exception e){
return;
}
}public String getIpAddress() {
ipAddress = Integer.toString(digitalText1.getValue()) + "." +
Integer.toString(digitalText2.getValue()) + "." +
Integer.toString(digitalText3.getValue()) + "." +
Integer.toString(digitalText4.getValue());
return ipAddress;
}void this_componentResized(ComponentEvent e) {
this.setSize(fixedWidth,fixedHeight);
validate();
}public void setBounds(int l,int t,int w,int h)
{
System.out.println("my setBounds fuction3!");
super.setBounds(l,t,fixedWidth,fixedHeight);
return;
}void digitalText1_keyReleased(KeyEvent e) {
if(digitalText1.getText().length() >= digitalText1.getMaxLength()){
digitalText2.requestFocus();
digitalText2.selectAll();
}
}void digitalText2_keyReleased(KeyEvent e) {
if(digitalText2.getText().length() >= digitalText2.getMaxLength()){
digitalText3.requestFocus();
digitalText3.selectAll();
}
}void digitalText3_keyReleased(KeyEvent e) {
if(digitalText3.getText().length() >= digitalText3.getMaxLength()){
digitalText4.requestFocus();
digitalText4.selectAll();
}
}void digitalText4_keyReleased(KeyEvent e) {
if(digitalText4.getText().length() >= digitalText4.getMaxLength()){
digitalText4.nextFocus();
}
}
}
文件一:
package textField;import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.border.*;public class IPAddressEditor extends JPanel {
private static final Dimension fixedDimension = new Dimension(148,26);
private static final int fixedWidth = 148;
private static final int fixedHeight = 26;private String ipAddress;DigitalText digitalText1 = new DigitalText();
DigitalText digitalText2 = new DigitalText();
DigitalText digitalText3 = new DigitalText();
DigitalText digitalText4 = new DigitalText();JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();Border border1;public IPAddressEditor() {
try {
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
}private void jbInit() throws Exception {
border1 = BorderFactory.createEmptyBorder();this.setLayout(null);
this.setBorder(BorderFactory.createEtchedBorder());
this.addComponentListener(new java.awt.event.ComponentAdapter() {public void componentResized(ComponentEvent e) {
this_componentResized(e);
}
});this.setSize(fixedWidth,fixedHeight);
this.setMaximumSize(fixedDimension);
this.setMinimumSize(fixedDimension);
this.setPreferredSize(fixedDimension);
//this.setRequestFocusEnabled(false);
digitalText1.setMaxLength(3);
digitalText1.setMaxValue(255);
digitalText1.setValue(255);
digitalText1.setBorder(border1);
digitalText1.setHorizontalAlignment(SwingConstants.CENTER);
digitalText1.setBounds(new Rectangle(2, 2, 30, 22));
digitalText1.addKeyListener(new java.awt.event.KeyAdapter() {public void keyReleased(KeyEvent e) {
digitalText1_keyReleased(e);
}
});digitalText2.setBounds(new Rectangle(40, 2, 30, 22));
digitalText2.addKeyListener(new java.awt.event.KeyAdapter() {public void keyReleased(KeyEvent e) {
digitalText2_keyReleased(e);
}
});
digitalText2.setBorder(border1);
digitalText2.setHorizontalAlignment(SwingConstants.CENTER);
digitalText2.setMaxLength(3);
digitalText2.setMaxValue(255);
digitalText2.setValue(255);digitalText3.setBounds(new Rectangle(78, 2, 30, 22));
digitalText3.addKeyListener(new java.awt.event.KeyAdapter() {public void keyReleased(KeyEvent e) {
digitalText3_keyReleased(e);
}
});
digitalText3.setBorder(border1);
digitalText3.setHorizontalAlignment(SwingConstants.CENTER);
digitalText3.setMaxLength(3);
digitalText3.setMaxValue(255);
digitalText3.setValue(255);digitalText4.setBounds(new Rectangle(116, 2, 30, 22));
digitalText4.addKeyListener(new java.awt.event.KeyAdapter() {public void keyReleased(KeyEvent e) {
digitalText4_keyReleased(e);
}
});
digitalText4.setBorder(border1);
digitalText4.setHorizontalAlignment(SwingConstants.CENTER);
digitalText4.setMaxLength(3);
digitalText4.setMaxValue(255);
digitalText4.setValue(255);jLabel1.setBackground(Color.white);
jLabel1.setFont(new java.awt.Font("Dialog", 1, 14));
jLabel1.setForeground(Color.blue);
jLabel1.setOpaque(true);
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setText(".");
jLabel1.setBounds(new Rectangle(32, 2, 8, 22));jLabel2.setBounds(new Rectangle(70, 2, 8, 22));
jLabel2.setText(".");
jLabel2.setForeground(Color.blue);
jLabel2.setOpaque(true);
jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
jLabel2.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel2.setFont(new java.awt.Font("Dialog", 1, 14));
jLabel2.setBackground(Color.white);jLabel3.setBounds(new Rectangle(108, 2, 8, 22));
jLabel3.setText(".");
jLabel3.setForeground(Color.blue);
jLabel3.setOpaque(true);
jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
jLabel3.setFont(new java.awt.Font("Dialog", 1, 14));
jLabel3.setBackground(Color.white);
this.add(digitalText1, null);
this.add(jLabel1, null);
this.add(digitalText2, null);
this.add(jLabel2, null);
this.add(digitalText3, null);
this.add(jLabel3, null);
this.add(digitalText4, null);this.setIpAddress("127.0.0.0");
}public void setIpAddress(String newIpAddress) {//以"/"为分割符
StringTokenizer strToken=new StringTokenizer(newIpAddress,".");
String[] strIPAddress=new String[strToken.countTokens()];if(strToken.countTokens()!=4)
return;//divide the newIpAddress up three strings
int k=0;
while(strToken.hasMoreTokens())
{
strIPAddress[k]=strToken.nextToken();
k++;
}//set value for each TextField
try{
digitalText1.setValue(Integer.parseInt(strIPAddress[0]));
digitalText2.setValue(Integer.parseInt(strIPAddress[1]));
digitalText3.setValue(Integer.parseInt(strIPAddress[2]));
digitalText4.setValue(Integer.parseInt(strIPAddress[3]));ipAddress = getIpAddress();
}
catch(Exception e){
return;
}
}public String getIpAddress() {
ipAddress = Integer.toString(digitalText1.getValue()) + "." +
Integer.toString(digitalText2.getValue()) + "." +
Integer.toString(digitalText3.getValue()) + "." +
Integer.toString(digitalText4.getValue());
return ipAddress;
}void this_componentResized(ComponentEvent e) {
this.setSize(fixedWidth,fixedHeight);
validate();
}public void setBounds(int l,int t,int w,int h)
{
System.out.println("my setBounds fuction3!");
super.setBounds(l,t,fixedWidth,fixedHeight);
return;
}void digitalText1_keyReleased(KeyEvent e) {
if(digitalText1.getText().length() >= digitalText1.getMaxLength()){
digitalText2.requestFocus();
digitalText2.selectAll();
}
}void digitalText2_keyReleased(KeyEvent e) {
if(digitalText2.getText().length() >= digitalText2.getMaxLength()){
digitalText3.requestFocus();
digitalText3.selectAll();
}
}void digitalText3_keyReleased(KeyEvent e) {
if(digitalText3.getText().length() >= digitalText3.getMaxLength()){
digitalText4.requestFocus();
digitalText4.selectAll();
}
}void digitalText4_keyReleased(KeyEvent e) {
if(digitalText4.getText().length() >= digitalText4.getMaxLength()){
digitalText4.nextFocus();
}
}
}
********************
* Title: DigitalText.java
* Des cription: CDMA2000-1x OMC-R project
* Copyright: Copyright (c) 2002,EASTCOMTD CO.,LTD
* Company: Eastern Communication Technology Development CO.,LTD
* @author: wenyq
* @version: 1.0
* Reversion: 1.0.1 wenyq,2002/7/26 添加对修改后值的判断,如果与原来相同,则不做修改
* 1.0.2 wenyq,2002/8/13 添加setMinMaxValue和commit方法,重写部分代码
* 添加键盘处理事件
******************************************************************************/
package textField;import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;/**
* 用来输入正整数的单行文本框
* 可以控制输入的最大长度,最大值和最小值并在显示时可以选择是否在数字前面补零以达到最大长度
*/
public class DigitalText extends JTextField implements Serializable {/**数据成员变量,分别代表当前值,最大值,最小值和最大长度,分别赋默认值*/
private int value = 0;
private int maxValue = 99;
private int minValue = 0;
private int maxLength = 2;
/**成员变量,代表输入框是否需要在最前面补零,true表示需要*/
private boolean enableAddZero = false;//构造函数
public DigitalText() {
try {
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
/**
* <pre>
* Function: jbInit
* Des cription: initialize,called by constructor
* </pre>
* @param void
* @return void
* @exception void
*/
private void jbInit() throws Exception {
//set default property such as background color,
//doucument,value,alignment etc.
setDocument(new DigitalTextDocument());
setBackground(Color.white);
setHorizontalAlignment(JTextField.RIGHT);setMaxLength(maxLength = 2);
setMaxValue(maxValue = 99);
setMinValue(minValue = 0);
setValue(value = 0);//add keyListener and focuslistener
this.addKeyListener(new java.awt.event.KeyAdapter() {public void keyReleased(KeyEvent e) {
this_keyReleased(e);
}
});
addFocusListener(new java.awt.event.FocusAdapter() {
public void focusLost(FocusEvent e) {
this_focusLost(e);
}public void focusGained(FocusEvent e) {
this_focusGained(e);
}
});
}/**
* <pre>
* Function: setValue(int newValue)
* Des cription: set a new value and refresh the view
* </pre>
* @param newValue
* @return void
* @exception void
*/
public void setValue(int newValue) {
if((newValue <= maxValue) && (newValue >= minValue))
value = newValue;
setText(Integer.toString(value));
adjustForZero();
revalidate();
}
//获取value
public int getValue() {
return value;
}//设置最大值
public void setMaxValue(int newMaxValue) {
setMinMaxValue(minValue,newMaxValue);
}//获取最大值
public int getMaxValue() {
return maxValue;
}//设置最小值
public void setMinValue(int newMinValue) {
setMinMaxValue(newMinValue,maxValue);
}
//获取最小值
public int getMinValue() {
return minValue;
}/**
* <pre>
* Function: setMinMaxValue
* Des cription: this method sets the minValue and maxValue one time,
* called by setMinValue and setMaxValue,may be used by programmer.
* </pre>
* @param newMainValue
* @param newMinValue
* @return void
* @exception void
*/
public void setMinMaxValue(int newMinValue,int newMaxValue){
//at first,save two parameters
int tempMinValue = newMinValue;
int tempMaxValue = newMaxValue;//if ralation of two parameters is NOT right,do nothing
if(tempMinValue > tempMaxValue)
return;
if(tempMaxValue < 0 || tempMinValue >(int)Math.pow(10,maxLength)-1 )
return;//if one of the two parameters is out of right boudary,set news value for them
if(tempMinValue < 0)
tempMinValue = 0;
if(tempMaxValue >(int)Math.pow(10,maxLength)-1)
tempMaxValue = (int)Math.pow(10,maxLength)-1;//give data member new values
this.maxValue = tempMaxValue;
this.minValue = tempMinValue;//adjust current value because of fresh boudaries:minValue and maxValue
//being set again
if (value < newMinValue)
setValue(newMinValue);
if (value > newMaxValue)
setValue(newMaxValue);//update document's members to hold coherence
((DigitalTextDocument)getDocument()).setMinValue(minValue);
((DigitalTextDocument)getDocument()).setMaxValue(maxValue);
}/**
* <pre>
* Function: setMaxLength
* Des cription: this method sets the maxLength and maybe need to set maxValue
* ,minValue enven value to hold logic right
* </pre>
* @param newMaxLength
* @return void
* @exception void
*/
public void setMaxLength(int newMaxLength) {
//if input parameter is illegal,do nothing
if(newMaxLength < 1)
return;//if new length is longer than the old,need NOT to change boudaries,because
//new value scope is greater than the old.All value is still legal in new length
//if new length is shorter,the case is complicated.There need to adjust some
//values accordingly//new length is longer than the old, evaluate only accordingly
if(newMaxLength >= maxLength) {
this.maxLength = newMaxLength;
((DigitalTextDocument)getDocument()).setMaxLength(maxLength);
}
else{//new length is shorter
//if the old minValue is greater than the new MAXVALUE(old maxValue is greater ofcause)
//setMinMaxValue:0~the new MAXVALUE
if (minValue > (int)Math.pow(10,newMaxLength)-1)
this.setMinMaxValue(0,(int)Math.pow(10,newMaxLength)-1);//else only old maxValue is greater than new MAXVALUE
else if (maxValue >(int)Math.pow(10,newMaxLength)-1)
this.setMinMaxValue(minValue,(int)Math.pow(10,newMaxLength)-1);//evaluate accrodingly
this.maxLength = newMaxLength;
((DigitalTextDocument)getDocument()).setMaxLength(maxLength);
}
}
return maxLength;
}private void this_focusLost(FocusEvent e){
commit();
}void writeObject(ObjectOutputStream oos) throws IOException {
oos.defaultWriteObject();
}void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
ois.defaultReadObject();
}/**
* <pre>
* Function: adjustForZero
* Des cription: this method adjust the view of textfield. if enableAddZero is TRUE,
* it add zeros before the string of value to make the string length is
* equal to maxLength.if enableAddZero is FALSE,it delete zeros which are
* the head of string and are not contributing for value;
* </pre>
* @param void
* @return void
* @exception void
*/
public void adjustForZero(){
if(enableAddZero){
String tempStr = Integer.toString(getValue());
int addZeroNumber = maxLength - tempStr.length();
for(int i = 0; i< addZeroNumber; i++)
tempStr= "0"+tempStr;
setText(tempStr);
revalidate();
}
else{
int tempValue = Integer.parseInt(getText().trim());
setText(Integer.toString(tempValue));
revalidate();
}
}public void setEnableAddZero(boolean newEnableAddZero) {
enableAddZero = newEnableAddZero;
adjustForZero();
}public boolean isEnableAddZero() {
return enableAddZero;
}void this_keyReleased(KeyEvent e) {
//if is ENTER,commit current value
if(e.getKeyCode()==e.VK_ENTER)
commit();
//if is ESCAPE,cancle current changes
if(e.getKeyCode() == e.VK_ESCAPE){
setValue(value);
}
}
/**
* <pre>
* Function: commit
* Des cription: this method is called when user ends the current edit and needs
* save current value.It is a private,called in KeyReleased and focusLost
* methods.
* </pre>
* @param void
* @return void
* @exception void
*/
private void commit(){
try{
setValue(Integer.parseInt(getText()));
}
catch(Exception ex){
setValue(value);
return;
}
}void this_focusGained(FocusEvent e) {
this.selectAll();
}
}
/******************************************************************************
* Class: DigitalTextDocument
* Extends: PlainDocument
******************************************************************************/
class DigitalTextDocument extends PlainDocument{ private int maxLength;
private int maxValue;
private int minValue;//不帯参数的构造函数
public DigitalTextDocument(){
super();
maxLength = 2;
minValue = 0;
maxValue = 99;
}//构造函数,
public DigitalTextDocument(int maxLength){
super();
maxLength = maxLength;
minValue = 0;
maxValue = (int)Math.pow(10,maxLength)-1;
}//构造函数,
public DigitalTextDocument(int maxLength,int min,int max){
super();
maxLength = maxLength;
minValue = Math.max(min,0);
maxValue = Math.min(max,(int)Math.pow(10,maxLength)-1);
}
//设定最大长度
public void setMaxLength(int length){
maxLength = length;
}//获取最大长度
public int getMaxLength(){
return maxLength;
}//设定最大值
public void setMaxValue(int max){
maxValue = max;
}//获取最大值
public int getMaxValue(){
return maxLength;
}//设定最小值
public void setMinValue(int min){
minValue = min;
}//获取最小值
public int getMinValue(){
return minValue;
}//重载父类的insertString函数
public void insertString(int offset,String str,AttributeSet a)throws BadLocationException{
int valueAfterInsert = 0;
String strBeforeInsert = getText(0,getLength());
String strAfterInsert = strBeforeInsert.substring(0,offset)+
str+
strBeforeInsert.substring(offset);
//首先保证插入该字符串后,是整数;如果不是,则不进行插入操作
try{
valueAfterInsert = Integer.parseInt(strAfterInsert);
}
catch ( NumberFormatException e){return;
} //如果插入字符创str后,文档超长,则插入失败
if(strAfterInsert.length() > maxLength)
return;
//如果插入后数据超出范围,插入失败
else if(valueAfterInsert > maxValue)
return;
else if((strAfterInsert.length() == maxLength) && (valueAfterInsert < minValue))
return;
else{
super.insertString(offset,str,a);
return;
}
}private boolean isValid(String str){
//首先保证插入该字符串后,是整数;如果不是,则不进行插入操作
int valueAfterInsert;
try{
valueAfterInsert = Integer.parseInt(str);
}
catch ( NumberFormatException e){
return false;
} //如果插入字符创str后,文档超长,则插入失败
if(str.length() > maxLength)
return false;
//如果插入后数据超出范围,插入失败
else if((valueAfterInsert > maxValue)||(valueAfterInsert < minValue))
return false;
else
return true;
}
}
首先感谢您用过我写的这个IP地址的输入框.同时,由于我的疏乎,上次给网上一个朋友mail这几个文件的时候,没有删除掉相应的个人信息,主要是里边包含了公司的一些信息,我想这个如果在网上流传,还是不太好.所以恳请诸位在共享这些文件的时候,特别是贴出来的时候,把文件头上的说明信息删除掉,这里先谢谢!.给您带来的不便还望谅解.至于楼上的这篇贴子,如果楼主或者板主能帮忙修改以下的话,再下万分感谢!
你真是个大好人啊,太谢谢你了,贴的时候本想说明一下的,可连贴了3篇就不让我再贴了,又没有人回复,所以一直等到现在,我当时也没仔细看其中的个人信息,还以为是故意保留的呢~~~所以我就尊重个人志愿了~~呵呵我是改不了了,没权限,请斑竹帮忙吧~~~我给他发给帖子
Email [email protected]
你看不到帖子吗?