I have written this Java program that resizes and displays an image located in C:/, and closes in five seconds of it being displayed. Although the class compiles correctly, I am unable to close it (not hide it) after five seconds. Here is the code :
import java.awt.*;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.awt.RenderingHints;
import java.io.*;
import java.io.File;
import java.lang.*;
import java.math.*;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UIManager;
public class loadscreen extends JFrame
{
private JMenuBar menuBar;
private JPanel panel1;
private static JFrame JFrame1;
//Constructor
public loadscreen()
{
final BufferedImage img=new ImgUtils().scaleImage(750,500,"C:/default_logo_img.jpg");
this.setTitle(" sCool ");
this.setSize(750,500);
//pane with null layout
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setPreferredSize(new Dimension(750,500));
contentPane.setBackground(new Color(255,255,255));
JLabel label=new JLabel(new ImageIcon((Image)img));
//adding components to contentPane panel
contentPane.add(label, BorderLayout.CENTER);
//adding panel to JFrame and seting of window position and close operation
this.add(contentPane);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.pack();
this.setVisible(true);
this.setResizable(false);
JFrame1 = this;
}
public static void main()
{
System.setProperty("swing.defaultlaf", "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new loadscreen();
}
});
loadscreen.closeCall();
}
public static void closeCall()
{
Calendar today = new GregorianCalendar();
int time = today.get(Calendar.SECOND);
int time2 = time + 5;
for(int i=1; i>0; i++)
{
if (today.get(Calendar.SECOND) == time2)
{
JFrame1.dispatchEvent(new WindowEvent(JFrame1, WindowEvent.WINDOW_CLOSING));
System.exit(0);
break;
}
}
}
}
Please help me with this.
Your way of writing a solution for your problem is kind of dirty to professional programmers, but since you are a beginner, I'll just fix your code with some small changes :-)
Only your method closeCall()
needs some adjustment:
public static void closeCall()
{
Long exitTime = Calendar.getInstance().getTime().getTime() + 5 * 1000;
for(int i=1; i>0; i++)
{
Long currentTime = Calendar.getInstance().getTime().getTime();
if (currentTime >= exitTime)
{
JFrame1.dispatchEvent(new WindowEvent(JFrame1, WindowEvent.WINDOW_CLOSING));
System.exit(0);
break;
}
}
}
I'm using Calendar.getInstance().getTime().getTime()
to get the current time in milliseconds. We need to add 5 seconds, so that's 5 * 1000 in milliseconds. In the loop, whenever the currentTime is larger than the exitTime, the screen is closed.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments