Friday 25 April 2014

Having trouble with Java project due tonight, can anyone plz take a look

Posted by Саша 01:52, under | No comments


Having trouble with Java project due tonight, can anyone plz take a look? Having trouble with Java project due tonight, can anyone plz take a look?

I've got an assignment to write a java app that will convert celsius to fahrenheit (or reverse), and I'm having trouble getting my JTextField objects (FText and CText) being recognized by ActionHandler, and putting their input into integer variables so I can use them in the calculations (my first program with event handling and GUI's, I'm still absorbing a lot of information all at once)

The app is a box with a fahrenheit icon with "Fahrenheit" below it, then a JTextField to the left that receives the temperature, and the same setup for celsius. When a value is put in one JTextField and user presses enter, the converted value appears in the other box.

I wrote the professor two days ago about this, knowing that I may not have it figured out by the due date (midnight tonight). She wrote me back 3 hours ago and basically re wrote my whole setup. I'm new to this, but I want it to come out my way, if I wanted to I could have copied code from examples in the book and been done, I actually want to know how to do this.

So I appreciate any help, here's my code, yahoo answers will likely annhilate the formatting but it isn't too long, I hope it's clear enough what some of my nonsense lines of code mean.

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.Icon;
import javax.swing.ImageIcon;

public class TempConverter extends JFrame
{
public TempConverter()
{
super( "Convert Celsius to American!" );
setLayout( new FlowLayout() );

//Fahrenheit box
JTextField FText = new JTextField( 5 );
add( FText );

//Fahrenheit label
Icon FIcon = new ImageIcon( getClass().getResource( "FIcon.jpg" ) );
JLabel FLabel = new JLabel( "Fahrenheit", FIcon, SwingConstants.LEFT );
FLabel.setToolTipText( "Do something already!" );
FLabel.setVerticalTextPosition( SwingConstants.BOTTOM );
add( FLabel );

//Celcius box
JTextField CText = new JTextField( 5 );
add ( CText );

Icon CIcon = new ImageIcon( getClass().getResource( "CIcon.jpg" ) );
JLabel CLabel = new JLabel( "Celsius", CIcon, SwingConstants.LEFT );
CLabel.setToolTipText( "Do something already!" );
CLabel.setVerticalTextPosition( SwingConstants.BOTTOM );
add( CLabel );

TextFieldHandler handler = new TextFieldHandler();
FText.addActionListener( handler );
CText.addActionListener( handler );
}
}

class TextFieldHandler implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
//if fahrenheit is known
if ( event.getSource() == FText )
String FString = event.getActionCommand();
int F = Integer.parseInt( FString );
C = (5/9)*(F-32)
CText.setText( "%d", C );

//if celsius is known
if (event.getSource() == CText )
String CString = event.getActionCommand();
int C = Integer.parseInt( CString );
F = (C + 32)/(5/9);
event.getActionCommand();
FText.setText( "%d", F );
}
}

import javax.swing.JFrame;

public class TextFieldTest
{
public static void main( String args[] )
{
TempConverter tempFrame = new TempConverter();
tempFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
tempFrame.setSize( 350, 350 );
tempFrame.setVisible( true );
}
}


Other Answers:













0 коммент.:

Post a Comment