此问题出现在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);
}
答案 0 :(得分:3)
请不要这样做,因为我读了你以前的帖子......,现在我无法抗拒
更好,更简单的方法是将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();
}
});
}
}
答案 1 :(得分:1)
由于某些L& Fs不显示背景颜色的变化是一种有用的方法,您可能需要考虑另一种突出显示形式:不同的边框,改变的前景色,自定义图标或合适的背景面板。更多here。
答案 2 :(得分:0)
自己找答案真是太好了:
private void setButtonDefaults(JButton but) {
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
final Color clr = ContentPane.getBackground();
final int r = clr.getRed();
final int g = clr.getGreen();
final int b = clr.getBlue();
but.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
log("r = " + r + ", g = " + g + ", b = " + b);
JButton button = (JButton) e.getSource();
button.setBackground(new Color(r, g, b));
}
@Override
public void focusGained(FocusEvent e) {
JButton button = (JButton) e.getSource();
button.setBackground(new Color(r, g, b));
}
});
}
感谢所有尝试(并且做过)帮助我的人