CDMaturity.java:89: setText(java.lang.String) in javax.swing.text.JTextComponent cannot be applied to (double)
tMaturity.setText(maturity);
I have my data types correct and my parsing is correct, so why can't I use .setText to display in the GUI?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CDMaturity extends JFrame {
// declare GUI components
JLabel lAmount, lYears, lInterest;
JTextField tAmount, tYears, tInterest, tMaturity;
JButton bCalculate, bReset;
Container c;
// declare variables
double amount, interest, maturity;
int years;
// override default constructor
public CDMaturity() {
// contruct the components
lAmount = new JLabel("Amount Deposited:");
lYears = new JLabel("Years:");
lInterest = new JLabel("Interest Rate:");
tAmount = new JTextField(10);
tYears = new JTextField(5);
tInterest = new JTextField(5);
tMaturity = new JTextField(10);
bCalculate = new JButton("Calculate");
bReset = new JButton("Reset");
// add components to container
c = getContentPane();
c.setLayout(new GridLayout(5,2));
c.add(lAmount);
c.add(tAmount);
c.add(lYears);
c.add(tYears);
c.add(lInterest);
c.add(tInterest);
c.add(bCalculate);
c.add(tMaturity);
c.add(bReset);
// make calculate non-editable
tMaturity.setEditable(false);
// render the window object
setSize(300,100);
setLocation(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("CoD on Maturity Calculator");
setResizable(false);
setVisible(true);
// create Button Handler object
ButtonHandler buttonHandler = new ButtonHandler();
// register this listener with the button
bCalculate.addActionListener(buttonHandler);
bReset.addActionListener(buttonHandler);
}
private class ButtonHandler implements ActionListener {
public void actionPerformed (ActionEvent ae) {
if (ae.getSource().equals(bReset)) {
// clear all the text fields
tAmount.setText("");
tYears.setText("");
tInterest.setText("");
tMaturity.setText("");
return;
// store entered information
try {
amount = Double.parseDouble(tAmount.getText());
years = Integer.parseInt(tYears.getText());
interest = Double.parseDouble(tInterest.getText());
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, "Please enter numberic data.");
return;
}
// compute and display maturity
maturity = amount * Math.pow((1 + interest / 100),15);
tMaturity.setText(maturity);
}
}
public void main(String[] args) {
CDMaturity myApp = new CDMaturity();
}
}
}


Sign In
Create Account


Back to top










