2

How can I create a JFrame that would look like a JPanel/JLabel?

I need a splash screen that will be displayed while my program does some computing before showing the main window (I know there's a splash screen option in Java, but I need mine to show after loading class, not during).

How can I create that?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Karlovsky120
  • 6,212
  • 8
  • 41
  • 94

2 Answers2

4

Create an undecorated JFrame like this:

JFrame frame = new JFrame();
frame.setUndecorated(true);

However, I suggest you use the java.awt.SplashScreen as it is a piece of code that has been used and tested in so many cases, so it is definitely more stable than the one you can write.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
4

You do not need a JFrame. Here is an example using JWindow.

Click on the button to display the Spash screen, and hide it by click on the the spash screen.

Spash screen

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Properties;

import javax.swing.*;

public class Splashing {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Splashing");
                JButton splash = new JButton("Splash");
                JPanel orangePanel = new JPanel();
                orangePanel.setBackground(Color.orange);
                frame.getContentPane().add(orangePanel, BorderLayout.CENTER);
                frame.getContentPane().add(splash, BorderLayout.SOUTH);
                splash.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Splash s = new Splash();
                        s.setVisible(true);
                    }
                });
                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                frame.setMinimumSize(new Dimension(800, 450));
                frame.setLocationRelativeTo(null); // Center
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    static class Splash extends JWindow {

        private final Properties sysProps = System.getProperties();

        public Splash() {
            super();
            this.setAlwaysOnTop(true);
            initListeners();
            initGUI();
            this.setVisible(false);
        }

        void initListeners() {
            this.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    Splash.this.setVisible(false);
                    Splash.this.dispose();
                }
            });
        }

        void initGUI() {
            final Dimension preferredSize = new Dimension(480, 360);
            setPreferredSize(preferredSize);
            setMinimumSize(preferredSize);
            setMaximumSize(preferredSize);

            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            this.setLocation((screenSize.width - 480) >> 1, (screenSize.height - 360) >> 1);
            this.pack();
            repaint();
        }

        public void paint(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            // TODO Add an appropriate image here
            //g2.drawImage(someImage, 0, 0, this);
            drawAboutText2(g2);
        }

        void drawAboutText2(Graphics2D g2) {
            g2.setFont(new Font("Verdana", Font.PLAIN, 10));
            g2.setColor(new Color(128, 130, 132));
            g2.drawString("All rights reserved", 20, 310);
            g2.drawString("Author: To be decided", 20, 325);
            g2.drawString("http://www.mywebsite.com", 20, 340);

            g2.drawString(getBuild(), 260, 295);
            g2.drawString(getJDK(), 260, 310);
            g2.drawString(getVendor(), 260, 325);
            g2.setColor(new Color(241, 101, 56)); // orange

            g2.drawString(getLicensee(), 260, 340);
        }

        private String getBuild() {
            return "Build# 427 on 15. June 2006";
        }

        private String getJDK() {
            return "JDK: " + sysProps.getProperty("java.version");
        }

        private String getVendor() {
            return "Vendor: " + sysProps.getProperty("java.vendor");
        }

        private String getLicensee() {
            return "Licensee: " + "John Doe";
        }
    }
}
Skjalg
  • 763
  • 5
  • 13