I'm trying to make a basic GUI program that computes interest based on a few user-specified values.
When I run the driver without adding a listener to my buttons, everything works properly, but when I go to add the listener to the button, the GUI doesn't show when I run the driver.
Driver class:
package calculator; import javax.swing.JFrame; import javax.swing.WindowConstants; public class InterestGUI { public static void createAndShowGUI() { JFrame frame = new JFrame("Interest Calculator"); frame.setContentPane(new calcGUI()); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
GUI Class
package calculator; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JButton; import javax.swing.JTextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.NumberFormat; public class calcGUI extends JPanel{ private JButton simpleInterestButton = new JButton("Compute Simple Interest"); private JButton compoundInterestButton = new JButton("Compute Compound Interest"); private JLabel principal = new JLabel("Principal:"); private JLabel rate = new JLabel("Rate(Percentage):"); private JLabel years = new JLabel("Years:"); private JTextField principalInput = new JTextField(7); private JTextField rateInput = new JTextField(7); private JTextField yearsInput = new JTextField(7); private JLabel interestResult = new JLabel(interestTotal); private static String interestTotal; private JPanel topPanel = new JPanel(); private JPanel midPanel = new JPanel(); private JPanel bottomPanel = new JPanel(); public class simpleInterestButtonListener implements ActionListener { private String principalValStr = principalInput.getText(); private double principalValInt = Integer.parseInt(principalValStr.trim()); private String rateValStr = rateInput.getText(); private double rateValInt = Integer.parseInt(rateValStr.trim()); private String yearsValStr = yearsInput.getText(); private double yearsValInt = Integer.parseInt(yearsValStr.trim()); private double simpleInterest = 0; private String simpleInterestStr = ""; public void actionPerformed(ActionEvent arg0) { simpleInterest = principalValInt + (principalValInt*(rateValInt/100)*yearsValInt); simpleInterestStr = NumberFormat.getCurrencyInstance().format(simpleInterest); interestTotal = "Computed simple interest is " + simpleInterestStr; } } public calcGUI() { topPanel.add(principal); topPanel.add(principalInput); topPanel.add(rate); topPanel.add(rateInput); topPanel.add(years); topPanel.add(yearsInput); add(topPanel); simpleInterestButton.addActionListener(new simpleInterestButtonListener()); midPanel.add(simpleInterestButton); compoundInterestButton.addActionListener(new ActionListener() { private String principalValStr = principalInput.getText(); private double principalValInt = Integer.parseInt(principalValStr.trim()); private String rateValStr = rateInput.getText(); private double rateValInt = Integer.parseInt(rateValStr.trim()); private String yearsValStr = yearsInput.getText(); private double yearsValInt = Integer.parseInt(yearsValStr.trim()); private double compoundInterest = 0; private String compoundInterestStr = ""; public void actionPerformed(ActionEvent arg0) { compoundInterest = (int) (principalValInt*Math.pow((1+rateValInt/100),yearsValInt)); compoundInterestStr = NumberFormat.getCurrencyInstance().format(compoundInterest); interestTotal = "Computed simple interest is " + compoundInterestStr; } }); midPanel.add(compoundInterestButton); add(midPanel); bottomPanel.add(interestResult); add(bottomPanel); } }