Discussions
Categories
- 196.7K All Categories
- 2.2K Data
- 234 Big Data Appliance
- 1.9K Data Science
- 449.8K Databases
- 221.5K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 549 MySQL Community Space
- 477 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 532 SQLcl
- 4K SQL Developer Data Modeler
- 186.8K SQL & PL/SQL
- 21.2K SQL Developer
- 295.3K Development
- 17 Developer Projects
- 138 Programming Languages
- 292K Development Tools
- 104 DevOps
- 3.1K QA/Testing
- 645.9K Java
- 27 Java Learning Subscription
- 37K Database Connectivity
- 153 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 17 Java Essentials
- 157 Java 8 Questions
- 85.9K Java Programming
- 79 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.2K Java SE
- 13.8K Java Security
- 203 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 389 LiveLabs
- 37 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.6K Other Languages
- 2.3K Chinese
- 170 Deutsche Oracle Community
- 1K Español
- 1.9K Japanese
- 230 Portuguese
Help Please.. Labels jumping after button click/refresh

843807
Member Posts: 46,582
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();
}
}
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
-
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());
-
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... -
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.