java swing 颜色,Java Swing按钮颜色

在Linux环境中使用NETBeans IDE开发应用时,如何根据数据库状态改变按钮颜色?文章提供了一个示例,当餐厅桌子被占用时,对应按钮变为绿色;处理订单后变为橙色,处理中则闪烁。通过定时器和随机颜色更新实现按钮颜色变化,使用`setForeground()`和`setBackground()`方法调整颜色,但注意到某些平台的显示效果。同时,通过创建带颜色的面板实现闪烁效果。

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.

For example:

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.

解决方案

Here is a question and several answers related to flashing a component.

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.

puaIA.png

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 panels = new ArrayList();

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();

}

});

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值