【发布时间】:2014-07-28 12:22:36
【问题描述】:
我在关闭 GUI 时遇到了一些问题。我试过了
frame.setDefaultCloseOperation(JFrame.CLOSE_ON_EXIT);
我已尝试使用 DISPOSE_ON_EXIT,但无法正常工作。
当我在没有任何代码的情况下运行程序并单击“X”时,它会关闭窗口,但它仍在运行。
当我将该代码放入其中时,它将无法编译,并且出现此错误。
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
at InvestmentFrame2.main(InvestmentFrame2.java:103)
我已尝试阅读此处以及其他网站上的建议。我用来学习这些东西的书并没有真正解释它的任何内容,只是在一些示例代码中提供了它所以我一直在尝试一些不同的建议。
我不知道是否需要导入其他内容或是否存在其他问题?
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);
}
}
【问题讨论】:
-
我的 IDE 在我运行它之前也会给出一条消息,说它找不到符号 CLOSE_ON_EXIT 或 DISPOSE_ON_EXIT?