Click here to Skip to main content
15,915,081 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have my 2 values that come up after entering some values into the textfields but they cover the bottom two strings of text. I need to move the values that appear to the right side of the app.

What I have tried:

package howmuch;
/**
*
* @author stephenwessels
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
public class HowMuch extends JFrame
implements ActionListener
{
Container content = this.getContentPane();

JLabel purchase = new JLabel("Amount of Purchase");
JLabel lblPayment = new JLabel("Monthly Payment");
JLabel lblTotCost = new JLabel("Total Purchase Cost");
JLabel IR = new JLabel("Interest Rate");
JLabel YTP = new JLabel("Years to Pay");
JLabel mpAmount = new JLabel();
JLabel tpc = new JLabel();
JTextField tfAmount = new JTextField();
JTextField tfIR = new JTextField();
JTextField tfYTP = new JTextField();
JButton button = new JButton("Calculate");



public HowMuch()
{
content.setLayout(new GridLayout(0,2, 5,5));
content.add(purchase);
content.add(tfAmount);
content.add(IR);
content.add(tfIR);
content.add(YTP);
content.add(tfYTP);
content.add(lblPayment);
content.add(mpAmount);
content.add(lblTotCost);
content.add(tpc);
content.add(button);



this.setVisible(true);
this.setSize(350,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("First GUI App");

button.addActionListener(this);

}
public void actionPerformed(ActionEvent e)
{
String strAmount = tfAmount.getText();
String strIR = tfIR.getText();
String strYTP = tfYTP.getText();

Double dAmount = new Double(strAmount);
Double dIR = new Double(strIR);
Double dYTP = new Double(strYTP);

double amount = Double.parseDouble(tfAmount.getText());
double ir = Double.parseDouble(tfIR.getText());
double ytp = Double.parseDouble(tfYTP.getText());

Double MP = (amount * ir) / (1 - (Math.pow(1.0 / (1.0 + ir), ytp * 12)));
Double TC = (MP * 12) * ytp;

NumberFormat fmt = NumberFormat.getCurrencyInstance();

lblPayment.setText(fmt.format(MP));
lblTotCost.setText(fmt.format(TC));
}

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
HowMuch gui = new HowMuch();
}
}
Posted

1 solution

You need to add two more labels, one beside lblPayment and another add beside lblTotCost. Once the button clicked, set value to these two labels.

Below code can be removed
lblPayment.setText(fmt.format(MP));
lblTotCost.setText(fmt.format(TC));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900