Java Genesis

Hints for Chapter 10: Graphical Components

Problem 17: implementing an interface

Here is our code for the revised ConversionFrame class, with only the method actionPerformed missing:

   import genesis.*;
   import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;

   public class ConversionFrame
     extends ExitFrame implements ActionListener {
	/*
  	Convert Fahrenheit to Centigrade and conversely.
  	*/
   
  	// instance variables
	private JTextField fah, cent;
	
	// constructor	
	public ConversionFrame ( ) {
		setTitle("Fahrenheit <-> Centigrade");
		setBounds(100, 100, 350, 250);
		Font f = new Font("SansSerif", Font.BOLD, 30);
		fah = new JTextField("0.0", 9);
		fah.setFont(f);
		cent = new JTextField("0.0", 9);
		cent.setFont(f);
		JLabel fahLabel = new JLabel("deg F:", JLabel.RIGHT);
		fahLabel.setFont(f);
		JLabel centLabel = new JLabel("deg C:", JLabel.RIGHT);
		centLabel.setFont(f);
		JButton fahToCent = new JButton("convert F to C");
		fahToCent.setFont(f);
		JButton centToFah = new JButton("convert C to F");
		centToFah.setFont(f);
		fahToCent.addActionListener(this);
		centToFah.addActionListener(this);
		JPanel p = new JPanel();
		p.add(fahLabel);
		p.add(fah);
		p.add(centLabel);
		p.add(cent);
		p.add(fahToCent);
		p.add(centToFah);
		Container c = getContentPane();
		p.setBackground(Color.white);
		c.add(p);
 	}
   }
Try to code the method actionPerformed before looking at the last hint.


Next Hint