Java Swing 按钮颜色 [英] Java Swing button colors

查看:39
本文介绍了Java Swing 按钮颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 NET Beans IDE 在 LINUX 中开发我的应用程序.我使用了合成包来生成新的外观和感觉.到目前为止一切都很好.

I am using NET Beans IDE for developing my application in LINUX. I have used synthetica package to generate new look and feel. All is well till now.

现在我的下一阶段是在某些数据库状态发生变化时为按钮添加颜色.

Now my next stage is to add colors to buttons when some database status changes.

例如:

在一家餐厅,我有 2 张桌子,当 8 个人进来用餐时,我将在我的软件中创建 2 张桌子,因为人们无人看管,我希望这 2 张桌子的按钮是绿色的.当为这些表中的任何一个处理订单时,已处理表的按钮颜色应更改为橙色.当它在加工中时,它应该是闪烁的颜色.如何在 java 中做到这一点?我将负责数据库更新,我只想知道如何更改按钮的颜色和添加闪烁技术.

In a restaurant i have 2 tables and when 8 people came in to dine and i will create 2 table in my software since the people are unattended i want the buttons to those 2 tables to be green. When the order is processed for any of those tables the button color of the processed table should be changed to orange. When it is under processing it should be flashing color. How to do this in java ? I will take care of database update i just want to know how to change the colors of the buttons and adding flashing technique.

推荐答案

这里是一个 问题和几个答案组件.

附录:您可以在文章中了解更多信息使用按钮.特别是,您可以使用 setForeground() 来更改按钮文本的颜色,但相应的 setBackground() 在某些平台上读取效果不佳.使用 Border 是其中之一选择;一个彩色面板,如下所示,是另一个.

Addendum: You can learn more in the article How to Use Buttons. In particular, you can use setForeground() to change the color of a button's text, but the corresponding setBackground() doesn't read well on some platforms. Using a Border is one alternative; a colored 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, 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();
            }
        });
    }
}

这篇关于Java Swing 按钮颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆