my last question here之后出现此问题。我想将每个按钮的焦点和失去焦点的背景设置为背景颜色,该颜色位于主菜单(ContentPane为JPanel)下方,因此按钮看起来像标签。在不同的环境中它可能会有所不同,因此它是动态的,因此我无法手动设置。现在,如果我登录ContentPane背景,它会显示238、238、238。如果我将其记录在FocusListener中,它也会显示238、238、238。如果我将按钮的背景直接设置为FocusListener之外的ContentPane背景,则可以,但是如果我设置了在FocusListener内部-似乎未读取和设置值,但是如果我手动设置颜色-则可以。这怎么可能发生?将FocusListener设置为按钮是我在主JPanel初始化中所做的最后一件事。
private void setButtonDefaults(JButton but) {//calls once for each menu button to set defaults
but.setBorderPainted(false);
but.setBackground(Color.DARK_GRAY);
but.setForeground(Color.WHITE);
but.setName(but.getText().toLowerCase());
but.setPreferredSize(buttonSize);
but.addActionListener(this);
//add focus listener
but.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
Color clr = ContentPane.getBackground();
log(clr + "");//logs that color is 238, 238, 238
JButton button = (JButton) e.getSource();
button.setBackground(clr);//value is not read
//button.setBackground(new Color(238, 238, 238)); //value is read
}
@Override
public void focusGained(FocusEvent e) {
//same as focusLost function
}
});
}
private void enableOnlyOne(JButton but) {
/* calls each time when one of menu buttons are pressed.
All buttons are unpressed and changed colors to black and one
button is set as pressed and changes background color to
ContentPane background color
*/
//disable all
setButtonDisabled(MLibrary);
setButtonDisabled(MScheduler);
setButtonDisabled(MBudget);
setButtonDisabled(MReports);
setButtonDisabled(MManage);
setButtonDisabled(MSettings);
//enable one
but.getModel().setPressed(true);
but.setBackground(ContentPane.getBackground());//value is read perfect
but.setForeground(Color.BLACK);
}
private void setButtonDisabled(JButton but) {
but.getModel().setPressed(false);
but.setBackground(Color.DARK_GRAY);
but.setForeground(Color.WHITE);
}
最佳答案
请不要这样,因为我阅读了您以前的主题...,现在我无法抗拒
更好和最简单的方法是将ChangeListener
添加到预期的JButtons
中,然后在stateChanged(ChangeEvent changeEvent)
中,您可以确定哪个JButtons
触发了此事件,对于事件类型,您必须从ButtonModel
中提取该事件的类型
(取决于您,请把这些JButtons
放在Vector, Array or Enumerations
上),例如
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class MyButtonGroup {
private JButton button1 = new JButton("Test Enabled / Disabled");
private JButton button2 = new JButton("Test Enabled / Disabled");
private JButton button3 = new JButton("Test Enabled / Disabled");
public MyButtonGroup() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 0, 10, 10));
button1.addChangeListener(changeListener);
panel.add(button1);
button2.addChangeListener(changeListener);
panel.add(button2);
button3.addChangeListener(changeListener);
panel.add(button3);
JFrame frame = new JFrame("Grouping Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
private ChangeListener changeListener = new ChangeListener() {
public void stateChanged(ChangeEvent changeEvent) {
JButton abstractButton = (JButton) changeEvent.getSource();
if (abstractButton == button1) {
ButtonModel buttonModel = abstractButton.getModel();
boolean armed = buttonModel.isArmed();
boolean pressed = buttonModel.isPressed();
boolean selected = buttonModel.isSelected();
boolean rolover = buttonModel.isRollover();
System.out.println("Changed: " + armed + "/" + pressed + "/" + selected + "/" + rolover);
} else if (abstractButton == button2) {
ButtonModel buttonModel = abstractButton.getModel();
boolean armed = buttonModel.isArmed();
boolean pressed = buttonModel.isPressed();
boolean selected = buttonModel.isSelected();
boolean rolover = buttonModel.isRollover();
System.out.println("Changed: " + armed + "/" + pressed + "/" + selected + "/" + rolover);
}
}
};
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MyButtonGroup xxx = new MyButtonGroup();
}
});
}
}