这是一个计时器弹窗的实现代码,主要包括一个继承自 JFrame 的 TimeOut 类和一个实现 ActionListener 接口的 actionPerformed 方法。在 TimeOut 类中,通过添加 JLabel 和 JButton 实现弹窗的显示和操作。在 actionPerformed 方法中,通过判断事件源是否为确定按钮,来执行相应操作,本例中是隐藏弹窗。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//  计时
package lianliankan;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import lianliankan.map;

public class TimeOut extends JFrame implements ActionListener{

    JLabel label1,label2;
    JButton jbtOK ;
    JPanel panel ;

        

 public TimeOut() {
        
        label1=new JLabel('时间到!!!!');
        label2=new JLabel('');
        jbtOK = new JButton('确定');
        JPanel jpJButton=new JPanel();
        JPanel jpLabel=new JPanel();
        label1.setLayout(new GridLayout(2,0));
        label2.setLayout(new GridLayout(4,0));
        jpLabel.add(label1); 
        jpLabel.add(label2);
                label1.setFont(new java.awt.Font('宋体', 0, 24));
        label1.setForeground(new java.awt.Color(255, 0, 0));
        label1.setText('         时间到!!!请重新开始继续努力!!!');
        jpJButton.add(jbtOK);
        jbtOK.addActionListener(this);
        add(label2,BorderLayout.NORTH);
        add(label1,BorderLayout.CENTER);
        getContentPane().add(jpJButton,BorderLayout.SOUTH);

        
}
  
    public void actionPerformed(ActionEvent e) {
                 if(e.getSource()==jbtOK)
                 {
                     String[] a=null;
                     this.setVisible(false);
                    
                    
                      
                
                 } 
    }
}
                 
    
Java 计时器弹窗实现代码 - TimeOut 类

原文地址: https://www.cveoy.top/t/topic/n5kW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录