Source Code Cross Referenced for CelsiusConverter.java in  » Testing » abbot-1.0.1 » example » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » abbot 1.0.1 » example 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.