001: /**
002: * A simple Swing UI demonstrating the use of JButton, JTextField and JLabel.
003: * Code contributed by Satadip Dutta was labeled <code>v 1.3</code>, and was
004: * extended/revised by Tom Roche & Tim Wall.
005: *
006: * @author Satadip Dutta
007: * @version $Id: CelsiusConverter.java 1419 2005-01-05 18:34:48Z twall $
008: */package example;
009:
010: import java.awt.BorderLayout;
011: import java.awt.Dimension;
012: import java.awt.GridLayout;
013: import java.awt.event.ActionEvent;
014: import java.awt.event.ActionListener;
015:
016: import java.text.DecimalFormat;
017: import java.text.MessageFormat;
018:
019: import javax.swing.BorderFactory;
020: import javax.swing.ButtonGroup;
021: import javax.swing.JButton;
022: import javax.swing.JFrame;
023: import javax.swing.JLabel;
024: import javax.swing.JMenu;
025: import javax.swing.JMenuBar;
026: import javax.swing.JPanel;
027: import javax.swing.JRadioButtonMenuItem;
028: import javax.swing.JTextField;
029: import javax.swing.SwingConstants;
030: import javax.swing.UIManager;
031:
032: public class CelsiusConverter extends JPanel {
033: private static final int NPRECISIONS = 5;
034: private int precision;
035: private JLabel celsiusLabel;
036: private JLabel fahrLabel;
037: private JTextField inputText;
038:
039: // Constructor
040: public CelsiusConverter() {
041: // Create the container.
042: final int MARGIN = 2;
043: setLayout(new GridLayout(0, 2, MARGIN, MARGIN));
044: setBorder(BorderFactory.createEmptyBorder(MARGIN, MARGIN,
045: MARGIN, MARGIN));
046:
047: JPanel left = new JPanel(new BorderLayout());
048: left.setBorder(BorderFactory.createEtchedBorder());
049: add(left);
050:
051: JPanel right = new JPanel(new BorderLayout());
052: right.setBorder(BorderFactory.createEtchedBorder());
053: add(right);
054:
055: // Create widgets.
056: inputText = new JTextField(2);
057: final JButton convertTemp = new JButton(
058: lookupString("conversion.button.text")); //$NON-NLS-1$
059: celsiusLabel = new JLabel(
060: lookupString("input.label.text"), SwingConstants.LEFT); //$NON-NLS-1$
061: fahrLabel = new JLabel(
062: lookupString("output.label.text"), SwingConstants.LEFT); //$NON-NLS-1$
063:
064: // Add widgets to container.
065: left.add(inputText, BorderLayout.NORTH);
066: left.add(convertTemp, BorderLayout.SOUTH);
067:
068: right.add(celsiusLabel, BorderLayout.NORTH);
069: right.add(fahrLabel, BorderLayout.SOUTH);
070:
071: celsiusLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5,
072: 5));
073: fahrLabel
074: .setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
075:
076: // Listen to events from Convert button.
077: convertTemp.addActionListener(new ActionListener() {
078: public void actionPerformed(ActionEvent event) {
079: updateLabels();
080: }
081: });
082: inputText.addActionListener(new ActionListener() {
083: public void actionPerformed(ActionEvent e) {
084: convertTemp.doClick();
085: }
086: });
087: }
088:
089: /** Convert the given Celsius value to Fahrenheit. */
090: public static double convert(double celsius) {
091: return celsius * 9 / 5 + 32;
092: }
093:
094: // convenience
095: public static String lookupString(String key) {
096: return CelsiusConverterStrings.getString(key);
097: }
098:
099: // convenience, reused in tests
100: public static String formatOutput(String format, double value,
101: int precision) {
102: MessageFormat f = new MessageFormat(format);
103: String pattern = "#";
104: if (precision > 0)
105: pattern += ".";
106: while (precision-- > 0)
107: pattern += "#";
108: DecimalFormat dfmt = new DecimalFormat(pattern);
109: return f.format(new Object[] { dfmt.format(value) });
110: }
111:
112: // convenience, reused in tests
113: public static String fahrenheitOutput(double value, int precision) {
114: return formatOutput(lookupString("F"), value, precision);
115: }
116:
117: // convenience, reused in tests
118: public static String celsiusOutput(double value, int precision) {
119: return formatOutput(lookupString("C"), value, precision);
120: }
121:
122: private void updateLabels() {
123: String in = inputText.getText();
124: try {
125: // Convert degrees Celsius to Fahrenheit.
126: double celsius = Double.parseDouble(in);
127: celsiusLabel.setText(formatOutput(lookupString("C"),
128: celsius, precision)); //$NON-NLS-1$
129: double fahr = convert(celsius);
130: fahrLabel.setText(formatOutput(lookupString("F"), fahr,
131: precision)); //$NON-NLS-1$
132: } catch (Exception e) {
133: inputText.selectAll();
134: }
135: }
136:
137: private JMenuBar createMenuBar() {
138:
139: JMenuBar menuBar = new JMenuBar();
140: JMenu menu = new JMenu(lookupString("menu.options"));
141: menuBar.add(menu);
142: JMenu submenu = new JMenu(lookupString("menu.precision"));
143: menu.add(submenu);
144: ActionListener listener = new ActionListener() {
145: public void actionPerformed(ActionEvent e) {
146: if (((JRadioButtonMenuItem) e.getSource()).isSelected()) {
147: precision = Integer.parseInt(e.getActionCommand());
148: updateLabels();
149: }
150: }
151: };
152: ButtonGroup group = new ButtonGroup();
153: for (int i = 0; i < NPRECISIONS; i++) {
154: JRadioButtonMenuItem item = new JRadioButtonMenuItem(String
155: .valueOf(i), i == precision);
156: item.setActionCommand(String.valueOf(i));
157: item.addActionListener(listener);
158: group.add(item);
159: submenu.add(item);
160: }
161:
162: return menuBar;
163: }
164:
165: /**
166: * Stick us in a <code>JFrame</code>.
167: * Reused in tests.
168: */
169: public void enframe(JFrame frame) {
170: frame.setTitle(lookupString("frame.title")); //$NON-NLS-1$
171: // Add the panel to the frame.
172: frame.setContentPane(this );
173: frame.setJMenuBar(createMenuBar());
174:
175: // Exit when the window is closed.
176: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
177:
178: frame.pack();
179: Dimension d = frame.getSize();
180: d.width = Math.max(d.width, 350);
181: frame.setSize(d);
182: }
183:
184: // main method
185: public static void main(String[] args) {
186: try {
187: UIManager.setLookAndFeel(UIManager
188: .getCrossPlatformLookAndFeelClassName());
189: } catch (Exception e) {
190: e.printStackTrace();
191: System.exit(1);
192: }
193: CelsiusConverter converter = new CelsiusConverter();
194: JFrame frame = new JFrame();
195: converter.enframe(frame);
196: frame.setVisible(true);
197: }
198:
199: }
|