-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathNumberFormatTest.java
More file actions
145 lines (133 loc) · 4.65 KB
/
Copy pathNumberFormatTest.java
File metadata and controls
145 lines (133 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package numberFormat;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
/**
* This program demonstrates formatting numbers under various locales.
* @version 1.13 2007-07-25
* @author Cay Horstmann
*/
public class NumberFormatTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new NumberFormatFrame();
frame.setTitle("NumberFormatTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
/**
* This frame contains radio buttons to select a number format, a combo box to pick a locale, a text
* field to display a formatted number, and a button to parse the text field contents.
*/
class NumberFormatFrame extends JFrame
{
private Locale[] locales;
private double currentNumber;
private JComboBox<String> localeCombo = new JComboBox<>();
private JButton parseButton = new JButton("Parse");
private JTextField numberText = new JTextField(30);
private JRadioButton numberRadioButton = new JRadioButton("Number");
private JRadioButton currencyRadioButton = new JRadioButton("Currency");
private JRadioButton percentRadioButton = new JRadioButton("Percent");
private ButtonGroup rbGroup = new ButtonGroup();
private NumberFormat currentNumberFormat;
public NumberFormatFrame()
{
setLayout(new GridBagLayout());
ActionListener listener = new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
updateDisplay();
}
};
JPanel p = new JPanel();
addRadioButton(p, numberRadioButton, rbGroup, listener);
addRadioButton(p, currencyRadioButton, rbGroup, listener);
addRadioButton(p, percentRadioButton, rbGroup, listener);
add(new JLabel("Locale:"), new GBC(0, 0).setAnchor(GBC.EAST));
add(p, new GBC(1, 1));
add(parseButton, new GBC(0, 2).setInsets(2));
add(localeCombo, new GBC(1, 0).setAnchor(GBC.WEST));
add(numberText, new GBC(1, 2).setFill(GBC.HORIZONTAL));
locales = (Locale[]) NumberFormat.getAvailableLocales().clone();
Arrays.sort(locales, new Comparator<Locale>()
{
public int compare(Locale l1, Locale l2)
{
return l1.getDisplayName().compareTo(l2.getDisplayName());
}
});
for (Locale loc : locales)
localeCombo.addItem(loc.getDisplayName());
localeCombo.setSelectedItem(Locale.getDefault().getDisplayName());
currentNumber = 123456.78;
updateDisplay();
localeCombo.addActionListener(listener);
parseButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
String s = numberText.getText().trim();
try
{
Number n = currentNumberFormat.parse(s);
if (n != null)
{
currentNumber = n.doubleValue();
updateDisplay();
}
else
{
numberText.setText("Parse error: " + s);
}
}
catch (ParseException e)
{
numberText.setText("Parse error: " + s);
}
}
});
pack();
}
/**
* Adds a radio button to a container.
* @param p the container into which to place the button
* @param b the button
* @param g the button group
* @param listener the button listener
*/
public void addRadioButton(Container p, JRadioButton b, ButtonGroup g, ActionListener listener)
{
b.setSelected(g.getButtonCount() == 0);
b.addActionListener(listener);
g.add(b);
p.add(b);
}
/**
* Updates the display and formats the number according to the user settings.
*/
public void updateDisplay()
{
Locale currentLocale = locales[localeCombo.getSelectedIndex()];
currentNumberFormat = null;
if (numberRadioButton.isSelected()) currentNumberFormat = NumberFormat
.getNumberInstance(currentLocale);
else if (currencyRadioButton.isSelected()) currentNumberFormat = NumberFormat
.getCurrencyInstance(currentLocale);
else if (percentRadioButton.isSelected()) currentNumberFormat = NumberFormat
.getPercentInstance(currentLocale);
String n = currentNumberFormat.format(currentNumber);
numberText.setText(n);
}
}