2017-04-06 135 views
0

我正在寫一個簡單的程序,我有多個JFrame窗口,但是當我使用多個堆棧時,我試圖將它們的可見性設置爲false,這是行不通的,所以我試圖模擬一個關閉,所以框架不堆疊在一起,但我不知道該怎麼用,因爲我的程序不是一個窗口。用Windows事件關閉JFrames

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

我的第一個問題就是**爲什麼**你的程序會顯示多個JFrames,因爲大多數真實世界的程序只顯示一個主窗口?請參閱[使用多個JFrames,好/壞實踐?](http://stackoverflow.com/questions/9554636)瞭解更多信息。我的下一個問題是,爲什麼你會嘗試在非窗口上調用窗口事件? –

+0

那麼我的教授要求多個JFrames,我知道這是不切實際的,但他希望我們使用它足以理解它。我想弄清楚如何關閉它,如果一個Windows事件可以做到這一點,我厭倦了浪費時間試圖解決這個問題,當我有更多的工作要做。 –

+0

*「我試圖將他們的可見性設置爲false,這是行不通的」* - 是一個需要調查的問題,因爲它應該工作得很好,可能的原因是,您嘗試更新的框架不是一個在屏幕上 - 但這是一個猜測,我們沒有一個可運行的示例演示您的問題 – MadProgrammer

回答

2

你沒有把足夠的信息和說明您的具體問題,但這裏是我可以幫你: 要使當你點擊操作系統提供的關閉按鈕(通常在右上角)時,JFrame關閉,這對你來說很有用(把它放在控制你的JFrame的類中,它也擴展了JFrame類) S):

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

要在一個特定的事件用接近一個JFrame:

this.dispose(); 

爲了使一個JFrame看不見的使用:

this.setVisible(false);