0

So I am new in swing and working on the hangman game. So the way it is supposed to work is that the user is prompt to welcome message which will last a 3seconds, disappears, and then sends the user to the next frame. Everything is working perfectly except that when I run it the first frame is still visible and running in the shadow even though it goes to the next one. I have tried to use the dispose method but it's just closing the frame without going to the next one. Here is my what I have done so far

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.Timer;

public class PA1test extends JFrame{

public static void main(String[] args) {

    // opens the first page
    JFrame gui = new JFrame("Hangman");
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui.add(new First_PageImage());
    gui.pack();
    gui.setVisible(true);


  // action to open the second page    
    ActionListener taskPerformer = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            //...Perform a task...
            JFrame secpage = new JFrame("Hangman");
            secpage.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
           secpage.add(new SecondPage());
            secpage.pack();
            secpage.setVisible(true);
        }
    };

    // set timer to perform action after 3 seconds
    Timer timer = new Timer(3000 ,taskPerformer);
    timer.setRepeats(false);
    timer.start();     

}

}

ThisIsMi
  • 109
  • 1
  • 6
  • 2
    Take a look at [The use of multiple jframes good or bad practice?](http://stackoverflow.com/q/9554636/4475997) – MasterBlaster Aug 08 '16 at 18:14
  • Could be wrong but this sounds more like a splash screen, which I would consider different than the discussion linked to by @MasterBlaster – copeg Aug 08 '16 at 18:58
  • @copeg *"sounds more like a splash screen"* In that case, use a [`SplashScreen`](http://docs.oracle.com/javase/8/docs/api/java/awt/SplashScreen.html)! – Andrew Thompson Aug 09 '16 at 00:14
  • See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. – Andrew Thompson Aug 09 '16 at 00:16

2 Answers2

2

A few points:

  1. Setting the dispose operation to JFrame.EXIT_ON_CLOSE will exit your entire application. Consider using JFrame.DISPOSE_ON_CLOSE
  2. Sounds like you wish to create a splash screen - consider reading Oracle's tutorial on using the SplashScreen class
  3. If your intent is a splash screen and you wish to use a JFrame or JDialog rather than java's build in SplashScreen from (2), consider designing it to look like a splash screen by removing the decorations (setUndecorated(true)) and centering (setLocationRelativeTo(null);)
  4. In order to dispose/hide the initial JFrame/splash, you need to do so after the Timer has fired, which can be done from within the Timer's ActionListener implementation - in order to access the splash screen instance from within the anonymous class, you must mark it as final.

Pseudo-code:

final JFrame splashScreen = new JFrame("Hangman");//mark as final for visibility's sake
splashScreen.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

ActionListener taskPerformer = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent evt) {
        //logic to create the main UI frame here
        splashScreen.dispose();
    }
};

// set timer to perform action after 3 seconds
Timer timer = new Timer(3000 ,taskPerformer);
timer.setRepeats(false);
timer.start();
copeg
  • 8,290
  • 19
  • 28
  • thanks for the feedback. I want to JFrame instead. I will go with option 3. I hope I don't run into another probelm – ThisIsMi Aug 08 '16 at 19:18
0

Try .setVisible(false) on the first frame.

slambeth
  • 887
  • 5
  • 17
  • If I do that it won't display the first frame – ThisIsMi Aug 08 '16 at 18:15
  • You need to do it in your timer task after you've set the second frame visible. You will probably run into some difficulties there as well accessing the variable. The bottom line ,is that the first frame is still visible because you haven't, well, set it invisible. If you are simply trying to create a delay before the next frame opens, take a look at Thread.sleep(). it'll be much easier and you cont' have to use the task timer to show the second frame. – slambeth Aug 08 '16 at 18:18
  • Also, for what you're doing with the initial "splash" screen, JDialog is likely more appropriate – slambeth Aug 08 '16 at 18:21
  • 1
    This should be used only as a temportary solution because the use of multiple 'JFrames' is *very* bad practice and *very* hard to maintain (I tried it myself once). Take the time to re-build your code and it will be much easier from there. – MasterBlaster Aug 08 '16 at 18:34
  • Certainly... I have a feeling that the OP is simply dabbling in Java/SWING a this point and not looking to create the perfectly designed application. – slambeth Aug 08 '16 at 18:36
  • I was not aware that it is a very bad practice. This is my first exposure to swing so I am learning and want to improve my skill @MasterBlaster Thanks – ThisIsMi Aug 08 '16 at 19:23