退出框架时如何关闭程序

时间:2014-07-28 04:10:15

标签: java swing jframe

我遇到一些问题让我的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);
    }
}

1 个答案:

答案 0 :(得分:4)

您正在尝试运行无法编译的类... JFrame.DISPOSE_ON_EXIT不存在。您实际上在寻找JFrame.EXIT_ON_CLOSE

请花一点时间阅读JavaDocs for JFrame

  

public void setDefaultCloseOperation(int operation)

设置   用户启动时默认发生的操作   在这个框架上“关闭”。您必须指定以下之一   选项:

* DO_NOTHING_ON_CLOSE(在WindowConstants中定义):   什么都不做;要求程序处理中的操作   注册WindowListener对象的windowClosing方法。
*   HIDE_ON_CLOSE(在WindowConstants中定义):自动隐藏   调用任何已注册的WindowListener对象后的框架。
*   DISPOSE_ON_CLOSE(在WindowConstants中定义):自动隐藏和   在调用任何已注册的WindowListener之后处理该帧   对象。
* EXIT_ON_CLOSE(在JFrame中定义):退出   使用System退出方法的应用程序。仅在此使用此功能   应用程序。

默认情况下,该值设置为HIDE_ON_CLOSE。   对此属性值的更改会导致触发属性   更改事件,属性名称为“defaultCloseOperation”。

您可能还会发现某些使用How to Make Frames (Main Windows) ...

您还应确保在事件调度线程的上下文中创建UI,请查看Initial Threads以获取更多详细信息

更新了示例

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.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

编译并运行就好了......