1

New here - my first question. Anyway, I am here because I want to make a JFrame, that is timed for 10000 milliseconds, I think, and then when it closes, it should open another (which is in another class). I already did the timer part, not the 'closing the timed JFrame, and opening another' part.

I remember doing this, and found an answer. It went something like NewClass.show() ('NewClass' is the class name that should open) and then you type in OldClass.dispose() ('OldClass' is the class name that should close).

Here is my code so far:

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class SplashScreen extends JPanel {

public SplashScreen() {
 setOpaque(false);
 setLayout(new FlowLayout());
}

public static void main(String[] args) {

    final JFrame frame = new JFrame("Loading game...");
    frame.setSize(800, 600);
    frame.setResizable(false);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    SplashScreen background = new SplashScreen();
    frame.add(background);

    Timer timer = new Timer(10000, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            frame.setVisible(false);
            frame.dispose();
           //I want to place my code here so then this class will close, and then the other class will open
        }
    });
    timer.setRepeats(false);
    timer.start();

    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Image image = toolkit.getImage("Waiting.png");
    Point hotSpot = new Point(0,0);
    Cursor cursor = toolkit.createCustomCursor(image, hotSpot, "Cursor");
    frame.setCursor(cursor);

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    int w = frame.getSize().width;
    int h = frame.getSize().height;
    int x = (dim.width - w) / 2;
    int y = (dim.height - h) / 2;

    frame.setLocation(x, y);

    JButton button = new JButton();

    JPanel panel = new JPanel();

}

public void paint(Graphics g) {
    Image a=Toolkit.getDefaultToolkit().getImage("Splash Screen.gif");
    g.drawImage(a,0,0,getSize().width,getSize().height,this);
    super.paint(g);

    }
}

I didn't make the second class (which would be called 'LoadingScreen.class', but I will, and it will just have 'JSomethings' or whatever (like JFrame, JPanel, etc...)

I can make the second class, but all I want is the first class to close after the timer finishes at 10 seconds, or 10000 milliseconds, and then automatically open the second class.

Thanks

  • 2
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Feb 28 '13 at 09:21
  • *"which would be called 'SplashScreen.class'"* Why not use [`java.awt.SplashScreen`](http://docs.oracle.com/javase/7/docs/api/java/awt/SplashScreen.html) for this? Java Web Start also supports a splash image. – Andrew Thompson Feb 28 '13 at 09:25

3 Answers3

1

Try adding the call to your second class like below

Timer timer = new Timer(10000, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        frame.setVisible(false);
        frame.dispose();
       //I want to place my code here so then this class will close, and then the other class will open

       //SplashScreen screen = new SplashScreen();
       //screen.showGUI();
    }
});

Another good practice is to call frame.setVisible(true) at the last so that you wont find any drift in the frame position on the screen.

prasanth
  • 3,502
  • 4
  • 28
  • 42
  • thank you for the second advice - adding `frame.setVisible(true)` last, but I still don't get what you mean about calling that code to the second class. I'm new to Java, and this is my first program I'm making without using YouTube. when I try what you did, it just showed the first class, but didn't show the JFrame in the second class. Did I do something wrong? I got confused. Basically, all I want is to dispose the LoadingScreen.class file, and when SplashScreen.class has disposed after the 10 seconds, LoadingScreen.class should show. Sometimes, Java can be a little annoying and difficult. –  Feb 28 '13 at 10:56
0

Suppose your second class is like,

public class LoadingScreen extends JFrame   
{    
    public LoadingScreen(String title)  
    {   
        super(title);  
    }

    public void showGUI()   
    {
        //  Do whatever you want
        setSize(500,500);
        setVisible(true);
    }
}

You just have to call,

LoadingScreen loadScreen = new LoadingScreen("Loading screen....");
loadscreen.showGUI();
Patrick
  • 1,717
  • 7
  • 21
  • 28
prasanth
  • 3,502
  • 4
  • 28
  • 42
0

when you call frame.setVisoble(true) dont you get an error non static method cannot be referenced from static context?