Hi qaromi,
I have made some changes in the program. Please first compile and execute it. I have commented in front of the lines where i have made the changes. Please go through it.
package projek;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class loan extends JFrame implements ActionListener{
JButton calculate;
JTextField pinjaman, interest;
double hasil;
JLabel loanAmount,interesting,jawapan; //See here i have Declared all the labels globally as we need them for
//the whole class i.e. for all its methods.
loan(){
Container kotak=getContentPane();
kotak.setLayout(new GridLayout(3,3));
loanAmount=new JLabel("Jumlah pinjaman");
kotak.add(loanAmount);
pinjaman=new JTextField(10);
kotak.add(pinjaman);
interesting=new JLabel("Faedah(Riba)");
kotak.add(interesting);
interest=new JTextField(10);
kotak.add(interest);
calculate=new JButton("Kira");
kotak.add(calculate);
calculate.addActionListener(this);
jawapan=new JLabel("Result: TOTAL LOAN WITH INTEREST IS "+hasil);
kotak.add(jawapan);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("LOAN CALCULATION");
pack();
show();
}
public void actionPerformed(ActionEvent e){
Object butangtekan=e.getSource();
if(butangtekan==calculate){
try{
double untukloan=Double.parseDouble(pinjaman.getText());
double faedah=Double.parseDouble(interest.getText()), riba; //See here i have removed the hasil variable
// as it is already declared globally. so no need
riba=(faedah*untukloan)/100;
hasil=riba+untukloan;
jawapan.setText("Result: TOTAL LOAN WITH INTEREST IS "+hasil); //setText(String) is a method which
// sets the the given text in the given
//component
}
catch(Exception a){
JOptionPane.showMessageDialog(null, "Wrong input, must be numeric", "Error",JOptionPane.ERROR_MESSAGE);
}
}
}
public static void main(String args[]){
loan n=new loan();
}
}
Regards,
guptapr