在我的程式中,我有 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);
}
}
uj5u.com熱心網友回復:
如果您將按鈕存盤在傳遞給函式的串列中,您將能夠遍歷它并為每個按鈕設定背景顏色。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/355619.html
