Coder's Guild Mailing List

Re: Java Programming

Posted by Muhammad Ali Shah on 2001-06-24

Hi,

I am not sure if this is what you wanted to do. See the "actionPerformed" 
method. I have been coding Java for quite some time, so I may be able to 
answer your questions.


import java.awt.*;
import java.awt.event.*;

public class Base extends Frame implements ActionListener {

	TextField input;
	TextField output;

	Base () {
		super ("Demo");
		input = new TextField ();
		output = new TextField ();
		Button go = new Button ("Go");

		this.setLayout (new GridLayout (0, 1, 10, 10));
		this.add (input);
		this.add (output);
		this.add (go);
		this.pack();
		this.setVisible (true);

		go.addActionListener (this);
	}


	public void actionPerformed (ActionEvent e) {
		int whole;
		double total;

		String text = input.getText();
		int dotLocation = text.lastIndexOf (".");
		if (dotLocation == -1) dotLocation = text.length();

		text = text.substring (0, dotLocation);
		whole = Integer.parseInt (text);
		total = Double.parseDouble (input.getText());

		text = "" + (total - whole);
		output.setText (text);
	}

	public static void main (String args[]) {
		new Base();
	}
}


Hope this helps....
<Ali/>