使用Windows事件关闭JFrame

时间:2017-04-06 00:04:46

标签: java swing jframe

我正在编写一个简单的程序,其中我有多个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 ) 

{

  //Title bar
  JFrame getExtraFeaturesJFrame   = new JFrame("User's Extra Features" );

  //Set background 
  JLabel backgroundImageJLabel    = new JLabel( );
  backgroundImageJLabel.setIcon( new ImageIcon( "VV.jpg" ) );        

  //Treasure
  JLabel treasureJLabel = new JLabel( );

  //Creatures
  JLabel creature1JLabel = new JLabel( );
  JLabel creature2JLabel = new JLabel( );

  //Key
  JLabel keyJLabel      = new JLabel( );

  //Second panel
  JPanel theSecondPanel = new JPanel( );

  getExtraFeaturesJFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

  treasureJLabel.setIcon( new ImageIcon( "lovee.gif" ) );
  creature1JLabel.setIcon( new ImageIcon( "gun.gif" ) );
  creature2JLabel.setIcon( new ImageIcon( "creature2.gif" ) );
  keyJLabel.setIcon( new ImageIcon( "fashionkey.jpg" ) );

  //Extra feature price label
  JLabel extraFeatureTextJLabel = new JLabel( "Extra Features : $1.25 ea" );

  //Treasure
  JLabel treasureTextJLabel = new JLabel( "Treasure" );
  JLabel treasureDescriptionText0JLabel = new JLabel( "Extra treasures are some of the" );
  JLabel treasureDescriptionText1JLabel = new JLabel( "latest fashion trends!" );

  //Creature
  JLabel creature1TextJLabel = new JLabel( "The Guy With The Gun" );
  JLabel creature2TextJLabel = new JLabel( "Unfashionable Shopaholics" );
  JLabel creature1TextDescription0JLabel = new JLabel( "Beware of the creatures!" );
  JLabel creature1TextDescription1JLabel = new JLabel( "if you encounter then at any point" );
  JLabel creature1TextDescription2JLabel = new JLabel( "you will lose all your precious shops" );

  //Key
  JLabel keyTextJLabel = new JLabel( "Fashion Key" );
  JLabel keyTextDescription0JLabel = new JLabel( "Fashion keys are the most" );
  JLabel keyTextDescription1JLabel = new JLabel( "special features in this game" );
  JLabel keyTextDescription2JLabel = new JLabel( "since they add the final touch" );
  JLabel keyTextDescription3JLabel = new JLabel( "to your perfect outfit" );

  //Extra feature label characteristics
  extraFeatureTextJLabel.setFont( new Font( "Cooper Black", Font.BOLD, 40 ) );
  extraFeatureTextJLabel.setForeground( new Color( 250, 150, 200 ));
  extraFeatureTextJLabel.setHorizontalAlignment( JLabel.CENTER );

  //Extra feature label characteristics
  treasureTextJLabel.setFont( new Font( "Cooper Black", Font.BOLD, 25 ) );
  treasureDescriptionText0JLabel.setFont( new Font( "Cooper Black", Font.BOLD, 20 ) );
  treasureDescriptionText1JLabel.setFont( new Font( "Cooper Black", Font.BOLD, 20 ) );

  //Extra feature label characteristics
  creature1TextJLabel.setFont( new Font( "Cooper Black", Font.BOLD, 25 ) );
  creature1TextDescription0JLabel.setFont( new Font( "Cooper Black", Font.BOLD, 20 ) );
  creature1TextDescription1JLabel.setFont( new Font( "Cooper Black", Font.BOLD, 20 ) );
  creature1TextDescription2JLabel.setFont( new Font( "Cooper Black", Font.BOLD, 20 ) );

  //Extra feature label characteristics
  creature2TextJLabel.setFont( new Font( "Cooper Black", Font.BOLD, 25 ) );

  //Extra feature label characteristics
  keyTextJLabel.setFont( new Font( "Cooper Black", Font.BOLD, 25 ) );    
  keyTextDescription0JLabel.setFont( new Font( "Cooper Black", Font.BOLD, 20 ) );
  keyTextDescription1JLabel.setFont( new Font( "Cooper Black", Font.BOLD, 20 ) );
  keyTextDescription2JLabel.setFont( new Font( "Cooper Black", Font.BOLD, 20 ) ); 
  keyTextDescription3JLabel.setFont( new Font( "Cooper Black", Font.BOLD, 20 ) ); 

  //Extra feature label characteristics
  extraFeatureTextJLabel.setBounds( 450,1,700,40 );
  treasureTextJLabel.setBounds( 400,315,500,50 );
  treasureJLabel.setBounds( 200,35,500,300 );
  treasureDescriptionText0JLabel.setBounds( 200,345,500,50 );
  treasureDescriptionText1JLabel.setBounds( 200,365,500,50 );

  keyTextJLabel.setBounds( 1195,284,500,50 );
  keyJLabel.setBounds( 1132,45,400,250 ); 
  keyTextDescription0JLabel.setBounds( 770,20,400,250 );
  keyTextDescription1JLabel.setBounds( 770,40,400,250 );
  keyTextDescription2JLabel.setBounds( 770,60,400,250 );
  keyTextDescription3JLabel.setBounds( 770,80,400,250 );

  creature1TextJLabel.setBounds( 300,645,500,50 );
  creature2TextJLabel.setBounds( 980,645,545,50 );
  creature1TextDescription0JLabel.setBounds( 200,675,500,50 );
  creature1TextDescription1JLabel.setBounds( 200,695,500,50 );
  creature1TextDescription2JLabel.setBounds( 200,715,500,50 );

  creature1JLabel.setBounds( 200,390,500,300 );
  creature2JLabel.setBounds( 897,377,500,300 );     

  //Add all labels to the pannel
  theSecondPanel.add(backgroundImageJLabel);
  backgroundImageJLabel.add(extraFeatureTextJLabel);

  backgroundImageJLabel.add(treasureTextJLabel);
  backgroundImageJLabel.add(treasureJLabel);
  backgroundImageJLabel.add(treasureDescriptionText0JLabel);
  backgroundImageJLabel.add(treasureDescriptionText1JLabel);

  backgroundImageJLabel.add(creature1TextJLabel);
  backgroundImageJLabel.add(creature1JLabel);
  backgroundImageJLabel.add(creature2TextJLabel);
  backgroundImageJLabel.add(creature2JLabel);
  backgroundImageJLabel.add(creature1TextDescription0JLabel);
  backgroundImageJLabel.add(creature1TextDescription1JLabel);
  backgroundImageJLabel.add(creature1TextDescription2JLabel);

  backgroundImageJLabel.add(keyTextJLabel);
  backgroundImageJLabel.add(keyJLabel);
  backgroundImageJLabel.add(keyTextDescription0JLabel);
  backgroundImageJLabel.add(keyTextDescription1JLabel);
  backgroundImageJLabel.add(keyTextDescription2JLabel);
  backgroundImageJLabel.add(keyTextDescription3JLabel);

  //Add panel to the frame
  getExtraFeaturesJFrame.add(theSecondPanel);
  getExtraFeaturesJFrame.setSize ( 1280, 900 );      
  getExtraFeaturesJFrame.setLocationRelativeTo( null ); 
  getExtraFeaturesJFrame.setVisible( true );


  //Ask for user's amount to deposit, output changes from a String to an Object
  //Image 2
  Object getTreasures = JOptionPane.showInputDialog( null,
                                                "Dear " + alias + "\u2661,\n"
                                                +"Please enter the number of treasures\n"
                                                +"you wish to acquire",
                                                "\u2661Number of Treasures\u2661", 
                                                JOptionPane.PLAIN_MESSAGE,
                                                icon, null,
                                                "0");

  //TypeCast, converts returned Object to a string  
  getUserTreasures = (String)getTreasures;

  //Converts returned String to an int
  numberOfTreasures = Integer.parseInt( getUserTreasures );
  return numberOfTreasures;

}

    public int get_addon_2(ArrayList users_Information) {
        return 2;
    }

    public int get_addon_3(ArrayList users_Information) {
        return 3;
    }

    public void confirm(ArrayList users_Information, int[] user_number_information) {
    }

    public double compute_total_addons(int[] user_number_information) {
        return 1.0;
    }

    public void feature_recipt(ArrayList users_Information, int[] user_number_information) {
    }

    public void pre_total_reg_fee_recipt(ArrayList users_Information,
            int[] user_number_information) {
    }

    public int get_players_age(int[] user_number_information) {
        return 1;
    }

    public double players_age_reg_fee(int[] user_number_information) {
        return 1.0;
    }

    public double daimeons_round_to_penny(int[] user_number_information) {
        return 1.0;
    }

    public void final_player_fee_amount(ArrayList users_Information, double[] user_dep_amount) {
    }

    public void exit_m() {
        System.exit(0);
    }

    public void close() {
        // TODO: Fix:
        // WindowEvent winClosingEvent = new WindowEvent(this,
        // WindowEvent.WINDOW_CLOSING);
        // Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosingEvent);
    }

    public static void main(String[] args) {
        Program1 methods = new Program1();
        ArrayList<String> users_Information = new ArrayList<>();
        users_Information.add(methods.welcome());
        methods.start_regstration(users_Information);
methods.extraFeatureTreasure();
    }
}

1 个答案:

答案 0 :(得分:2)

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

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

this.dispose();

要使JFrame不可见,请使用:

this.setVisible(false);