質問

How to add button to splash screen

Hi i have to create java splashscreen and during splash it will halt/pause and display ok button. I have made splash screen run every time i compile netbean.

public class Main
{
  static SplashScreen mySplash;    // instantiated by JVM we use it to
                                   // get graphics
 static Graphics2D splashGraphics;  // graphics context for overlay of the
                                   // splash image
 static Rectangle2D.Double splashTextArea;       // area where we draw the text

 static Rectangle2D.Double splashProgressArea;   // area where we draw the progress bar

 static Font font;                               // used to draw our text

 public static void main(String[] args)
 {
   splashInit();           // initialize splash overlay drawing parameters

}

 //create button here
 private static void splashInit()
{

  //do coding here for mannipulating splash screen
  //put ok button here

 }
}

How can we possibly put button to splashscreen? Usually I can only put JButton at JFrame or JPanel. Is it possible to put button on images like splash screen?

reference : Splashscreen beginner netbean

役に立ちましたか?

解決

"Can you kind enough to instruct me on how to make jdialog as splash screen? "

In the example below, here are the things I do.

  • Create a JDialog class and make sure it is undecorated

    public class SplashDialog extends JDialog {
        ....
        setUndecorated(true);
    
  • Give it a background image

    JLabel background = new JLabel(createImage());
    background.setLayout(new BorderLayout());
    setContentPane(background);
    
  • Add a JPanel with a JButton to the background. But set the JPanel not visible, also just to add some styling, give the JPanel a little transparency, so when you do set it visible, you can still see the background image

    final JPanel panel = new JPanel(new GridBagLayout());
    panel.setVisible(false);
    panel.setBackground(new Color(0, 0, 0, 150));
    JButton okBut = new JButton("OK");
    panel.add(okBut);
    background.add(panel);
    
  • Use a javax.swing.Timer to set the delay for the button to appear.

    Timer timer = new Timer(5000, new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            panel.setVisible(true);
        }
    });
    timer.setRepeats(false);
    timer.start();
    
  • Make sure the frame isn't visible, but when the button is pressed, the frame becomes visible and the JDialog disposes

    okBut.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            dispose();
            parent.setVisible(true);
        }
    });
    

Now you have yourself a simple slash screen

Initial splash

enter image description here

Splash After 5 seconds

enter image description here

import java.awt.*;
import java.awt.event.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.*;
import javax.swing.*;

public class SplashDialogDemo {

    public SplashDialogDemo() {
        JPanel panel = new JPanel() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 400);
            }
        };
        panel.setBackground(Color.BLACK);

        JFrame frame = new JFrame();
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBackground(Color.BLACK);
        frame.pack();
        frame.setLocationRelativeTo(null);

        SplashDialog splash = new SplashDialog(frame, true);
    }

    public class SplashDialog extends JDialog {

        public SplashDialog(final JFrame parent, boolean modal) {
            super(parent, modal);

            JLabel background = new JLabel(createImage());
            background.setLayout(new BorderLayout());
            setContentPane(background);

            final JPanel panel = new JPanel(new GridBagLayout());
            panel.setVisible(false);
            panel.setBackground(new Color(0, 0, 0, 150));
            JButton okBut = new JButton("OK");
            panel.add(okBut);
            background.add(panel);

            okBut.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dispose();
                    parent.setVisible(true);
                }
            });

            Timer timer = new Timer(5000, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    panel.setVisible(true);
                }
            });
            timer.setRepeats(false);
            timer.start();

            addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });

            setUndecorated(true);
            pack();
            setLocationRelativeTo(parent);
            setVisible(true);
        }

        private ImageIcon createImage() {
            ImageIcon icon = null;
            try {
                URL url = new URL("http://www.iconsdb.com/icons/download/black/stackoverflow-2-256.png");
                icon = new ImageIcon(url);
            } catch (MalformedURLException ex) {
                Logger.getLogger(SplashDialogDemo.class.getName()).log(Level.SEVERE, null, ex);
            }
            return icon;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(SplashDialogDemo.class.getName()).log(Level.SEVERE, null, ex);
                }
                new SplashDialogDemo();
            }
        });
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top