Hi, I am relatively new to applets, hence having some problems when it comes to handling event driven code.
Below is a java applet code. All I want is when the button called "Example" is pressed, the three textfields, tf1, tf2 and tf3 should show the values 5000, 6 and 8. will someone please help, thanks for the time.
import java.awt.*; import javax.swing.*; import java.awt.event.*;
import java.text.DecimalFormat;
public class money_time extends JApplet implements ActionListener{
JTextField tf1, tf2, tf3, tf4;
JLabel label1, label2, label3, label4;
JButton b1, b2;
public void init(){
setLayout( new FlowLayout());
label1 = new JLabel( " Enter deposit amount" );
tf1 = new JTextField( 10 );
label2 = new JLabel( " Enter interest rate" );
tf2 = new JTextField( 10 );
label3 = new JLabel( " Enter number of years" );
tf3 = new JTextField( 10 );
label4 = new JLabel( " Future Amount" );
tf4 = new JTextField( 10 );
b1 = new JButton( "Enter" );
b1.addActionListener( this );
b2 = new JButton( "Example" );
b2.addActionListener( this );
add(label1); add(label2); add(label3); add(label4);
add(tf1); add(tf2); add(tf3); add(tf4);
add(b1); add(b2);
}
public void actionPerformed( ActionEvent ae ) {
Object obj = ae.getSource( );
String a = "5000", b = "6", c = "8";
String st1 = tf1.getText();
String st2 = tf2.getText();
String st3 = tf3.getText();
double p = Double.parseDouble(st1);
double i = Double.parseDouble(st2);
double n = Double.parseDouble(st3);
double f = p*Math.pow(1+i/100, n);
DecimalFormat formatter = new DecimalFormat("0.00");
if (obj == b2){
tf1.setText(String.valueOf(a));
tf2.setText(String.valueOf(b));
tf3.setText(String.valueOf(c));
}
else if (obj == b1)
tf4.setText(formatter.format(f));
}
}