【问题标题】:Using a Timer in Swing to display a picture for 5 seconds在 Swing 中使用 Timer 显示图片 5 秒
【发布时间】:2015-04-27 21:35:26
【问题描述】:

我正在尝试使用Timer 为我的应用程序制作登录图片。这个想法是,当用户打开应用程序时,他会看到一张图片 5 秒钟,然后应用程序就会启动。

我试过了,你可以在方法shoutoff()看到:

     /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package login;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

/**
 *
 * @author isslam
 */

public class login extends javax.swing.JFrame {
 Timer time;
    /**
     * Creates new form login
     */

    public login() {
        initComponents();
        setLocation(350, 200);
         time = new Timer(5000,new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
              dispose();
         }
     });
     time.setRepeats(false);

    }
 public void shoutoff(){
if (!time.isRunning()) {
        time.start();
    }
 }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setAlwaysOnTop(true);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setLocationByPlatform(true);
        setUndecorated(true);
        setResizable(false);

        jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\isslam\\Desktop\\one_piece_marble_play_by_iviarker-d511vb0.jpg")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1)
        );

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new login().setVisible(true);
               new login().shoutoff();
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel1;
    // End of variables declaration                   
}

【问题讨论】:

  • 听起来您正在尝试创建通常称为“启动画面”或有时称为“启动图像”的内容。这可能有助于您的搜索。

标签: java swing timer


【解决方案1】:

首先在构造函数中创建一次TimerTimer 也应该只关闭 login 的 CURRENT 实例

public login() {
    //...
    time = new Timer(5000,new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
              dispose();
         }
     });
     timer.setRepeats(false);
}

shoutoff 方法中,您启动计时器...

public void shoutoff(){
    if (!time.isRunning()) {
        timer.start();
    }
}

请仔细查看How to use Swing Timers 了解更多详情。

您可能想通读Code Conventions for the Java TM Programming Language,它会让人们更容易阅读您的代码,也让您更容易阅读其他人

【讨论】:

  • 我只是对 OP 的问题感到困惑,因为我看不到什么叫 shoutoff()。 1+ 当然是您通常的出色答案。
  • @HovercraftFullOfEels 我假设他们正在使用多个帧或其他东西:P
  • @MadProgrammer 我添加了你的代码,但你能告诉我将要显示的新框架的新声明放在哪里我的意思是 frame f= new frame();因为还是不行
  • @MadProgrammer 我对代码感到抱歉,因为我正在使用拖放操作
  • 现在可以了,谢谢,你能投票给我吗,我可以投票给你
【解决方案2】:

想法是,当用户打开应用程序时,他会看到一张图片 5 秒钟,然后应用程序就会启动。

您应该使用启动画面。启动画面的优点是图像会立即显示,因为不需要加载整个 Swing 应用程序。

查看 How to Create a Splash Screen 上的 Swing 教程部分,了解更多信息和工作示例。

【讨论】:

  • 谢谢,它看起来不错,但第一个正在工作,我会试试这个 2
猜你喜欢
  • 1970-01-01
  • 2017-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-16
  • 2017-05-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多