java - 如何更改按钮的颜色?
问题描述
在我的程序中,我有 2 个按钮:第一个更改框架背景,第二个更改按钮背景(仅用于自身)。关键是程序应该改变程序中每个按钮的按钮背景(不仅是它自己)。我应该如何重写对话框?
class ButtonBackgroundChange implements ActionListener{
private JDialog dialog;
private JColorChooser chooser;
private Color currentBackground;
public ButtonBackgroundChange(JButton button1, Component component, Color currentBackground){
this.currentBackground = currentBackground;
chooser = new JColorChooser();
dialog = JColorChooser.createDialog(component, "Background Color", false /* not modal */, chooser, event -> button1.setBackground(chooser.getColor()), null /* no Cancel button listener */);
}
@Override
public void actionPerformed(ActionEvent e) {
chooser.setColor(currentBackground);
dialog.setVisible(true);
}
}
解决方案
如果您将按钮存储在传递给函数的列表中,您将能够遍历它并为每个按钮设置背景颜色。
推荐阅读
- azure - 如何使用 Bearer 令牌授权一个 Azure Active Directory 应用访问不同的 AAD 应用服务 Web API?
- python - 有没有办法抓取页面上的所有链接,除了包含硒中特定单词的链接?
- python - 如何将 Dataframe 中一列中值的最后 3 位拆分为两个新的 Dataframe?
- c# - 如何在遵循 MVVM 格式的同时访问子 ViewModel 中的事件
- java - 是什么导致我的这部分 java 代码中的空指针异常?
- r - 是否有更有效的匹配版本来搜索数字的重复排列?
- c++ - C++ cin.getline 忽略空行
- javascript - 读取 API 请求时未定义的 const
- java - Java 和 Kotlin 泛型方法之间的区别
- c# - 用鼠标平滑移动子刚体 Unity3D