Hey everyone, I have no experience with writing programs. This is my first program and I'm sort of "winging it". I have a problem converting from fahrenheit to celsius, I can convert from celsius to fahrenheit fine though. The bolded part is where I'm having troubles. Here is what I've got:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* TemperatureConverterGUI.java
*
* Created on Jan 28, 2010, 9:24:37 PM
*/
package learn;
/**
*
* @author Matt
*/
public class TemperatureConverterGUI extends javax.swing.JFrame {
/** Creates new form TemperatureConverterGUI */
public TemperatureConverterGUI() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
celsiusTempTextField = new javax.swing.JTextField();
celsiusLabel = new javax.swing.JLabel();
convertButton1 = new javax.swing.JButton();
fahrenheitLabel = new javax.swing.JLabel();
fahrenheitTempTextField = new javax.swing.JTextField();
fahrenheitLabel2 = new javax.swing.JLabel();
celsiusLabel2 = new javax.swing.JLabel();
convertButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Temperature Converter");
celsiusLabel.setText("Celsius");
convertButton1.setText("Convert");
convertButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
convertButton1ActionPerformed(evt);
}
});
fahrenheitLabel.setText("Fahrenheit");
fahrenheitLabel2.setText("Fahrenheit");
celsiusLabel2.setText("Celsius");
convertButton2.setText("Convert");
convertButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
convertButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(celsiusTempTextField, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(convertButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(celsiusLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(fahrenheitLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(77, 77, 77)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(convertButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(fahrenheitTempTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE))
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(celsiusLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(fahrenheitLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(110, Short.MAX_VALUE))
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {celsiusTempTextField, convertButton1, convertButton2, fahrenheitTempTextField});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(celsiusTempTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(celsiusLabel)
.addComponent(fahrenheitTempTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(fahrenheitLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(convertButton1)
.addComponent(fahrenheitLabel)
.addComponent(celsiusLabel2)
.addComponent(convertButton2))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {celsiusTempTextField, convertButton1, convertButton2, fahrenheitTempTextField});
pack();
}// </editor-fold>
private void convertButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//Parse degrees Celsius as a double and convert to Fahrenheit.
int tempFahr = (int)((Double.parseDouble(celsiusTempTextField.getText()))
* 1.8 + 32);
fahrenheitLabel.setText(tempFahr + " Fahrenheit");
// TODO add your handling code here:
}
private void convertButton2ActionPerformed(java.awt.event.ActionEvent evt) [B]{
//Parse degrees Fahrenheit as a double and convert to Celsius.
int tempCels = (int) (fahrenheitTempTextField.getText())
((5.0 / 9) * (fahrenheitTempTextField - 32));
celsiusLabel2.setText(tempCels + " Celsius");
// TODO add your handling code here:
} [/B]
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TemperatureConverterGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel celsiusLabel;
private javax.swing.JLabel celsiusLabel2;
private javax.swing.JTextField celsiusTempTextField;
private javax.swing.JButton convertButton1;
private javax.swing.JButton convertButton2;
private javax.swing.JLabel fahrenheitLabel;
private javax.swing.JLabel fahrenheitLabel2;
private javax.swing.JTextField fahrenheitTempTextField;
// End of variables declaration
}
The bolded is where I'm having problems. What would I have to do to fix this problem? Thanks for all the help!