Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import java.util.Scanner;
- import javax.swing.*;
- public class TempConverterGUI extends JFrame {
- private TempConverter converter;
- public TempConverterGUI() {
- converter = new TempConverter(TempConverter.TEMP_TYPE.Celsius);
- initComponents();
- }
- private void convertButtonActionPerformed(ActionEvent evt) {
- getConvertedTemp();
- }
- private void tempTextFieldMouseClicked(MouseEvent evt) {
- tempTextField.setText("");
- }
- private void tempTextFieldKeyReleased(KeyEvent evt) {
- if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
- getConvertedTemp();
- }
- }
- private void convertButtonKeyReleased(KeyEvent evt) {
- if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
- getConvertedTemp();
- }
- }
- private void tempsComboBoxItemStateChanged(ItemEvent evt) {
- String tempStr = tempsComboBox.getSelectedItem().toString();
- tempTextField.setText("Temperature");
- if (tempStr.equals("Convert Celsius to Fahrenheit")) {
- converter = new TempConverter(TempConverter.TEMP_TYPE.Celsius);
- }
- else if (tempStr.equals("Convert Fahrenheit to Celsius")) {
- converter = new TempConverter(TempConverter.TEMP_TYPE.Fahrenheit);
- }
- else {
- throw new TempConverter.TempTypeDoesntExistException("Temperature type from combo box isn't supported.");
- }
- convertFromLabel.setText(converter.fromTempTypeStr());
- convertToLabel.setText(converter.toTempTypeStr());
- tempTextField.setToolTipText("Enter a temperature in degrees " + converter.fromTempTypeStr());
- }
- private void getConvertedTemp() {
- double fromTemp = 0.0;
- int toTemp = 0;
- boolean validInput = false;
- // Parse degrees Celsius asa double and convert to Fahrenheit.
- try {
- fromTemp = new Scanner(tempTextField.getText()).nextDouble();
- validInput = true;
- }
- catch (java.util.InputMismatchException e) {
- System.out.println("**ERROR**: Entered temperature isn't valid.");
- }
- catch (java.util.NoSuchElementException e) {
- System.out.println("**ERROR**: No temperature entered.");
- }
- if (validInput) {
- toTemp = converter.convertTemp(fromTemp);
- convertToLabel.setText(toTemp + " " + converter.toTempTypeStr());
- }
- else {
- convertToLabel.setText(converter.toTempTypeStr());
- }
- pack();
- }
- /**
- * This method is called from within the constructor to initialize the Netbeans form.
- */
- private void initComponents() {
- convertFromLabel = new JLabel();
- convertToLabel = new JLabel();
- convertButton = new JButton();
- tempTextField = new JTextField();
- tempsComboBox = new JComboBox();
- setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- setTitle("Temperature Converter");
- convertFromLabel.setLabelFor(convertFromLabel);
- convertFromLabel.setText("Celsius");
- convertToLabel.setText("Fahrenheit");
- convertButton.setText("Convert");
- convertButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
- convertButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent evt) {
- convertButtonActionPerformed(evt);
- }
- });
- convertButton.addKeyListener(new KeyAdapter() {
- public void keyReleased(KeyEvent evt) {
- convertButtonKeyReleased(evt);
- }
- });
- tempTextField.setText("Temperature");
- tempTextField.setToolTipText("Enter a temperature in degrees Celsius");
- tempTextField.addMouseListener(new MouseAdapter() {
- public void mouseClicked(MouseEvent evt) {
- tempTextFieldMouseClicked(evt);
- }
- });
- tempTextField.addKeyListener(new KeyAdapter() {
- public void keyReleased(KeyEvent evt) {
- tempTextFieldKeyReleased(evt);
- }
- });
- tempsComboBox.setMaximumRowCount(2);
- tempsComboBox.setModel(new DefaultComboBoxModel(new String[] { "Convert Celsius to Fahrenheit", "Convert Fahrenheit to Celsius" }));
- tempsComboBox.addItemListener(new ItemListener() {
- public void itemStateChanged(ItemEvent evt) {
- tempsComboBoxItemStateChanged(evt);
- }
- });
- GroupLayout layout = new GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(convertButton)
- .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(convertToLabel)
- .addGap(0, 0, Short.MAX_VALUE))
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(tempTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(convertFromLabel))
- .addComponent(tempsComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
- .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
- );
- layout.linkSize(SwingConstants.HORIZONTAL, new Component[] {convertButton, tempTextField});
- layout.setVerticalGroup(
- layout.createParallelGroup(GroupLayout.Alignment.LEADING)
- .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(tempsComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addComponent(tempTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addComponent(convertFromLabel))
- .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
- .addComponent(convertButton)
- .addComponent(convertToLabel))
- .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- pack();
- }
- /**
- * @param args Command-line arguments aren't currently supported
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (UIManager.LookAndFeelInfo info
- : UIManager.getInstalledLookAndFeels())
- {
- if ("Nimbus".equals(info.getName())) {
- UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
- | UnsupportedLookAndFeelException ex)
- {
- java.util.logging.Logger.getLogger(TempConverterGUI.class.getName())
- .log(java.util.logging.Level.SEVERE, null, ex);
- }
- /* Create and display the form */
- EventQueue.invokeLater(() -> {
- new TempConverterGUI().setVisible(true);
- });
- }
- // Variables declaration - do not modify
- private JButton convertButton;
- private JLabel convertFromLabel;
- private JLabel convertToLabel;
- private JTextField tempTextField;
- private JComboBox tempsComboBox;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment