Java Swing button color

See the English answer > how to set background color of a button in Java GUI? 7

Now my next stage is to add color to the buttons when some database states change

For example:

In a restaurant, I have two tables. When eight people come in for dinner, I will create two tables in my software because people are unattended. I want the buttons of these two tables to be green When processing an order for any of these tables, the button color of the processing table shall be changed to orange When processing, it should be a flashing color How do I do this in Java? I will be responsible for database update. I just want to know how to change the color of the button and add flashing technology

Solution

This is a question and partial answers related to flash components

Appendix: you can learn more in the article how to use buttons In particular, you can use setforegroup () to change the color of button text, but the corresponding setbackground () does not read well on some platforms Using border is another option; The color panel shown below is another

package overflow;

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));
        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();
            }
        });
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>