3 Replies - 1436 Views - Last Post: 07 August 2011 - 07:04 PM Rate Topic: -----

#1 volvera215  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 08-March 11

Temperature Conversion Program --using an inner class

Posted 07 August 2011 - 06:23 PM

OK so I have exhausted my efforts and really need some assistance...

I'm trying to get the below code to execute by using an inner class but am having issues when clicking the buttons...like its not excepting my inputs or something. If I take out the inner class function (lines 110 & 111)the program works successfully. However the assignment asks "Event handling should be set up so that clicking on any one of the six buttons generates an event which your program handles. You must use an inner class to set up your event handling. You can display the result in an output text field or in a JLabel. Your program should accurately convert from Fahrenheit, Celsius, Kelvin to Fahrenheit, Celsius, Kelvin. NOTE: Only the selected conversion is displayed in the output area!!! When the conversion selection changes, the output area should change to show only the new result. The output should display three digits after the decimal point." Below is the code:

package temp1;

import javax.swing.JFrame;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.text.DecimalFormat;

@SuppressWarnings("serial")
public class  Temp1 extends JFrame 
	implements ActionListener {
private JButton ftocbutton;
private JButton ctofbutton; 
private JButton ktocbutton; 
private JButton ctokbutton;
private JButton ktofbutton; 
private JButton ftokbutton;

@SuppressWarnings("unused")
private JFrame frame;

@SuppressWarnings("unused")
private JPanel panel;

private final JTextField infield = new JTextField(7);
private final JTextField outfield = new JTextField(7);
private final JLabel inlabel = new JLabel("Enter Input Value Here:");
private final JLabel outlabel = new JLabel("Converted Temperature Output:");



	public static void main(final String[] args) {
		
		final Temp1 frame = new Temp1();
		frame.createFrame();
		frame.createftocbutton();
		frame.createctofbutton();
		frame.createktocbutton();
		frame.createctokbutton();
		frame.createktofbutton();
		frame.createftokbutton();
		frame.addLabels();
}

	public void createFrame() {
		final Container window = getContentPane();
		window.setLayout(new FlowLayout() ); //layout manager
		setSize(600, 400); //sets the window size 
		setTitle("Temperature Converter Application");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
		
		setVisible(true); //allows for manual sizing 
		 
		panel = new JPanel(new GridLayout(1,6));
		add(new JLabel("Temperature Conversions:"+ " C: Celsius"+ " F: Farhrenheit"+ " K: Kelvin")); 
}

ActionListener buttonclickListener = new ButtonActionListener( );
	
//F to C button
public void createftocbutton() {
	ftocbutton = new JButton("F-to-C");
	add(ftocbutton);
	ftocbutton.addActionListener((ActionListener) this);//using "this" cmd to activate on-click
}

//C to F button
public void createctofbutton() {
	ctofbutton = new JButton("C-to-F");
	add(ctofbutton); 
	ctofbutton.addActionListener((ActionListener) this);//using "this" cmd to activate on-click
}

//K to C button
public void createktocbutton() {
	ktocbutton = new JButton("K-to-C");
	add(ktocbutton); 
	ktocbutton.addActionListener((ActionListener) this);//using "this" cmd to activate on-click
}

//C to K button
public void createctokbutton() {
	ctokbutton = new JButton("C-to-K");
	add(ctokbutton); 
	ctokbutton.addActionListener((ActionListener) this);//using "this" cmd to activate on-click
}

//K to F button
public void createktofbutton() {
	ktofbutton = new JButton("K-to-F");
	add(ktofbutton); 
	ktofbutton.addActionListener((ActionListener) this);//using "this" cmd to activate on-click
}

//F to K button
public void createftokbutton() {
	ftokbutton = new JButton("F-to-K");
	add(ftokbutton);
	ftokbutton.addActionListener((ActionListener) this);//using "this" cmd to activate on-click
}

public void addLabels() {
	add(inlabel);
	add(infield);
	add(outlabel);
	add(outfield);
	outfield.setEditable(false);//makes the output non-editable
}

@SuppressWarnings("unused")
private class ButtonActionListener implements ActionListener{ 	
	
public void actionPerformed(final ActionEvent i) { 
		DecimalFormat three = new DecimalFormat("0.000");	
		int temp;
		double newtemp = 0;
		String inputString;
		inputString = infield.getText();
		temp = Integer.parseInt(inputString);
	
	if(i.getSource() == ftocbutton) {
		newtemp = (5.0/9.0) * (temp - 32);//F to C formula
}
		else if(i.getSource() == ctofbutton) {
		newtemp = ((9.0/5.0) * temp) + 32;//C to F formula
		}
			else if(i.getSource() == ktocbutton) {
				newtemp = temp - 273;//K to C formula
			}
				else if(i.getSource() == ctokbutton) {
					newtemp = temp + 273;//C to K formula
				}
					else if(i.getSource() == ktofbutton) {
						newtemp = ((temp - 273) * (9.0/5.0)) + 32;//K to F formula
					}
	else {
		newtemp = (5.0/9.0) * (temp - 32) + 273;//F to K formula
}
	outfield.setText(" "+ three.format(newtemp));
	}
 
}


public void actionPerformed(ActionEvent i) {
	
	
}

}




Is This A Good Question/Topic? 0
  • +

Replies To: Temperature Conversion Program --using an inner class

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Temperature Conversion Program --using an inner class

Posted 07 August 2011 - 06:31 PM

You should declare

ActionListener buttonclickListener;

in your instance variables.

In your createFrame() method add

buttonclickListener = new ButtonActionListener();

and in all the place where you add an actionListener like in

ctofbutton.addActionListener((ActionListener) this);

replace the "this" by

ctofbutton.addActionListener(buttonclickListener);

and remove that line in the middle of nowhere

ActionListener buttonclickListener = new ButtonActionListener( );
Was This Post Helpful? 0
  • +
  • -

#3 volvera215  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 08-March 11

Re: Temperature Conversion Program --using an inner class

Posted 07 August 2011 - 06:43 PM

View Postpbl, on 07 August 2011 - 06:31 PM, said:

You should declare

ActionListener buttonclickListener;

in your instance variables.

In your createFrame() method add

buttonclickListener = new ButtonActionListener();

and in all the place where you add an actionListener like in

ctofbutton.addActionListener((ActionListener) this);

replace the "this" by

ctofbutton.addActionListener(buttonclickListener);

and remove that line in the middle of nowhere

ActionListener buttonclickListener = new ButtonActionListener( );



I made the changes you suggested and it works perfectly again...thanks so much. I just couldn't figure it out.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Temperature Conversion Program --using an inner class

Posted 07 August 2011 - 07:04 PM

Glad I could help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1