set JWindow Image Icon - Java Swing

Java examples for Swing:JWindow

Description

set JWindow Image Icon

Demo Code


//package com.java2s;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JWindow;

public class Main {
    public static void main(String[] argv) throws Exception {
        String imagePath = "java2s.com";
        int time = 2;
        setWin(imagePath, time);/*w  ww.  j ava 2s  . c  o m*/
    }

    public static void setWin(String imagePath, int time) {
        JWindow win = new JWindow();
        win.add(new JLabel(new ImageIcon(imagePath)));
        win.pack();
        setCenter(win);
        win.setVisible(true);
        try {
            Thread.sleep(time);
        } catch (InterruptedException e1) {
            System.out.println(e1.getMessage());
        }
        win.dispose();
    }

    public static void setCenter(JWindow win) {
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        win.setLocation((d.width - win.getWidth()) / 2,
                (d.height - win.getHeight()) / 2);
    }

    public static void setCenter(JFrame frame) {
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation((d.width - frame.getWidth()) / 2,
                (d.height - frame.getHeight()) / 2);
    }
}

Related Tutorials