如何更正我的Java代码?

时间:2015-03-22 12:07:52

标签: java swing awt

我编写了这个Java程序,它调整大小并显示位于C:/中的图像,并在显示它的五秒钟后关闭。虽然该类正确编译,但我无法在五秒钟后关闭它(不隐藏它)。 这是代码:

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;
            }        
        }
    }
}

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

为您的问题编写解决方案的方式对于专业程序员来说有点肮脏,但由于您是初学者,我只需稍微修改您的代码: - )

只有您的方法closeCall()需要进行一些调整:

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;
        }        
    }
}

我正在使用Calendar.getInstance().getTime().getTime()来获取当前时间(以毫秒为单位)。我们需要增加5秒,所以这是5 * 1000毫秒。在循环中,只要currentTime大于exitTime,屏幕就会关闭。