0

I'm writing a simple program where I have multiple JFrame windows but when I use multiple they stack and I've tried to set their visibility to false that isn't working so I'm trying to simulate a close so the frames don't stack on top of each other but I don't know what to use since my program isn't a window.

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

on the this I keep getting a error of

incompatible types: Program1 cannot be converted to Window

If someone has a better way of doing this please let me know I cant find a better way that's working for me.

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();
    }
}
D Rodriguez
  • 41
  • 1
  • 7
  • 1
    My first question is **why** your program would display multiple JFrames as most real-world programs display only one main window? Please see [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636) for more on this. My next question would be why would you try to call a window event on a non-window? – Hovercraft Full Of Eels Apr 06 '17 at 00:06
  • Well my professor is asking for multiple JFrames I know it isn't practical but he want us to use it enough to understand it. I am trying to figure out how to close it and if a windows event would do the trick I'm tired of wasting time trying to figure this out, when I have more to work on. – D Rodriguez Apr 06 '17 at 00:09
  • *"Ive tried to set their visibility to false that isn't working"* - is an issue which needs to be investigated as it should work just fine, likely cause is, the frame you're trying to update isn't the one on the screen - but that's a guess and we don't have a runnable example demonstrates your problem – MadProgrammer Apr 06 '17 at 00:11
  • Ive tried to recall the method that contains the JFrame by adding a If else stamen and with a parameter of int. So when I recall the method if sets the visibility to false when I make the if true. – D Rodriguez Apr 06 '17 at 00:16
  • 1
    `"I'm tired of wasting time trying to figure this out, when I have more to work on."` -- then if you want a quick decent solution, you're probably going to have to make the question easier to answer. Please consider creating and posting a valid [mcve] as suggested by @MadProgrammer above. It will give you your best chance of getting an answer quickly. This is not the entire program nor non-runnable snippets but rather a small new program, posted with your question as code-formatted text, that we can copy, compile, and run without altering it. It will have a main method and imports and all. – Hovercraft Full Of Eels Apr 06 '17 at 00:18
  • Edit: no, not in pastebin. All code must be posted as code-formatted text with your question. – Hovercraft Full Of Eels Apr 06 '17 at 00:19
  • OK, I've posted your code from pastebin into your question -- but I only see **one** JFrame being used, not the multiple that you mention. Please clarify. – Hovercraft Full Of Eels Apr 06 '17 at 00:26
  • 1
    What do images, fonts, and all those controls have to do with the problem? If, as I suspect 'nothing', they should not be included in the example. Get this working in the simplest form first. Given the problem description, that would involve just two frames, and maybe a single button in one of them to trigger the close action. *"my professor is asking for multiple JFrames"* Tell your professor, from me, that they are a moron who should not be teaching until they learn how stupid that is. *"but he want us to use it enough to understand it."* Evidence suggests **they** don't understand it. .. – Andrew Thompson Apr 06 '17 at 00:34
  • .. if they did, they would not be setting such a pointless, time wasting task for the students. In fact, tell them to show you their working implementation. Not the code, just the frames on-screen. Once they've tried it, they'll be a lot less keen to set such ridiculous tasks for their students. – Andrew Thompson Apr 06 '17 at 00:35
  • Ive added some more code from a previods version that I ended up dumping because the professor didn't want what I added . But this is the code or a little modified to be added in the new one but Ive tried to close them they will not close they just stack with the newest one on top – D Rodriguez Apr 06 '17 at 00:43
  • I see it better if I post all of the code later tonight when it is done or near done to see whats the best I can do. – D Rodriguez Apr 06 '17 at 00:45
  • 1
    No, again, don't post all the code. If you want a quick solution, you will want to create a new separate small program, one without unnecessary bits. Please read or re-read the [mcve] link. – Hovercraft Full Of Eels Apr 06 '17 at 00:51
  • Ok thank you ill delete this post and repost issue when near completion – D Rodriguez Apr 06 '17 at 01:05

1 Answers1

1

You didn't put enough information and explanation to your exact problem but here is what I can help you with : To make a JFrame close when you click on the OS provided close button (at the top right corner usually) this will work for you (put it in the class that controlls your JFrame which also extends JFrame class ) :

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

To close a JFrame on an a specific event use :

this.dispose();

To make a JFrame invisible use this :

this.setVisible(false);
Ammar Akouri
  • 526
  • 9
  • 17