Java 使用Windows事件关闭JFrames

Java 使用Windows事件关闭JFrames,java,swing,jframe,Java,Swing,Jframe,我正在编写一个简单的程序,其中我有多个JFrame窗口,但当我使用多个窗口时,它们会堆叠,并且我试图将它们的可见性设置为false,但这不起作用,因此我尝试模拟关闭,以便帧不会堆叠在一起,但我不知道使用什么,因为我的程序不是窗口 public void close(){ WindowEvent winClosingEvent = new WindowEvent(this, WindowEvent.WINDOW_CLOSING); Toolkit.getDefaultToolkit

我正在编写一个简单的程序,其中我有多个
JFrame
窗口,但当我使用多个窗口时,它们会堆叠,并且我试图将它们的可见性设置为false,但这不起作用,因此我尝试模拟关闭,以便帧不会堆叠在一起,但我不知道使用什么,因为我的程序不是窗口

public void close(){
    WindowEvent winClosingEvent = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
    Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosingEvent);
}
这个上,我一直得到一个错误

incompatible types: Program1 cannot be converted to Window
如果有人有更好的方法来做这件事,请让我知道我找不到更好的方法来为我工作

import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

//http://vignette4.wikia.nocookie.net/bioshock/images/b/ba/Monu_Island-Skyline01.png/revision/latest?cb=20130521042740
public class Program1 {
    public String welcome() {
        Date date = new Date();
        File music = new File("BioShock_Infinite_-_BioShock_Infinite_-_After_Youv.wav");
        try {
            Clip clip = AudioSystem.getClip();
            clip.open(AudioSystem.getAudioInputStream(music));
            clip.start();
        } catch (LineUnavailableException | UnsupportedAudioFileException | IOException e) {
        }
        JFrame welcomeJFrame = new JFrame("Welcome to Daimeon's ESCAPE!");
        JLabel backgroundImageJLabel = new JLabel();
        backgroundImageJLabel.setIcon(new ImageIcon("Monu_Island-Skyline01.png"));
        JLabel gameNameJLabel = new JLabel("An Escape from Monument Island!");
        JLabel welcomeJLabel = new JLabel("Welcome to Daimeon's Escape Game.");
        JLabel currentDateJLabel = new JLabel("" + date.toString() + "");
        JPanel Panel = new JPanel();
        welcomeJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        currentDateJLabel.setBounds(400, 700, 800, 60);
        currentDateJLabel.setFont(new Font("Andes", Font.BOLD, 60));
        currentDateJLabel.setHorizontalAlignment(JLabel.CENTER);
        gameNameJLabel.setBounds(350, 15, 900, 60);
        gameNameJLabel.setForeground(Color.red);
        gameNameJLabel.setFont(new Font("Andes", Font.BOLD, 60));
        gameNameJLabel.setHorizontalAlignment(JLabel.CENTER);
        welcomeJLabel.setBounds(450, 75, 700, 35);
        welcomeJLabel.setFont(new Font("Andes", Font.BOLD, 30));
        welcomeJLabel.setHorizontalAlignment(JLabel.CENTER);
        Panel.add(backgroundImageJLabel);
        backgroundImageJLabel.add(gameNameJLabel);
        backgroundImageJLabel.add(welcomeJLabel);
        backgroundImageJLabel.add(currentDateJLabel);
        welcomeJFrame.add(Panel);
        welcomeJFrame.setSize(1828, 1080);
        welcomeJFrame.setLocationRelativeTo(null);
        welcomeJFrame.setVisible(true);
        String name = "";
        name = getUsers_Name(name);
        gretting(name);
        return name;
    }

    public String getUsers_Name(String name) {
        return name = JOptionPane.showInputDialog(null, "Hey there please sign-in with your name:");
    }

    public void gretting(String name) {
        JOptionPane.showMessageDialog(null, "Well howdy there " + name + " welcome to the website."
                + "\nHeres a run down of what this is all about.");
        JOptionPane.showMessageDialog(null,
                name + "\n you are stuck in the Monument on Monument island trying to escape"
                        + "\n from songbrid and from the sherpards men, both are trying to kill"
                        + " you.");
        JOptionPane.showMessageDialog(null,
                "\t \t \t Basic Registration Fees\n" + "$2.50 : Dont run before you can walk "
                        + "( Ages 0 - 4 )\n" + "$5.00 : Felling courageous " + "( Ages 4 - 12 )\n"
                        + "$7.50 : Wont be a walk in the park " + "( Ages 13 - 17 )\n"
                        + "$9.75 : Expert " + "( Ages 18+ )\n" + "$1.25 : Additional Items "
                        + "( Lock Pick, Silver Eagle or Creature )",
                "Fees", JOptionPane.WARNING_MESSAGE, null);
        JOptionPane.showMessageDialog(null,
                "Hey " + name + ",\n" + "Please continue to start\n" + "registration",
                "Please Continue", JOptionPane.WARNING_MESSAGE, null);
    }

    public void start_regstration(ArrayList users_Information) {
        int[] user_number_information = new int[5];
        int addons = 0;
        double[] user_dep_amount = new double[5];
        users_Information.add(get_user_alias(users_Information));
        users_Information.add(get_user_gender(users_Information));
        users_Information.add(get_user_atrological_sign(users_Information));
        user_number_information[0] = get_user_birth_year(users_Information);
        user_dep_amount[0] = get_int_player_dep_amount(users_Information);
        user_number_information[2] = get_addon_1(users_Information);
        user_number_information[3] = get_addon_2(users_Information);
        user_number_information[4] = get_addon_3(users_Information);
        confirm(users_Information, user_number_information);
        compute_total_addons(user_number_information);
        feature_recipt(users_Information, user_number_information);
        pre_total_reg_fee_recipt(users_Information, user_number_information);
        user_number_information[1] = get_players_age(user_number_information);
        user_dep_amount[1] = players_age_reg_fee(user_number_information);
        user_dep_amount[2] = daimeons_round_to_penny(user_number_information);
        final_player_fee_amount(users_Information, user_dep_amount);
        exit_m();
    }

    public String get_user_alias(ArrayList users_Information) {
        String alias;
        alias = (String) JOptionPane.showInputDialog(null,
                "Hey " + users_Information.get(0) + " what is your alias?", "Alias",
                JOptionPane.QUESTION_MESSAGE, null, null, "Captain");
        return alias;
    }

    public String get_user_gender(ArrayList users_Information) {
        String user_gender;
        user_gender = (String) JOptionPane.showInputDialog(null,
                "Hey " + users_Information.get(1) + " what is your gender?", "Gender",
                JOptionPane.QUESTION_MESSAGE, null, null, "Male");
        return user_gender;
    }

    public String get_user_atrological_sign(ArrayList users_Information) {
        close();
        String astro_sign;
        astro_sign = (String) JOptionPane.showInputDialog(null,
                "Hey " + users_Information.get(1) + " what's your astrological sign?",
                "Astrological Sign", JOptionPane.QUESTION_MESSAGE, null, null, "Gemini");
        return astro_sign;
    }

    public int get_user_birth_year(ArrayList users_Information) {
        int bY;
        String input = JOptionPane.showInputDialog(null,
                "Hey " + users_Information.get(1) + " what is your birth year?");
        bY = Integer.parseInt(input);
        return bY;
    }

    public double get_int_player_dep_amount(ArrayList users_Information) {
        double user_dep;
        Object input = JOptionPane.showInputDialog(null,
                "Hey " + users_Information.get(1) + " how much would you like to deposit?",
                "Deposit", JOptionPane.QUESTION_MESSAGE, null, null, "1234.56");
        user_dep = Double.parseDouble((String) input);
        return user_dep;
    }

    public int get_addon_1(ArrayList users_Information) {
        return 1;
    }
public int extraFeatureTreasure( String alias ) 
{

}

public int get_addon_2(ArrayList用户信息){
返回2;
}
public int get_addon_3(ArrayList用户信息){
返回3;
}
公共作废确认(ArrayList用户信息,int[]用户编号信息){
}
公共双计算总插件(int[]用户编号信息){
返回1.0;
}
公共无效功能(ArrayList用户信息,int[]用户编号信息){
}
公开作废预登记费总额收据(ArrayList用户信息,
int[]用户号(信息){
}
公共int获取玩家年龄(int[]用户编号信息){
返回1;
}
公开双人球员年龄注册费(int[]用户编号信息){
返回1.0;
}
公共双日币(整数[]用户号信息){
返回1.0;
}
公开作废最终玩家费用金额(ArrayList用户信息,双[]用户费用金额){
}
公共无效出口_m(){
系统出口(0);
}
公众假期结束(){
//TODO:修复:
//WindowEvent winClosingEvent=新的WindowEvent(此,
//WindowEvent.WINDOW\u关闭);
//Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosingEvent);
}
公共静态void main(字符串[]args){
Program1方法=新的Program1();
ArrayList用户信息=新建ArrayList();
users_Information.add(methods.welcome());
方法:开始注册(用户信息);
方法:extraFeatureTreasure();
}
}

您没有为您的确切问题提供足够的信息和解释,但以下是我可以帮助您解决的问题: 要在单击操作系统提供的关闭按钮(通常位于右上角)时关闭JFrame,这将对您有效(将其放入控制JFrame的类中,该类还扩展了JFrame类):

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

要在特定事件上关闭JFrame,请使用:

this.dispose();
要使JFrame不可见,请使用以下命令:

this.setVisible(false);

我的第一个问题是,为什么您的程序会显示多个jframe,因为大多数真实世界的程序只显示一个主窗口?请参阅了解更多关于此的信息。我的下一个问题是,为什么要在非窗口上调用窗口事件?我的教授要求使用多个jframe,我知道这不实用,但他希望我们充分使用它来理解它。我正在试图弄清楚如何关闭它,如果windows事件能起到作用,那么当我有更多的工作要做时,我已经厌倦了浪费时间来弄清楚这一点。
“当我有更多的工作要做时,我厌倦了浪费时间来弄清楚这一点。”
——那么如果你想要一个快速、体面的解决方案,你可能不得不让这个问题更容易回答。请考虑创建并发布一个有效的,如上面提到的@ MaDealver。这将给你最好的机会快速得到答案。这不是整个程序,也不是不可运行的代码片段,而是一个新的小程序,以代码格式的文本形式发布您的问题,我们可以复制、编译和运行它,而无需修改它。它将有一个主方法和导入等等。图像、字体和所有这些控件与这个问题有什么关系?如果,正如我所怀疑的那样,它们不应该包括在示例中。首先让它以最简单的形式工作。根据问题描述,这将只涉及两个帧,其中一个帧中可能有一个按钮来触发关闭操作。“我的教授要求多个JFrame”告诉你的教授,从我这里,他们是一个白痴,在他们知道这有多愚蠢之前不应该教书。“但他希望我们充分利用它来理解它。”证据表明他们不理解它。。不,同样,不要发布所有代码。如果你想要一个快速的解决方案,你会想要创建一个新的独立的小程序,一个没有不必要的位。请阅读或重新阅读链接。
this.dispose();
this.setVisible(false);