在 JFrame 中使用 Java 计时器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20706952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:43:25  来源:igfitidea点击:

Using java timer in JFrame

javaswingtimerjbuttonactionlistener

提问by Ahmet Emin Pehlivan

when handset button pressed I want to green button color change red.After a certain time,again I want to green button.I dont use timer class.Please help me.

当手机按钮按下时我想绿色按钮颜色变成红色。一段时间后,我又想绿色按钮。我不使用计时器类。请帮助我。

For example my code

例如我的代码

        if (e.getSource() == handsetbutton) {

                              text1.setBackground(Color.RED);

                          // What l have to add here?



        }

采纳答案by Paul Samsotha

Run this example. I use a Swing Timer and set the delay to 4 seconds. When the button is pressed, it fires an action to change the color and start the timer. When the 4 seconds is up, the color changes back

运行这个例子。我使用 Swing Timer 并将延迟设置为 4 秒。当按钮被按下时,它会触发一个动作来改变颜色并启动计时器。当 4 秒到时,颜色变回

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class GreenToRed extends JPanel {
    private Color color = Color.RED;                 // global color object
    static JButton button = new JButton("Change");
    private Timer timer = null;

    public GreenToRed(){

        timer = new Timer(4000, new ActionListener(){      // Timer 4 seconds
            public void actionPerformed(ActionEvent e) {
                color = Color.RED;                         // after 4 seconds change back to red
                repaint();
            }
        });


        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                color = Color.GREEN;        // change to green
                repaint();                  // repaint
                timer.start();              // start timer
            }
        });
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame();
        frame.add(new GreenToRed(), BorderLayout.CENTER);
        frame.add(button, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(color);
        g.fillOval(getWidth() / 2 - 50, getHeight() / 2 - 50, 100, 100);
    }

    public Dimension getPreferredSize() {
        return new Dimension(300, 300);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

enter image description here

在此处输入图片说明