如何在程序中间关闭JFrame [英] How to close a JFrame in the middle of a program

查看:107
本文介绍了如何在程序中间关闭JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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消息对话框可以在程序中继续运行而无需完全退出程序。

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.

编辑:我尝试过。 dispose()但我得到了:

I tried .dispose() but I get this:

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


推荐答案

尝试: this.dispose()

JPanel 没有方法,但 JFrame 可以

编辑

在您的main中,您没有调用框架:

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

结果是:


其后是


编辑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 );

    }
}

这篇关于如何在程序中间关闭JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆