动作之间的延迟

2022-01-29 03:03:49 标签 javaswingtimerdelay

我有一个(可能很愚蠢的)问题…

我已经尝试了几个小时来编程一个计时器,它可以在命令之间暂停,但我不能让它工作。这应该看起来像一种“老虎机”效应。已经尝试了一些事情,我绝望了。谁能帮我一下,或者至少给我一点提示,我哪里做错了?

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Window implements ActionListener
{
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    JFrame MAIN = new JFrame("Test");
    Integer[] ZufallsNUMBERen = { 1, 2, 3, 4 };
    String[] Names = { "Name1", "Name2", "Name3", "Name4" };
    JButton B_Start = new JButton();
    Integer NUMBER = 0;
    Timer timer;
    JLabel Name1 = new JLabel(Names[0]);
    JLabel Name2 = new JLabel(Names[1]);
    JLabel Name3 = new JLabel(Names[2]);
    JLabel Name4 = new JLabel(Names[3]);
    public static void main(String[] args)
    {
        new Window().window();
    }
    public void window()
    {
        MAIN.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        MAIN.setLayout(null);
        MAIN.setResizable(false);
        MAIN.setSize( 665, 519);
        MAIN.setForeground(Color.BLACK);
        MAIN.getContentPane().setBackground(Color.decode("#008080"));
        MAIN.setVisible(true);
        int x = (int) ((dimension.getWidth() - MAIN.getWidth()) / 2);
        int y = (int) ((dimension.getHeight() - MAIN.getHeight()) / 2 - 100);
        MAIN.setLocation(x,y);
        B_Start.setBounds(250, 110, 300, 300);
        Name1.setBounds(100,125,200,40);
        Name2.setBounds(100,200,200,40);
        Name3.setBounds(100,275,200,40);
        Name4.setBounds(100,350,200,40);
        Name1.setFont(new Font("SnowCaps", Font.BOLD, 30)); 
        Name2.setFont(new Font("SnowCaps", Font.BOLD, 30)); 
        Name3.setFont(new Font("SnowCaps", Font.BOLD, 30)); 
        Name4.setFont(new Font("SnowCaps", Font.BOLD, 30)); 
        MAIN.add(B_Start);
        MAIN.add(Name1);
        MAIN.add(Name2);
        MAIN.add(Name3);
        MAIN.add(Name4);
        B_Start.addActionListener(this);
        timer = new Timer(500, new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                    NUMBER = NUMBER +1;
                    Slot1();
                    System.out.println(NUMBER);
                    Slot2();
                    System.out.println(NUMBER);
                    Slot3();
                    System.out.println(NUMBER);
                    Slot4();
                    System.out.println(NUMBER);
            }
        });
    }
    public void Slot1(){
        Name1.setText(Names[0]);
        Name2.setText(Names[1]);
        Name3.setText(Names[2]);
        Name4.setText(Names[3]);
        NUMBER = NUMBER +1;
    }
    public void Slot2() {
        Name1.setText(Names[3]);
        Name2.setText(Names[0]);
        Name3.setText(Names[1]);
        Name4.setText(Names[2]);
        NUMBER = NUMBER +1;
    }
    public void Slot3() {
        Name1.setText(Names[2]);
        Name2.setText(Names[3]);
        Name3.setText(Names[0]);
        Name4.setText(Names[1]);
        NUMBER = NUMBER +1;
    }
    public void Slot4() {
        Name1.setText(Names[1]);
        Name2.setText(Names[2]);
        Name3.setText(Names[3]);
        Name4.setText(Names[0]);
        NUMBER = NUMBER +1;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == B_Start)
        {
            if(!timer.isRunning())
                timer.start();
            else
                timer.stop();
        }
    }
}

谢谢

###使用NUMBER(或其他迭代器变量)来打破你的Slot方法调用的计时。

        public void actionPerformed(ActionEvent e){
                NUMBER = NUMBER +1;
                If (NUMBER.intValue() == 1) {
                  Slot1();
                  System.out.println(NUMBER);
                }
                If (NUMBER.intValue() == 2) {
                  Slot2();
                  System.out.println(NUMBER);
                }   
                ...
        }
阅读全文

▼ 版权说明

相关文章也很精彩
推荐内容
更多标签
相关热门
全站排行
随便看看

错说 cuoshuo.com —— 程序员的报错记录

部分内容根据CC版权协议转载;网站内容仅供参考,生产环境使用务必查阅官方文档

辽ICP备19011660号-5

×

扫码关注公众号:职场神器
发送: 1
获取永久解锁本站全部文章的验证码