Yurttas/PL/OOL/Java/F/05/01/05/03/NumberFormat00.java

From ZCubes Wiki
Jump to navigation Jump to search
  1/**
  2 * @version 1.10 1999-10-14
  3 * @author Cay Horstmann
  4 */
  5
  6import java.awt.*;
  7import java.awt.event.*;
  8import java.text.*;
  9import java.util.*;
 10import javax.swing.*;
 11
 12public class NumberFormat00 {
 13  public static void main(String[] args) {
 14    JFrame frame = new NumberFormatFrame();
 15    frame.show();
 16  }
 17}
 18
 19class NumberFormatFrame extends JFrame {
 20  public NumberFormatFrame() {
 21    setSize(400, 200);
 22    setTitle("NumberFormat00");
 23
 24    addWindowListener(new WindowAdapter() {
 25      public void windowClosing(WindowEvent e) {
 26        System.exit(0);
 27      }
 28    });
 29
 30    getContentPane().setLayout(new GridBagLayout());
 31
 32    ActionListener listener = new ActionListener() {
 33      public void actionPerformed(ActionEvent event) {
 34        updateDisplay();
 35      }
 36    };
 37
 38    JPanel p = new JPanel();
 39
 40    addCheckBox(p, numberCheckBox, cbGroup, listener, true);
 41    addCheckBox(p, currencyCheckBox, cbGroup, listener, false);
 42    addCheckBox(p, percentCheckBox, cbGroup, listener, false);
 43
 44    GridBagConstraints gbc = new GridBagConstraints();
 45
 46    gbc.fill = GridBagConstraints.NONE;
 47
 48    gbc.anchor = GridBagConstraints.EAST;
 49
 50    add(new JLabel("Locale"), gbc, 0, 0, 1, 1);
 51    add(p, gbc, 1, 1, 1, 1);
 52    add(parseButton, gbc, 0, 2, 1, 1);
 53
 54    gbc.anchor = GridBagConstraints.WEST;
 55
 56    add(localeCombo, gbc, 1, 0, 1, 1);
 57
 58    gbc.fill = GridBagConstraints.HORIZONTAL;
 59
 60    add(numberText, gbc, 1, 2, 1, 1);
 61
 62    locales = NumberFormat.getAvailableLocales();
 63
 64    for(int i=0; i<locales.length; i++)
 65      localeCombo.addItem(locales[i].getDisplayName());
 66
 67    localeCombo.setSelectedItem(Locale.getDefault().getDisplayName());
 68
 69    currentNumber = 123456.78;
 70    updateDisplay();
 71
 72    localeCombo.addActionListener(listener);
 73
 74    parseButton.addActionListener(new ActionListener() {
 75      public void actionPerformed(ActionEvent event) {
 76        String s = numberText.getText();
 77        try {
 78          Number n = currentNumberFormat.parse(s);
 79          if(n != null) {
 80            currentNumber = n.doubleValue();
 81            updateDisplay();
 82          }
 83          else
 84            numberText.setText("Parse error: " + s);
 85        }
 86        catch(ParseException e) {
 87          numberText.setText("Parse error: " + s);
 88        }
 89      }
 90    });
 91  }
 92
 93  public void add(Component c,
 94                  GridBagConstraints gbc,
 95                  int x,
 96                  int y,
 97                  int w,
 98                  int h) {
 99    gbc.gridx = x;
100    gbc.gridy = y;
101    gbc.gridwidth = w;
102    gbc.gridheight = h;
103    getContentPane().add(c, gbc);
104  }
105
106  public void addCheckBox(Container p,
107                          JCheckBox checkBox,
108                          ButtonGroup g,
109                          ActionListener listener,
110                          boolean v) {
111    checkBox.setSelected(v);
112    checkBox.addActionListener(listener);
113    g.add(checkBox);
114    p.add(checkBox);
115  }
116
117  public void updateDisplay() {
118    Locale currentLocale = locales[localeCombo.getSelectedIndex()];
119    currentNumberFormat = null;
120    if(numberCheckBox.isSelected())
121      currentNumberFormat = NumberFormat.getNumberInstance(currentLocale);
122    else if(currencyCheckBox.isSelected())
123      currentNumberFormat = NumberFormat.getCurrencyInstance(currentLocale);
124    else if(percentCheckBox.isSelected())
125      currentNumberFormat = NumberFormat.getPercentInstance(currentLocale);
126    String n = currentNumberFormat.format(currentNumber);
127    numberText.setText(n);
128  }
129
130  private Locale[] locales;
131
132  private double currentNumber;
133
134  private JComboBox localeCombo = new JComboBox();
135  private JButton parseButton = new JButton("Parse");
136  private JTextField numberText = new JTextField(30);
137  private JCheckBox numberCheckBox = new JCheckBox("Number");
138  private JCheckBox currencyCheckBox = new JCheckBox("Currency");
139  private JCheckBox percentCheckBox = new JCheckBox("Percent");
140  private ButtonGroup cbGroup = new ButtonGroup();
141  private NumberFormat currentNumberFormat;
142}