Forum Stats

  • 3,814,524 Users
  • 2,258,880 Discussions
  • 7,892,765 Comments

Discussions

Help Please.. Labels jumping after button click/refresh

843807
843807 Member Posts: 46,582
edited Feb 17, 2003 1:14PM in Java Desktop Applications
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

  • 843807
    843807 Member Posts: 46,582
    I don't know the exact cause of the problem, but as a -not so nice but it works- solution, I propose to add the following line right before the first c.repaint() call:
    c.add(new JLabel());
  • 843807
    843807 Member Posts: 46,582
    This may sound silly but are you using a layout manager? If not, why not? Setting positions is difficult and keeping the controls in the right place will be difficult. When you use a layout manager, you can programmatically "lock" the labels. Using BorderLayouts and GridBagLayouts are terrific. Check out this link, it helped me a lot.

    http://developer.java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/shortcourse.html

    Just remember that you should decide how you want the labels to behave (should they resize and move or be fixed) and pick the layout that matches that behavior. Create a JPanel, set that layout and then add the JLabels...
  • 843807
    843807 Member Posts: 46,582
    I added a null layout manager and everything stuck. One line of code. Ha ha that article on layout managers was good thanks.
This discussion has been closed.