Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.08 KB | None | 0 0
  1. /**
  2.  * A simple Swing UI demonstrating the use of JButton, JTextField and JLabel.
  3.  * Code contributed by Satadip Dutta was labeled <code>v 1.3</code>, and was
  4.  * extended/revised by Tom Roche & Tim Wall.
  5.  *  
  6.  * @author Satadip Dutta
  7.  * @version $Id: CelsiusConverter.java,v 1.5 2005/01/05 18:34:47 twall Exp $
  8.  */
  9.  
  10. package converter;
  11.  
  12. import java.awt.BorderLayout;
  13. import java.awt.Dimension;
  14. import java.awt.GridLayout;
  15. import java.awt.event.ActionEvent;
  16. import java.awt.event.ActionListener;
  17.  
  18. import java.text.DecimalFormat;
  19. import java.text.MessageFormat;
  20.  
  21. import javax.swing.BorderFactory;
  22. import javax.swing.ButtonGroup;
  23. import javax.swing.JButton;
  24. import javax.swing.JFrame;
  25. import javax.swing.JLabel;
  26. import javax.swing.JMenu;
  27. import javax.swing.JMenuBar;
  28. import javax.swing.JPanel;
  29. import javax.swing.JRadioButtonMenuItem;
  30. import javax.swing.JTextField;
  31. import javax.swing.SwingConstants;
  32. import javax.swing.UIManager;
  33.  
  34. public class CelsiusConverter extends JPanel {
  35.     private static final long serialVersionUID = 2407246206181752648L;
  36.  
  37.     private static final int NPRECISIONS = 5;
  38.     private int precision;
  39.     private JLabel celsiusLabel;
  40.     private JLabel fahrLabel;
  41.     private JTextField inputText;
  42.  
  43.     // Constructor
  44.     public CelsiusConverter() {
  45.         // Create the container.
  46.         final int MARGIN = 2;
  47.         setLayout(new GridLayout(0, 2, MARGIN, MARGIN));
  48.         setBorder(BorderFactory.createEmptyBorder(MARGIN, MARGIN, MARGIN, MARGIN));
  49.  
  50.         JPanel left = new JPanel(new BorderLayout());
  51.         left.setBorder(BorderFactory.createEtchedBorder());
  52.         add(left);
  53.  
  54.         JPanel right = new JPanel(new BorderLayout());
  55.         right.setBorder(BorderFactory.createEtchedBorder());
  56.         add(right);
  57.  
  58.         // Create widgets.
  59.         inputText = new JTextField(2);
  60.         final JButton convertTemp = new JButton(lookupString("conversion.button.text")); //$NON-NLS-1$
  61.         celsiusLabel = new JLabel(lookupString("input.label.text"), SwingConstants.LEFT); //$NON-NLS-1$
  62.         fahrLabel = new JLabel(lookupString("output.label.text"), SwingConstants.LEFT); //$NON-NLS-1$
  63.  
  64.         // Add widgets to container.
  65.         left.add(inputText, BorderLayout.NORTH);
  66.         left.add(convertTemp, BorderLayout.SOUTH);
  67.  
  68.         right.add(celsiusLabel, BorderLayout.NORTH);
  69.         right.add(fahrLabel, BorderLayout.SOUTH);
  70.  
  71.         celsiusLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  72.         fahrLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  73.  
  74.         // Listen to events from Convert button.
  75.         convertTemp.addActionListener(new ActionListener() {
  76.                 public void actionPerformed(ActionEvent event) {
  77.                     updateLabels();
  78.                 }
  79.             });
  80.         inputText.addActionListener(new ActionListener() {
  81.                 public void actionPerformed(ActionEvent e) {
  82.                     convertTemp.doClick();
  83.                 }
  84.             });
  85.     }
  86.  
  87.     /** Convert the given Celsius value to Fahrenheit. */
  88.     public static double convert(double celsius) {
  89.         return celsius * 9 / 5 + 32;
  90.     }
  91.  
  92.     // convenience
  93.     public static String lookupString(String key) {
  94.         return CelsiusConverterStrings.getString(key);
  95.     }
  96.    
  97.     // convenience, reused in tests
  98.     public static String formatOutput(String format, double value,
  99.                                       int precision) {
  100.         MessageFormat f = new MessageFormat(format);
  101.         String pattern = "#";
  102.         if (precision > 0)
  103.             pattern += ".";
  104.         while (precision-- > 0)
  105.             pattern += "#";
  106.         DecimalFormat dfmt = new DecimalFormat(pattern);
  107.         return f.format(new Object[] { dfmt.format(value) });
  108.     }
  109.  
  110.     // convenience, reused in tests
  111.     public static String fahrenheitOutput(double value, int precision) {
  112.         return formatOutput(lookupString("F"), value, precision);
  113.     }
  114.  
  115.     // convenience, reused in tests
  116.     public static String celsiusOutput(double value, int precision) {
  117.         return formatOutput(lookupString("C"), value, precision);
  118.     }
  119.  
  120.     private void updateLabels() {
  121.         String in = inputText.getText();
  122.         try {
  123.            
  124.             // Convert degrees Celsius to Fahrenheit.
  125.             double celsius = Double.parseDouble(in);
  126.             if(celsius >= -273.15){
  127.                 celsiusLabel.setText(formatOutput(lookupString("C"),
  128.                                               celsius,
  129.                                               precision)); //$NON-NLS-1$
  130.            
  131.                 double fahr = convert(celsius);
  132.                 fahrLabel.setText(formatOutput(lookupString("F"), fahr,
  133.                                             precision)); //$NON-NLS-1$
  134.             }
  135.             else{
  136.                 celsiusLabel.setText(lookupString("input.label.text") + "NA");
  137.                 fahrLabel.setText(lookupString("output.label.text") + "NA");
  138.             }
  139.         } catch (Exception e){
  140.             inputText.selectAll();
  141.         }
  142.     }
  143.  
  144.     private JMenuBar createMenuBar() {
  145.  
  146.         JMenuBar menuBar = new JMenuBar();
  147.         JMenu menu = new JMenu(lookupString("menu.options"));
  148.         menuBar.add(menu);
  149.         JMenu submenu = new JMenu(lookupString("menu.precision"));
  150.         menu.add(submenu);
  151.         ActionListener listener = new ActionListener() {
  152.                 public void actionPerformed(ActionEvent e) {
  153.                     if (((JRadioButtonMenuItem)e.getSource()).isSelected()) {
  154.                         precision = Integer.parseInt(e.getActionCommand());
  155.                         updateLabels();
  156.                     }
  157.                 }
  158.             };
  159.         ButtonGroup group = new ButtonGroup();
  160.         for (int i=0;i < NPRECISIONS;i++) {
  161.             JRadioButtonMenuItem item =
  162.                 new JRadioButtonMenuItem(String.valueOf(i), i==precision);
  163.             item.setActionCommand(String.valueOf(i));
  164.             item.addActionListener(listener);
  165.             group.add(item);
  166.             submenu.add(item);
  167.         }
  168.  
  169.         return menuBar;
  170.     }
  171.  
  172.     /**
  173.      * Stick us in a <code>JFrame</code>.
  174.      * Reused in tests.
  175.      */
  176.     public void enframe(JFrame frame) {
  177.         frame.setTitle(lookupString("frame.title")); //$NON-NLS-1$
  178.         // Add the panel to the frame.
  179.         frame.setContentPane(this);
  180.         frame.setJMenuBar(createMenuBar());
  181.  
  182.         // Exit when the window is closed.
  183.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  184.  
  185.         frame.pack();
  186.         Dimension d = frame.getSize();
  187.         d.width = Math.max(d.width, 350);
  188.         frame.setSize(d);
  189.     }
  190.  
  191.     // main method
  192.     public static void main(String[] args) {
  193.         try {
  194.             UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
  195.         }
  196.         catch (Exception e) {
  197.             e.printStackTrace();
  198.             System.exit(1);
  199.         }
  200.         CelsiusConverter converter = new CelsiusConverter();
  201.         JFrame frame = new JFrame();
  202.         converter.enframe(frame);
  203.         frame.setVisible(true);
  204.     }
  205.  
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement