簡體   English   中英

退出框架時如何關閉程序

[英]How to close my program when exiting the frame

我遇到一些問題,無法關閉我的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);
    }
}

您正在嘗試運行無法編譯的類... JFrame.DISPOSE_ON_EXIT不存在。 您實際上正在尋找JFrame.EXIT_ON_CLOSE

花一點時間閱讀JavaDocs for JFrame

公共無效setDefaultCloseOperation(int操作)

設置用戶在此框架上啟動“關閉”時默認情況下將發生的操作。 您必須指定以下選項之一:

* DO_NOTHING_ON_CLOSE(在WindowConstants中定義):什么都不做; 要求程序處理注冊的WindowListener對象的windowClosing方法中的操作。
* HIDE_ON_CLOSE(在WindowConstants中定義):調用任何已注冊的WindowListener對象后自動隱藏框架。
* DISPOSE_ON_CLOSE(在WindowConstants中定義):調用任何已注冊的WindowListener對象后,自動隱藏和處置框架。
* EXIT_ON_CLOSE(在JFrame中定義):使用系統退出方法退出應用程序。 僅在應用程序中使用它。

該值默認設置為HIDE_ON_CLOSE。 更改此屬性的值會導致觸發屬性更改事件,並帶有屬性名稱“ defaultCloseOperation”。

您可能還會發現“ 如何制作框架”(主窗口) ...

您還應該確保在事件調度線程的上下文中創建您的UI,有關更多詳細信息,請查看初始線程

更新了示例

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);
    }
}

編譯並運行對我來說很好...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM