How can I correct my Java code ?

user4699641

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.

Manu

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How can I get my code to return the correct variable?

From Dev

Android/Java : Is my code correct?

From Dev

How can I make FireFox understand the correct height of my divs?

From Dev

How can I add the correct protobuf libraries on my project?

From Dev

How can I implement ImageJ API in my Java code?

From Dev

How can i adapt my code to Asynktask

From Dev

How can I organize my Java code?

From Dev

How can I correct my code to produce a nested dictionary?

From Dev

How can i check to see if my WordPress code is correct?

From Dev

How can I link my JSON extension to the correct Ruby version?

From Dev

how can i condense my XML and Java code to make my app run smoother

From Dev

How do I make my automated Client-Server Java networking code output in the correct sequence?

From Dev

How can I improve my SQL code for correct results?

From Dev

Is my code correct and what improvements can I make?

From Dev

How can i optimize my Jquery code?

From Dev

How should i correct my proxy settings for my code to become correct?

From Dev

How can I use the Factory pattern to refactor my Java code?

From Dev

How can I protect my robot code?

From Dev

How can i get the correct information to display in my listbox?

From Dev

How can i update the midpoint in my code?

From Dev

How can I permanently correct a typo in my last bash command?

From Dev

how can i condense my XML and Java code to make my app run smoother

From Dev

How to correct my java recursive method so that I can use it to compute big value number?

From Dev

How can I easily transfer my serverside variables from the Java code to the client-side code?

From Dev

How can I improve my SQL code for correct results?

From Dev

How can i check the winner in the my Connect four java code?

From Dev

How can I add exponent in my code

From Dev

JAVA,how can I loop my code

From Dev

how can i speed up my code?

Related Related

  1. 1

    How can I get my code to return the correct variable?

  2. 2

    Android/Java : Is my code correct?

  3. 3

    How can I make FireFox understand the correct height of my divs?

  4. 4

    How can I add the correct protobuf libraries on my project?

  5. 5

    How can I implement ImageJ API in my Java code?

  6. 6

    How can i adapt my code to Asynktask

  7. 7

    How can I organize my Java code?

  8. 8

    How can I correct my code to produce a nested dictionary?

  9. 9

    How can i check to see if my WordPress code is correct?

  10. 10

    How can I link my JSON extension to the correct Ruby version?

  11. 11

    how can i condense my XML and Java code to make my app run smoother

  12. 12

    How do I make my automated Client-Server Java networking code output in the correct sequence?

  13. 13

    How can I improve my SQL code for correct results?

  14. 14

    Is my code correct and what improvements can I make?

  15. 15

    How can i optimize my Jquery code?

  16. 16

    How should i correct my proxy settings for my code to become correct?

  17. 17

    How can I use the Factory pattern to refactor my Java code?

  18. 18

    How can I protect my robot code?

  19. 19

    How can i get the correct information to display in my listbox?

  20. 20

    How can i update the midpoint in my code?

  21. 21

    How can I permanently correct a typo in my last bash command?

  22. 22

    how can i condense my XML and Java code to make my app run smoother

  23. 23

    How to correct my java recursive method so that I can use it to compute big value number?

  24. 24

    How can I easily transfer my serverside variables from the Java code to the client-side code?

  25. 25

    How can I improve my SQL code for correct results?

  26. 26

    How can i check the winner in the my Connect four java code?

  27. 27

    How can I add exponent in my code

  28. 28

    JAVA,how can I loop my code

  29. 29

    how can i speed up my code?

HotTag

Archive