Home >Java >javaTutorial >How Can I Create Flashing Buttons with Changing Colors in Java Swing?

How Can I Create Flashing Buttons with Changing Colors in Java Swing?

DDD
DDDOriginal
2024-12-03 08:17:13677browse

How Can I Create Flashing Buttons with Changing Colors in Java Swing?

Flashing Buttons in Java

In Java Swing, changing the appearance of a button can indicate different states, such as when a database status changes. This article addresses how to modify button colors and add a flashing effect to highlight specific statuses.

Changing Button Colors

To change the color of a button, you can use the setForeground() method to alter the text color. The corresponding setBackground() method affects the background color but may not be visible on all platforms. An alternative is to use a colored JPanel as the button's background.

Adding a Flashing Effect

To create a flashing effect, a Timer object can be utilized. The Timer repeatedly calls the actionPerformed() method, allowing you to change the button's color periodically. By setting the Timer interval to a short duration, e.g., 100 milliseconds, you achieve a flashing effect.

Code Example

The following code snippet demonstrates how to create a flashing button that changes colors every second:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class ButtonTest extends JPanel implements ActionListener {

    private static final int N = 4;
    private static final Random rnd = new Random();
    private final Timer timer = new Timer(1000, this);
    private final List<ButtonPanel> panels = new ArrayList<ButtonPanel>();

    public ButtonTest() {
        this.setLayout(new GridLayout(N, N, N, N));
        for (int i = 0; i < N * N; i++) {
            ButtonPanel bp = new ButtonPanel(i);
            panels.add(bp);
            this.add(bp);
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        for (JPanel p : panels) {
            p.setBackground(new Color(rnd.nextInt()));
        }
    }

    private static class ButtonPanel extends JPanel {

        public ButtonPanel(int i) {
            this.setBackground(new Color(rnd.nextInt()));
            this.add(new JButton("Button " + String.valueOf(i)));
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame("ButtonTest");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                ButtonTest bt = new ButtonTest();
                f.add(bt);
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
                bt.timer.start();
            }
        });
    }
}

This code generates a grid of buttons, each with a randomly selected color. The Timer invokes the actionPerformed() method every second, which updates the color of all the buttons to another random color, creating a continuous flashing effect.

The above is the detailed content of How Can I Create Flashing Buttons with Changing Colors in Java Swing?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn