I am having some issues getting my GUI to exit on close. I have tried
frame.setDefaultCloseOperation(JFrame.CLOSE_ON_EXIT);
I have tried it with DISPOSE_ON_EXIT and it is not working.
when I run the program without any of that code and I click "X" it closes the window, but it is still running.
When I put that code in it will not compile and I get this error.
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
at InvestmentFrame2.main(InvestmentFrame2.java:103)
I have tried reading suggestions on here as well as on other websites. The book I am using to learn this stuff didn't really explain anything about it, but just had it in some example code So I have just been trying some various suggestions.
I couldn't figure out if I maybe needed to import something else or if there is some other issue?
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class InvestmentFrame2 extends JFrame
{
private static final int FRAME_WIDTH = 450;
private static final int FRAME_HEIGHT = 250;
private static final double DEFAULT_RATE = 0;
private static final double INITIAL_BALANCE = 0;
private static final double YEARS = 0;
private JLabel rateLabel;
private JLabel balanceLabel;
private JLabel yearsLabel;
private JTextField rateField;
private JTextField balanceField;
private JTextField yearsField;
private JButton button;
private JLabel resultLabel;
private double balance;
public InvestmentFrame2()
{
balance = INITIAL_BALANCE;
resultLabel = new JLabel("Balance: " + balance);
createTextField();
createButton();
createPanel();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
private void createTextField()
{
rateLabel = new JLabel("Interest Rate: ");
balanceLabel = new JLabel("Account Balance: ");
yearsLabel = new JLabel("Number of Years Saving: ");
final int FIELD_WIDTH = 10;
rateField = new JTextField(FIELD_WIDTH);
rateField.setText ("" + DEFAULT_RATE);
balanceField = new JTextField(FIELD_WIDTH);
balanceField.setText("" + INITIAL_BALANCE);
yearsField = new JTextField(FIELD_WIDTH);
yearsField.setText("" + YEARS);
}
class AddInterestListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
double rate = Double.parseDouble(rateField.getText());
double accountBalance = Double.parseDouble(balanceField.getText());
double years = Double.parseDouble(yearsField.getText());
double interest = (accountBalance * rate / 100) * years;
balance = accountBalance + interest;
resultLabel.setText("Balance: " + balance);
}
}
private void createButton()
{
button = new JButton("Calculate Balance");
ActionListener listener = new AddInterestListener();
button.addActionListener(listener);
}
private void createPanel()
{
JPanel panel = new JPanel();
panel.add(rateLabel);
panel.add(rateField);
panel.add(balanceLabel);
panel.add(balanceField);
panel.add(yearsLabel);
panel.add(yearsField);
panel.add(button);
panel.add(resultLabel);
add(panel);
}
public static void main(String[] args)
{
JFrame frame = new InvestmentFrame2();
frame.setTitle("Savings Frame");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_EXIT);
frame.setVisible(true);
}
}