?? testjformattedtextfield2.java
字號:
import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.*;
import java.net.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.border.*;
/**
* Description:
* <br/>Copyright (C), 2005-2008, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class TestJFormattedTextField2
{
private JFrame mainWin = new JFrame("測試格式化文本框");
private JButton okButton = new JButton("確定");
//定義用于添加格式化文本框的容器
private JPanel mainPanel = new JPanel();
public void init()
{
//添加按鈕
JPanel buttonPanel = new JPanel();
buttonPanel.add(okButton);
mainPanel.setLayout(new GridLayout(0, 3));
mainWin.add(mainPanel, BorderLayout.CENTER);
JFormattedTextField intField0 = new JFormattedTextField(
new InternationalFormatter(NumberFormat.getIntegerInstance())
{
protected DocumentFilter getDocumentFilter()
{
return new NumberFilter();
}
});
intField0.setValue(100);
addRow("只接受數字的文本框", intField0);
JFormattedTextField intField1 = new JFormattedTextField(NumberFormat.getIntegerInstance());
intField1.setValue(new Integer(100));
//添加輸入校驗器
intField1.setInputVerifier(new FormattedTextFieldVerifier());
addRow("帶輸入校驗器的文本框", intField1);
//創建自定義格式器對象
IPAddressFormatter ipFormatter = new IPAddressFormatter();
ipFormatter.setOverwriteMode(false);
//以自定義格式器對象創建格式化文本框
JFormattedTextField ipField = new JFormattedTextField(ipFormatter);
ipField.setValue(new byte[] { (byte)192, (byte)168, 4, 1 });
addRow("IP地址格式", ipField);
mainWin.add(buttonPanel , BorderLayout.SOUTH);
mainWin.pack();
mainWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWin.setVisible(true);
}
//定義添加一行格式化文本框的方法
private void addRow(String labelText, final JFormattedTextField field)
{
mainPanel.add(new JLabel(labelText));
mainPanel.add(field);
final JLabel valueLabel = new JLabel();
mainPanel.add(valueLabel);
//為"確定"按鈕添加事件監聽器
//當用戶單擊“確定”按鈕時,文本框后顯示文本框內的值
okButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
Object value = field.getValue();
//如果該值是數組,使用Arrays的toString方法輸出數組
if (value.getClass().isArray())
{
StringBuilder builder = new StringBuilder();
builder.append('{');
for (int i = 0; i < Array.getLength(value); i++)
{
if (i > 0) builder.append(',');
builder.append(Array.get(value, i).toString());
}
builder.append('}');
valueLabel.setText(builder.toString());
}
else
{
//輸出格式化文本框的值
valueLabel.setText(value.toString());
}
}
});
}
public static void main(String[] args)
{
new TestJFormattedTextField2().init();
}
}
//輸入校驗器
class FormattedTextFieldVerifier extends InputVerifier
{
//當輸入組件失去焦點時,該方法被觸發
public boolean verify(JComponent component)
{
JFormattedTextField field = (JFormattedTextField) component;
//返回用戶輸入是否有效
return field.isEditValid();
}
}
//數字過濾器
class NumberFilter extends DocumentFilter
{
public void insertString(FilterBypass fb, int offset, String string,
AttributeSet attr)throws BadLocationException
{
StringBuilder builder = new StringBuilder(string);
//過濾用戶輸入的所有字符
filterInt(builder);
super.insertString(fb, offset, builder.toString(), attr);
}
public void replace(FilterBypass fb, int offset, int length, String string,
AttributeSet attr)throws BadLocationException
{
if (string != null)
{
StringBuilder builder = new StringBuilder(string);
//過濾用戶替換的所有字符
filterInt(builder);
string = builder.toString();
}
super.replace(fb, offset, length, string, attr);
}
//過濾整數字符,把所有非0~9的字符全部刪除
private void filterInt(StringBuilder builder)
{
for (int i = builder.length() - 1; i >= 0; i--)
{
int cp = builder.codePointAt(i);
if (cp > '9' || cp < '0')
{
builder.deleteCharAt(i);
}
}
}
}
class IPAddressFormatter extends DefaultFormatter
{
public String valueToString(Object value)
throws ParseException
{
if (!(value instanceof byte[]))
{
throw new ParseException("該IP地址的值只能是字節數組", 0);
}
byte[] a = (byte[])value;
if (a.length != 4)
{
throw new ParseException("IP地址必須是四個整數", 0);
}
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 4; i++)
{
int b = a[i];
if (b < 0) b += 256;
builder.append(String.valueOf(b));
if (i < 3) builder.append('.');
}
return builder.toString();
}
public Object stringToValue(String text) throws ParseException
{
//將格式化文本框內的字符串以點號(.)分成四節。
String[] nums = text.split("\\.");
if (nums.length != 4)
{
throw new ParseException("IP地址必須是四個整數", 0);
}
byte[] a = new byte[4];
for (int i = 0; i < 4; i++)
{
int b = 0;
try
{
b = Integer.parseInt(nums[i]);
}
catch (NumberFormatException e)
{
throw new ParseException("IP地址必須是整數", 0);
}
if (b < 0 || b >= 256)
{
throw new ParseException("IP地址值只能在0~255之間", 0);
}
a[i] = (byte) b;
}
return a;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -