好像JPanel背景不是在FocusListener中读取的 [英] Seems like JPanel background isn't read in FocusListener

查看:133
本文介绍了好像JPanel背景不是在FocusListener中读取的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题出现在我最后一个问题之后。我想设置每个按钮焦点和丢失焦点背景到主菜单下面的背景颜色(ContentPane是JPanel),所以按钮看起来像标签。它在不同的环境中有所不同,所以它是动态的,所以我不能手动设置它。现在,如果我记录ContentPane背景它说238,238,238。如果我记录它在FocusListener - 它也说238,238,238。如果我直接设置按钮的背景到FocusListener外部的ContentPane背景 - 它的工作,但如果我设置里面的FocusListener - 它看起来像值不被读取和设置,但如果我手动设置颜色 - 它的作品。这怎么可能发生?将FocusListener设置为按钮是我在主JPanel的初始化过程中所做的最后一件事。

  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);
//添加焦点侦听器
but.addFocusListener(new FocusListener(){
@Override
public void focusLost(FocusEvent e){
Color clr = ContentPane.getBackground ();
log(clr +); //记录颜色是238,238,238
JButton button =(JButton)e.getSource();
button.setBackground ); // 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){
/ *每次按下某个菜单按钮时都会调用它。
所有按钮都是未压缩的,并将颜色更改为黑色,并将一个
按钮设置为按下并将背景颜色更改为
ContentPane背景颜色
* /
//禁用所有
setButtonDisabled(MLibrary);
setButtonDisabled(MScheduler);
setButtonDisabled(MBudget);
setButtonDisabled(MReports);
setButtonDisabled(MManage);
setButtonDisabled(MSettings);
//启用一个
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);


解决方案

自己回答:

  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);
//添加焦点侦听器
final颜色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 =+
JButton button =(JButton)e.getSource();
button.setBackground(new Color(r,g,b));
}
$ b $ @覆盖
public void focusGained(FocusEvent e){
JButton button =(JButton)e.getSource();
button.setBackground(new Color r,g,b));
}
});



$ b $ p
$ b

感谢所有试图帮助我的人们


This problem appeared after my last question here. I want to set each button focused and lost focus background to background color which is below main menu (ContentPane which is JPanel), so buttons look like tabs. It couuld be different in different environments, so it is dynamic, so I can't set it manually. Now, If I log ContentPane background it says 238, 238, 238. If I log it inside FocusListener - it also states 238, 238, 238. If I directly set button's background to ContentPane background outside FocusListener - it works, but if I set inside FocusListener - it looks like value is not read and set, but if I set color manually - it works. How this could happen? Setting FocusListener to buttons is the last thing what I do in initialization of main 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);
}

解决方案

It is so good to find out answer by myself:

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

Thanks to everyone who tried (and did) to help me

这篇关于好像JPanel背景不是在FocusListener中读取的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆