numberformatdemo.java
来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 983 行 · 第 1/2 页
JAVA
983 行
*/
public void workaroundSetText(TextComponent area, String newText) {
String foo = workaround(area.getText());
if (foo.equals(newText)) return;
area.setText(newText);
//if (area.gotFocus())
// area.select(newText.length(),newText.length());
}
/**
* Update the attributes.
*/
public void updateAttributes() {
workaroundSetText(intMin,toString(format.getMinimumIntegerDigits()) );
workaroundSetText(intMax,toString(format.getMaximumIntegerDigits()) );
workaroundSetText(decMin,toString(format.getMinimumFractionDigits()) );
workaroundSetText(decMax,toString(format.getMaximumFractionDigits()) );
workaroundSetText(negPrefix,format.getNegativePrefix() );
workaroundSetText(negSuffix,format.getNegativeSuffix() );
workaroundSetText(posPrefix,format.getPositivePrefix() );
workaroundSetText(posSuffix,format.getPositiveSuffix() );
}
/**
* Get the default number formatter.
*/
public void clickedGetDefault() {
setFormatFromLocale();
}
/**
* Update the number formatter with currency attribute.
*/
public void clickedGetCurrency() {
setFormatFromLocale();
}
/**
* Update the number formatter with percentage attribute.
*/
public void clickedGetPercent() {
setFormatFromLocale();
}
/**
* Update the number formatter when a new locale is selected.
*/
public void selectedLocaleMenu() {
setFormatFromLocale();
}
/**
* If the user changes the input number, format it immediately
*/
public void inputChanged() {
clickedRightButton();
}
/**
* If the user changes the output number, format it immediately
*/
public void outputChanged() {
clickedLeftButton();
}
/**
* Update the text in the input area to use the new number format.
*/
public void clickedRightButton() {
String leftString = workaround(inputText.getText());
Number num;
try {
num = new Double(leftString);
}
catch (IllegalArgumentException e) {
outputText.setText("");
inputText.selectAll();
return;
}
workaroundSetText(outputText,format.format(num));
}
/**
* Update the text in the output area to use the new number format.
*/
public void clickedLeftButton() {
String rightString = workaround(outputText.getText());
Number num;
ParsePosition status = new ParsePosition(1);
try {
num = format.parse(rightString, status);
}
catch (IllegalArgumentException e) {
errorText("Exception: " + e.getClass() + e.getMessage());
inputText.setText("");
outputText.selectAll();
return;
}
if (status.getIndex() != rightString.length()) {
errorText("excess text: " +
rightString.substring(status.getIndex(),
rightString.length()));
}
workaroundSetText(inputText, num.toString() );
}
/**
* When the user changes the field containing the pattern, a new format
* needs to be constructed from the pattern, then redisplay everything.
*/
public void patternChanged() {
String foo = workaround(patternText.getText());
try {
if (isLocalized) {
format.applyLocalizedPattern(foo);
errorText("Pattern: " + foo + " -> " +
format.toLocalizedPattern());
} else {
format.applyPattern(foo);
errorText("Pattern: " + foo + " -> " +
format.toPattern());
}
} catch (Exception bar) {
errorText("Exception: " + bar.getClass() + bar.getMessage());
}
updateAttributes();
clickedRightButton();
}
//------------------------------------------------------------
// package private
//------------------------------------------------------------
void addWithFont(Container container, Component foo, Font font) {
if (font != null)
foo.setFont(font);
container.add(foo);
}
//{{DECLARE_CONTROLS
Panel localePanel;
CheckboxGroup group1;
Label label1;
Label label2;
Label label3;
Choice localeMenu;
Label localeLabel;
TextField inputText;
TextField outputText;
//Button rightButton;
//Button leftButton;
TextField patternText;
Label label4;
TextField negPrefix;
TextField negSuffix;
TextField posPrefix;
TextField posSuffix;
Label label5;
Label label6;
Label label7;
Label label8;
TextField intMin;
TextField intMax;
TextField decMin;
TextField decMax;
Label label9;
Label label10;
Label label11;
Label label12;
Checkbox getInstance;
Checkbox getCurrency;
Checkbox getPercent;
Checkbox getLocalized;
Label label13;
Label label14;
//}}
//------------------------------------------------------------
// private
//------------------------------------------------------------
private void buildGUI()
{
//{{INIT_CONTROLS
setBackground(Color.white); // MD 8/7
// shouldn't be necessary, but it is!
setLayout(new FlowLayout(FlowLayout.CENTER,2,2));
// INPUT/OUTPUT/PATTERN
Panel creditPanel = new Panel();
label1=new Label("Decimal Number Formatting Demo",
Label.CENTER);
label1.setFont(Utility.titleFont);
creditPanel.add(label1);
label13=new Label(creditString, Label.CENTER);
label13.setFont(Utility.creditFont);
creditPanel.add(label13);
Utility.fixGrid(creditPanel,1);
add(creditPanel);
// IO
Panel ioPanel = new Panel();
label3=new Label("New Number", Label.RIGHT);
label3.setFont(Utility.labelFont);
ioPanel.add(label3);
outputText=new TextField(FIELD_COLUMNS);
outputText.setFont(Utility.editFont);
ioPanel.add(outputText);
/*Panel gap1 = new Panel();
gap1.setLayout(null);
gap1.resize(5,5);
gap1.add(outputText);
Panel gap2 = new Panel();
gap2.setLayout(null);
gap2.resize(5,5);
gap2.add(outputText);
*/
label2=new Label("Java 1.0 Number", Label.RIGHT);
label2.setFont(Utility.labelFont);
ioPanel.add(label2);
inputText=new TextField(FIELD_COLUMNS);
inputText.addKeyListener(this);
inputText.setFont(Utility.editFont);
ioPanel.add(inputText);
label4=new Label("Pattern", Label.RIGHT);
label4.setFont(Utility.labelFont);
ioPanel.add(label4);
patternText=new TextField(FIELD_COLUMNS);
patternText.addKeyListener(this);
patternText.setFont(Utility.editFont);
ioPanel.add(patternText);
ioPanel.add(new Label(" "));
getLocalized=new Checkbox("Localized Pattern");
getLocalized.addItemListener(this);
getLocalized.setFont(Utility.labelFont);
ioPanel.add(getLocalized);
Utility.fixGrid(ioPanel,2);
add(ioPanel);
// LOCALE
localePanel=new Panel();
localeLabel=new Label("Locale:");
localeLabel.setFont(Utility.labelFont);
localePanel.add("loc",localeLabel);
localeMenu= new Choice();
localeMenu.addItemListener(this);
localeMenu.setFont(Utility.choiceFont);
localePanel.add(localeMenu);
// FORMAT CHECKS
group1= new CheckboxGroup();
getInstance=new Checkbox("Default Format",group1, false);
getInstance.addItemListener(this);
getInstance.setFont(Utility.labelFont);
localePanel.add(getInstance);
getCurrency=new Checkbox("Currency Format",group1, true);
getCurrency.addItemListener(this);
getCurrency.setFont(Utility.labelFont);
localePanel.add(getCurrency);
getPercent=new Checkbox("Percent Format",group1, false);
getPercent.addItemListener(this);
getPercent.setFont(Utility.labelFont);
localePanel.add(getPercent);
Utility.fixGrid(localePanel,1);
Panel bottomPanel = new Panel();
bottomPanel.add(localePanel);
BorderPanel x = new BorderPanel();
x.setBackground(Color.lightGray);
x.setLayout(null);
x.setSize(8,130);
bottomPanel.add(x);
// PARAMETERS
Panel parameterPanel = new Panel();
parameterPanel.add(new Label(" "));
label11=new Label("Min", Label.CENTER);
label11.setFont(Utility.labelFont);
parameterPanel.add(label11);
label12=new Label("Max", Label.CENTER);
label12.setFont(Utility.labelFont);
parameterPanel.add(label12);
label9=new Label("Integers", Label.RIGHT);
label9.setFont(Utility.labelFont);
parameterPanel.add(label9);
intMin=new TextField(4);
intMin.addKeyListener(this);
intMin.setFont(Utility.editFont);
parameterPanel.add(intMin);
intMax=new TextField(4);
intMax.addKeyListener(this);
intMax.setFont(Utility.editFont);
parameterPanel.add(intMax);
label10=new Label("Decimals", Label.RIGHT);
label10.setFont(Utility.labelFont);
parameterPanel.add(label10);
decMin=new TextField(4);
decMin.addKeyListener(this);
decMin.setFont(Utility.editFont);
parameterPanel.add(decMin);
decMax=new TextField(4);
decMax.addKeyListener(this);
decMax.setFont(Utility.editFont);
parameterPanel.add(decMax);
parameterPanel.add(new Label(" "));
label7=new Label("Prefix", Label.CENTER);
label7.setFont(Utility.labelFont);
parameterPanel.add(label7);
label8=new Label("Suffix", Label.CENTER);
label8.setFont(Utility.labelFont);
parameterPanel.add(label8);
label6=new Label("Positive", Label.RIGHT);
label6.setFont(Utility.labelFont);
parameterPanel.add(label6);
posPrefix=new TextField(4);
posPrefix.addKeyListener(this);
posPrefix.setFont(Utility.editFont);
parameterPanel.add(posPrefix);
posSuffix=new TextField(4);
posSuffix.addKeyListener(this);
posSuffix.setFont(Utility.editFont);
parameterPanel.add(posSuffix);
label5=new Label("Negative", Label.RIGHT);
label5.setFont(Utility.labelFont);
parameterPanel.add(label5);
negPrefix=new TextField(4);
negPrefix.addKeyListener(this);
negPrefix.setFont(Utility.editFont);
parameterPanel.add(negPrefix);
negSuffix=new TextField(4);
negSuffix.addKeyListener(this);
negSuffix.setFont(Utility.editFont);
parameterPanel.add(negSuffix);
Utility.fixGrid(parameterPanel,3);
bottomPanel.add(parameterPanel);
Utility.fixGrid(bottomPanel,3);
Utility.setInsets(bottomPanel,x,new Insets(20,20,2,2));
add(bottomPanel);
Panel copyrightPanel = new Panel();
addWithFont (copyrightPanel,new Label(copyrightString, Label.LEFT),
Utility.creditFont);
addWithFont (copyrightPanel,new Label(copyrightString2, Label.LEFT),
Utility.creditFont);
Utility.fixGrid(copyrightPanel,1);
add(copyrightPanel);
}
private String toString( int anInt )
{
Integer intObj = new Integer(anInt);
return intObj.toString();
}
private int parseIntField(TextComponent text) {
int result = 0;
try {
Integer value = new Integer(workaround(text.getText()));
result = value.intValue();
}
catch (NumberFormatException e) {
}
return result;
}
private void intMinChanged() {
format.setMinimumIntegerDigits(parseIntField(intMin));
updateAttributes();
handleAttributeChange();
}
private void intMaxChanged() {
format.setMaximumIntegerDigits(parseIntField(intMax));
updateAttributes();
handleAttributeChange();
}
private void decMinChanged() {
format.setMinimumFractionDigits(parseIntField(decMin));
updateAttributes();
handleAttributeChange();
}
private void decMaxChanged() {
format.setMaximumFractionDigits(parseIntField(decMax));
updateAttributes();
handleAttributeChange();
}
private void negPrefixChanged() {
format.setNegativePrefix(workaround(negPrefix.getText()));
updateAttributes();
handleAttributeChange();
}
private void negSuffixChanged() {
format.setNegativeSuffix(workaround(negSuffix.getText()));
updateAttributes();
handleAttributeChange();
}
private void posPrefixChanged() {
format.setPositivePrefix(workaround(posPrefix.getText()));
updateAttributes();
handleAttributeChange();
}
private void posSuffixChanged() {
format.setPositiveSuffix(workaround(posSuffix.getText()));
updateAttributes();
handleAttributeChange();
}
/**
* Strips high bits, because of bug in Java
*/
private static String workaround(String source) {
StringBuffer result = new StringBuffer();
for (int i = 0; i < source.length(); ++i)
result.append((char)(source.charAt(i) & 0xFF));
return result.toString();
}
private void errorText(String s)
{
if (DEBUG)
{
System.out.println(s);
}
}
private static final String creditString =
"v1.1a6, Demo:";
private static final String copyrightString =
"";
private static final String copyrightString2 =
"";
private static final int FIELD_COLUMNS = 35;
private static final boolean DEBUG = false;
private static boolean isLocalized = false;
// private Choice formatter, locale;
private DecimalFormat format;
private Locale[] locales;
private DemoApplet applet;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?