在 Swing 中使用计时器显示图片 5 秒

2024-01-04

我正在尝试使用以下方法为我的应用程序制作登录图片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                   
}

首先创建Timer一旦进入构造函数。这Timer还应该只创建 CURRENT 实例login close

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

In the shoutoff方法,你启动计时器...

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

仔细看看如何使用 Swing 定时器 http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html更多细节。

您可能想通读一下Java TM 编程语言的代码约定 http://www.oracle.com/technetwork/java/codeconvtoc-136057.html,这将使人们更容易阅读您的代码,也让您更轻松地阅读其他人的代码

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Swing 中使用计时器显示图片 5 秒 的相关文章

随机推荐

  • 现有应用程序的转换以兼容iphone5? [复制]

    这个问题在这里已经有答案了 可能的重复 如何开发或迁移适用于 iPhone 5 屏幕分辨率的应用程序 https stackoverflow com questions 12395200 how to develop or migrate
  • 如何在 activerecord 之外创建 activerecord 样式验证?

    我正在为我公司编写的软件开发一个测试框架 我们的产品是基于网络的 在运行 RESTful 请求后 我想处理结果 我希望能够在每个命令类中进行 activerecord 类型验证 以便在运行后 结果会自动针对所有 验证 进行测试 但是 我不知
  • 本地图像未在发布和 TestFlight 中渲染

    使用react native v0 63 4 并且所有本地图像在开发模式下加载良好
  • WPF - 从流加载字体?

    我有一个包含字体文件 ttf 内容的 MemoryStream 我希望能够从该流创建 FontFamily WPF 对象WITHOUT将流的内容写入磁盘 我知道这对于 System Drawing FontFamily 是可能的 但我不知道
  • Javascript背景颜色随着淡入淡出而闪烁

    我有一个 div 需要有持续闪烁的背景颜色 我想要它做的就是淡入透明的 to red并循环返回 我见过几个examples这样做 但它们都会影响整个内容div而不仅仅是background color 其他例子有闪烁background但这
  • UIWebView canGoBack 和 canGoForward 始终返回 NO

    我正在尝试将数据直接加载到UIWebView webView loadData data MIMEType text html textEncodingName utf 8 baseURL nil 数据是一些包含一些外部链接的html字符串
  • 去掉最后一个逗号

    当我的程序打印出整数 5 到 1 时 我需要帮助去掉最后一个逗号 int i for i 10 i gt 1 i if i 2 0 System out print i 2 它打印出来5 4 3 2 1 我想要打印出来5 4 3 2 1 一
  • System.Array 是否对值类型执行装箱?

    我最近做了一些粗略的性能测量List lt gt vs 对于一系列小型结构 System Array 似乎轻而易举地获胜了 所以我就这么做了 我才刚刚意识到 System Array 包含对象类型 所以用结构填充它肯定会导致装箱发生吗 然而
  • 如何在 Node.js 中使用 JQuery 选择器

    我正在尝试从硬盘驱动器中的 HTML 文件中提取电子邮件信息 如果我在 Firefox 中加载文件并运行 jQuerify bookmarklet 我可以成功使用以下选择器 函数 window jQuery a iEmail each fu
  • Rx:如何立即响应并限制后续请求

    我想设置一个 Rx 订阅 它可以立即响应事件 然后忽略指定 冷却 期内发生的后续事件 开箱即用的 Throttle Buffer 方法仅在超时结束后才会响应 这并不是我所需要的 这是一些设置场景并使用 Throttle 的代码 这不是我想要
  • 如何将 x,y,y0 以外的属性附加到 d3 中的流图层?

    我是 d3 新手 我一直在尝试自定义 Streamgraph 示例 http mbostock github com d3 ex stream html http mbostock github com d3 ex stream html
  • Vue debounce 方法?

    我知道 Vue js 内置了在输入字段上进行反跳的功能 我创建了一个滑块 它触发一个不使用输入字段的方法 我想知道是否可以利用方法内部的去抖功能 除了简单地向输入添加去抖功能之外 是否还可以使用此功能 或者我需要为此编写自己的功能吗 我刚刚
  • 从 web.config 重定向到 root 登录页面

    会话过期时 我将用户重定向到登录页面 Login aspx是在根中 我在 web config 文件中声明了这样的路径
  • PHP“while”循环或另一个“while”循环中的“if”语句?

    我有一个返回 HTML 表的 PHP MySQL 查询 但我被困在需要进行第二个查询的部分while循环该查询 我不知道从这里该去哪里 我尝试了几种不同的方法 我希望它循环并给我第一组数据 然后使用 Order ID 并获取第二组数据并将第
  • 如何在Android智能手机中使用加速度传感器找到位移距离?

    我有一部 Android 智能手机 其中包含加速器传感器 指南针传感器和陀螺仪传感器 我想使用这个传感器计算位移距离 我已经尝试过基本方法 即 final velocity initial velocity acceleration tim
  • 切换仅包含相同文本的 div

    HTML div class event div class clicker click div div class title a href first event a div div div class event div class
  • 检索 Facebook 粉丝姓名

    我正在尝试获取我管理的 Facebook 粉丝页面的粉丝姓名 我做了一些窥探 显然 FB API 不支持这一点 但 Facebook 实际上使用 AJAX JSON 来填充列表 无论如何 任何人都可以建议一种方法来让我自己调用并以纯文本形式
  • 超小屏幕上的居中按钮 Material-UI React 不起作用(justify-xs-center)

    我尝试了一切 但似乎我错过了一些东西 当屏幕特别小时 我花了很多时间在网格内制作一个按钮中心 这段代码工作完美 但问题是我希望我的按钮仅在屏幕特别小时而不是在所有尺寸上居中 工作代码 Grid item xs 12 gt
  • 不显示python OpenCV错误

    我正在使用 OpenCV 和 python 更具体地说findTransformECC的功能cv2执行图像配准 如果算法不收敛 此函数可能会引发错误 我用一个简单的方法捕获了错误try except指令 然后我处理错误 但是 OpenCV
  • 在 Swing 中使用计时器显示图片 5 秒

    我正在尝试使用以下方法为我的应用程序制作登录图片Timer 这个想法是 当用户打开应用程序时 他将看到一张图片 5 秒钟 然后应用程序将启动 我尝试过 正如你在方法中看到的shoutoff To change this license he