栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java Swing按钮颜色

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java Swing按钮颜色

你可以使用

setForeground()
来更改按钮文本的颜色,但是
setBackground()
在某些平台上,相应的文本不能很好地读取。使用
a Border
是一种选择。彩色面板(如下所示)是另一个面板。

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/470562.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号