SRL Archive - Java Help and Tutorials

Celsius Converter


Richard

2009-02-24 13:19:35

Well, I'm very new to Java, and this is what a tutorial taught me to do :)

Pretty simple, just converts celsius to fahrenheit.

I may work on it and make it do the other way around.

EDIT: Found out how to make it an exacutable .jar so uploaded.

Yakman

2009-02-24 13:34:58

where is the program? :confused:

Richard

2009-02-24 15:39:48

Can't seem to make it work as a .jar file, can't put them on here :s

I'll just post the code for now.

n3ss3s

2009-02-25 12:04:04

I'll just post the code for now.


You only posted the jar…

Richard

2009-02-25 12:34:00

Originally I couldn't find how to post the .jar properly, but I sorted that so people didn't need a compiler, but if you want to see the code, here it is:



package learn;

/**
*
* @author Richard
*/
public class CelsiusConverter extends javax.swing.JFrame {

public CelsiusConverter() {
initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

tempTextField = new javax.swing.JTextField();
celsiusLabel = new javax.swing.JLabel();
convertButton = new javax.swing.JButton();
fahrenheitLabel = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Celsius Converter");

tempTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tempTextFieldActionPerformed(evt);
}
});

celsiusLabel.setText("Celsius");

convertButton.setText("Convert");
convertButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
convertButtonActionPerformed(evt);
}
});

fahrenheitLabel.setText("Fahrenheit");

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)
.addGroup(layout.createSequentialGroup()
.addComponent(tempTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(celsiusLabel))
.addGroup(layout.createSequentialGroup()
.addComponent(convertButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(fahrenheitLabel)))
.addContainerGap(26, Short.MAX_VALUE))
);

layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {convertButton, tempTextField});

layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tempTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(celsiusLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(convertButton)
.addComponent(fahrenheitLabel))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

pack();
}// </editor-fold>//GEN-END:initComponents

private void tempTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tempTextFieldActionPerformed

}//GEN-LAST:event_tempTextFieldActionPerformed

private void convertButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_convertButtonActionPerformed
int tempFahr = (int)((Double.parseDouble(tempTextField.getText()))
* 1.8 + 32);
fahrenheitLabel.setText(tempFahr + " Fahrenheit");
}//GEN-LAST:event_convertButtonActionPerformed

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CelsiusConverterGUI().setVisible(true);
}
});
}

// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel celsiusLabel;
private javax.swing.JButton convertButton;
private javax.swing.JLabel fahrenheitLabel;
private javax.swing.JTextField tempTextField;
// End of variables declaration//GEN-END:variables

}

Not very good I know, my first use of GUI, I'm very new to this, and I get lazy and don't learn much for a while :p

impiwimpi

2009-03-03 13:29:16

Since u said you're new to Java; what did you used to learn it?
Cause i'm also interested in learning Java…

Method

2009-03-03 22:31:22

Take a class, look at the official Java tutorials, start reading the Javadocs, read other tutorials on Google, etc. There's a variety of ways you can learn Java, just go ahead and pick one. :)

Mistran

2009-03-04 05:27:03

It looks like you've use a GUI builder in an IDE to make the interface. This is Ok at the beginning however I would encourage you to learn how to code a GUI yourself.

The code will be much more readable, and much more reusable (Reusability being an important concept in programming) and you will learn a lot more about what is going on.

Clishem

2009-04-07 12:31:23

Haha, a converter hmm.. One of the many 'pointless' things you can do with Java. Well done ;)

Method

2009-04-07 14:56:22

From: Clishem
Haha, a converter hmm.. One of the many 'pointless' things you can do with Java. Well done ;)


You can create this 'pointless' thing in almost any language.