Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Help Please.. Labels jumping after button click/refresh

843807Feb 14 2003 — edited Feb 17 2003
Hi. My JLabels seem to jump around after any button click or refresh. This was supposed to be a simple program. I have included the code since it is short. This is a simple program to calculate the interest on a loan. A text file called money.txt is needed to run the program. It should containt three numbers on the first three lines. Thanks for the help.


import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Money extends JFrame implements ActionListener{
double currentTotal;
double interest;
double moneyOwed;
double interestPaid;
JButton makePayment;
JLabel label, label2;
public Money(){
super("Pay Me Back");
Container c = getContentPane();
c.setBackground(Color.orange);
setResizable(false);
setSize(520,200);
show();

try{

FileInputStream inStream = new FileInputStream("money.txt");
InputStreamReader read= new InputStreamReader(inStream);
BufferedReader reader= new BufferedReader(read);

String temp = reader.readLine();
Double d = new Double(temp);
currentTotal = d.doubleValue();
temp = reader.readLine();
d = new Double(temp);
moneyOwed=d.doubleValue();
temp = reader.readLine();
d = new Double(temp);
interestPaid=d.doubleValue();
reader.close();

makePayment = new JButton("Make Monthly Payment");
makePayment.setSize(175,30);
makePayment.setLocation(150,120);
makePayment.addActionListener(this);
c.add(makePayment);

// I have tried two labels and have combined them into one label, at least one is always jumping around.
// I have tried resizing and relocating after the button click.

label= new JLabel("Total amount owed: "+moneyOwed);
label.setSize(250,30);
label.setFont(new Font("Times Roman",2,15));
label.setLocation(150,10);
c.add(label);

label2= new JLabel("Total interest paid: "+interestPaid);
label2.setSize(250,30);
label2.setFont(new Font("Times Roman",2,15));
label2.setLocation(150,60);
c.add(label2);
c.repaint();
}catch(IOException e){
System.out.println(e);
}
}
public void actionPerformed(ActionEvent e){
//if(e.getSource()==makePayment){
String z = JOptionPane.showInputDialog(null,"Enter an amount: XXX.XX.","Make Payment",JOptionPane.PLAIN_MESSAGE);
if(z==null)
return;
Double d = new Double(z);
double payment=d.doubleValue();
try{
interest = currentTotal*(.03/12);
interest*=100;
interest=Math.ceil(interest);
interest/=100;
currentTotal += interest;
currentTotal*=100;
currentTotal=Math.ceil(currentTotal);
currentTotal/=100;
moneyOwed -= (payment-interest);
moneyOwed*=100;
moneyOwed=Math.ceil(moneyOwed);
moneyOwed/=100;
interestPaid+=interest;
interestPaid*=100;
interestPaid=Math.ceil(interestPaid);
interestPaid/=100;

label.setText("Total amount owed: "+moneyOwed); //resets the text for the two buttons to represent the new values
label2.setText("Total interest paid: "+interestPaid); // this is the only time, I reference these in the button click

File thisFile = new File("money.ben");
thisFile.delete();
FileOutputStream output = new FileOutputStream(thisFile);
PrintWriter printer = new PrintWriter(output);

printer.println(currentTotal);
printer.println(moneyOwed);
printer.println(interestPaid);
printer.close();


getContentPane().repaint();2
}catch(Exception a){System.out.println(a);}
}
public static void main(String[] args){
new Money();
}



}

Comments

Processing
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Mar 17 2003
Added on Feb 14 2003
3 comments
63 views