如何在程序运行过程中关闭JFrame

发布于 2024-09-06 02:15:24 字数 3202 浏览 3 评论 0原文

public class JFrameWithPanel extends JFrame implements ActionListener, ItemListener
{
    int packageIndex;
    double price;
    double[] prices = {49.99, 39.99, 34.99, 99.99};

    DecimalFormat money = new DecimalFormat("$0.00");
    JLabel priceLabel = new JLabel("Total Price: "+price);
    JButton button = new JButton("Check Price");
    JComboBox packageChoice = new JComboBox();
    JPanel pane = new JPanel();
    TextField text = new TextField(5);
    JButton accept = new JButton("Accept");
    JButton decline = new JButton("Decline");
    JCheckBox serviceTerms = new JCheckBox("I Agree to the Terms of Service.", false);
    JTextArea termsOfService = new JTextArea("This is a text area", 5, 10);

    public JFrameWithPanel()
    {
        super("JFrame with Panel");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pane.add(packageChoice);
        setContentPane(pane);
        setSize(250,250);
        setVisible(true);

        packageChoice.addItem("A+ Certification");
        packageChoice.addItem("Network+ Certification ");
        packageChoice.addItem("Security+ Certifictation");
        packageChoice.addItem("CIT Full Test Package");

        pane.add(button);
        button.addActionListener(this);

        pane.add(text);
        text.setEditable(false);
        text.setBackground(Color.WHITE);
        text.addActionListener(this);

        pane.add(termsOfService);
        termsOfService.setEditable(false);
        termsOfService.setBackground(Color.lightGray);

        pane.add(serviceTerms);
        serviceTerms.addItemListener(this);

        pane.add(accept);
        accept.addActionListener(this);

        pane.add(decline);
        decline.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e)
    {
        packageIndex = packageChoice.getSelectedIndex();
        price = prices[packageIndex];
        text.setText("$"+price);

        Object source = e.getSource();

        if(source == accept)
        {
            if(serviceTerms.isSelected() == false)
            {
                JOptionPane.showMessageDialog(null,"Please accept the terms of service.", "Terms of Service", JOptionPane.ERROR_MESSAGE);
            }
            else
            {
                JOptionPane.showMessageDialog(null,"Thank you. We will now move on to registering your product.");
                pane.dispose();
            }
        }
        else if(source == decline)
        {
            System.exit(0);
        }
    }

    public void itemStateChanged(ItemEvent e)
    {
        int select = e.getStateChange();
    }

    public static void main(String[] args)
    {
        String value1;
        int constant = 1, invalidNum = 0, answerParse, packNum, packPrice;

        JOptionPane.showMessageDialog(null,"Hello!"+"\nWelcome to the CIT Test Program.");

        JOptionPane.showMessageDialog(null,"IT WORKS!");
    }



}//class

如何关闭此框架,以便我的 JOptionPane 消息对话框可以在程序中继续,而无需完全退出程序。

编辑:我尝试了 .dispose() 但我得到了这个:

cannot find symbol
symbol  : method dispose()
location: class javax.swing.JPanel
                pane.dispose();
                    ^
public class JFrameWithPanel extends JFrame implements ActionListener, ItemListener
{
    int packageIndex;
    double price;
    double[] prices = {49.99, 39.99, 34.99, 99.99};

    DecimalFormat money = new DecimalFormat("$0.00");
    JLabel priceLabel = new JLabel("Total Price: "+price);
    JButton button = new JButton("Check Price");
    JComboBox packageChoice = new JComboBox();
    JPanel pane = new JPanel();
    TextField text = new TextField(5);
    JButton accept = new JButton("Accept");
    JButton decline = new JButton("Decline");
    JCheckBox serviceTerms = new JCheckBox("I Agree to the Terms of Service.", false);
    JTextArea termsOfService = new JTextArea("This is a text area", 5, 10);

    public JFrameWithPanel()
    {
        super("JFrame with Panel");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pane.add(packageChoice);
        setContentPane(pane);
        setSize(250,250);
        setVisible(true);

        packageChoice.addItem("A+ Certification");
        packageChoice.addItem("Network+ Certification ");
        packageChoice.addItem("Security+ Certifictation");
        packageChoice.addItem("CIT Full Test Package");

        pane.add(button);
        button.addActionListener(this);

        pane.add(text);
        text.setEditable(false);
        text.setBackground(Color.WHITE);
        text.addActionListener(this);

        pane.add(termsOfService);
        termsOfService.setEditable(false);
        termsOfService.setBackground(Color.lightGray);

        pane.add(serviceTerms);
        serviceTerms.addItemListener(this);

        pane.add(accept);
        accept.addActionListener(this);

        pane.add(decline);
        decline.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e)
    {
        packageIndex = packageChoice.getSelectedIndex();
        price = prices[packageIndex];
        text.setText("$"+price);

        Object source = e.getSource();

        if(source == accept)
        {
            if(serviceTerms.isSelected() == false)
            {
                JOptionPane.showMessageDialog(null,"Please accept the terms of service.", "Terms of Service", JOptionPane.ERROR_MESSAGE);
            }
            else
            {
                JOptionPane.showMessageDialog(null,"Thank you. We will now move on to registering your product.");
                pane.dispose();
            }
        }
        else if(source == decline)
        {
            System.exit(0);
        }
    }

    public void itemStateChanged(ItemEvent e)
    {
        int select = e.getStateChange();
    }

    public static void main(String[] args)
    {
        String value1;
        int constant = 1, invalidNum = 0, answerParse, packNum, packPrice;

        JOptionPane.showMessageDialog(null,"Hello!"+"\nWelcome to the CIT Test Program.");

        JOptionPane.showMessageDialog(null,"IT WORKS!");
    }



}//class

How do I get this frame to close so that my JOptionPane Message Dialogs can continue in the program without me exiting the program completely.

EDIT: I tried .dispose() but I get this:

cannot find symbol
symbol  : method dispose()
location: class javax.swing.JPanel
                pane.dispose();
                    ^

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何止钟意 2024-09-13 02:15:24

尝试:改为 this.dispose()

JPanel 没有该方法,但 JFrame 可以

编辑

在您的 main 中,您没有调用您的 Frame:

public static void main(String[] args)  {
    String value1;
    int constant = 1, invalidNum = 0, answerParse, packNum, packPrice;

    JOptionPane.showMessageDialog(null,"Hello!"+"\nWelcome to the CIT Test Program.");

    JOptionPane.showMessageDialog(null,"IT WORKS!");
    }

尝试添加它并查看差异:

public static void main(String[] args)  {
    String value1;
    int constant = 1, invalidNum = 0, answerParse, packNum, packPrice;

    JOptionPane.showMessageDialog(null,"Hello!"+"\nWelcome to the CIT Test Program.");

    JOptionPane.showMessageDialog(null,"IT WORKS!");
    new JFrameWithPanel(); //<-- creating a JFrameWithPanel
}

同样在执行操作的方法中,您要显示对话框,然后进行处理,可能您想以相反的方式进行操作。

if(serviceTerms.isSelected() == false) {
    JOptionPane.showMessageDialog(null,"Please accept the terms of service.", "Terms of Service", JOptionPane.ERROR_MESSAGE);
} else {
    this.dispose();
    JOptionPane.showMessageDialog(null,"Thank you. We will now move on to registering your product.");
}

结果:

“主要”

随后是

结果

编辑2

尝试下面的代码,它应该显示一个框架,当您单击“关闭”按钮时,它应该显示一个对话框,这是您正在寻找的吗?

import javax.swing.*;
import java.awt.event.*;

class FrameDemo {
    public static void main( String [] args ) {
        final JFrame frame = new JFrame("Main frame");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.add( new JPanel(){{
            add( new JLabel("This is the main content"));
            add( new JButton("Close"){{
                addActionListener( new ActionListener(){
                    public void actionPerformed(ActionEvent e ) {
                        frame.dispose();
                        JOptionPane.showMessageDialog(frame,"IT WORKS!");

                    }
                });
            }});
        }});
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );

    }
}

Try: this.dispose() instead.

JPanel doesn't have that method but JFrame does

edit

In your main you're not calling your Frame:

public static void main(String[] args)  {
    String value1;
    int constant = 1, invalidNum = 0, answerParse, packNum, packPrice;

    JOptionPane.showMessageDialog(null,"Hello!"+"\nWelcome to the CIT Test Program.");

    JOptionPane.showMessageDialog(null,"IT WORKS!");
    }

Try adding it and see the difference:

public static void main(String[] args)  {
    String value1;
    int constant = 1, invalidNum = 0, answerParse, packNum, packPrice;

    JOptionPane.showMessageDialog(null,"Hello!"+"\nWelcome to the CIT Test Program.");

    JOptionPane.showMessageDialog(null,"IT WORKS!");
    new JFrameWithPanel(); //<-- creating a JFrameWithPanel
}

Also in the action performed method, you're showing the dialog and then disposing, probably you want to do it the other way around.

if(serviceTerms.isSelected() == false) {
    JOptionPane.showMessageDialog(null,"Please accept the terms of service.", "Terms of Service", JOptionPane.ERROR_MESSAGE);
} else {
    this.dispose();
    JOptionPane.showMessageDialog(null,"Thank you. We will now move on to registering your product.");
}

Result in:

main

Followed by

result

edit 2

Try the following code, it should show a frame, and when you click the "close" button it should show a dialog, is that what you're looking for?

import javax.swing.*;
import java.awt.event.*;

class FrameDemo {
    public static void main( String [] args ) {
        final JFrame frame = new JFrame("Main frame");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.add( new JPanel(){{
            add( new JLabel("This is the main content"));
            add( new JButton("Close"){{
                addActionListener( new ActionListener(){
                    public void actionPerformed(ActionEvent e ) {
                        frame.dispose();
                        JOptionPane.showMessageDialog(frame,"IT WORKS!");

                    }
                });
            }});
        }});
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );

    }
}
笑着哭最痛 2024-09-13 02:15:24

我知道这可能是一个愚蠢的答案,但有时最明显的事情会引起问题。我没有看到你在代码中导入 javax.swing...你这样做了吗?

I know this might be a stupid answer, but sometimes the most obvious thing's are causing problems. I didn't see you importing javax.swing in your code... Did you do that?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文