【问题标题】:Appears default JButton when button looses focus当按钮失去焦点时出现默认按钮
【发布时间】:2012-02-05 12:18:56
【问题描述】:

按钮失去焦点: 按钮有焦点:

短代码:

MLibrary = new JButton();
MenuPane.add(MLibrary, new AnchorConstraint(0, 0, 0, 0, AnchorConstraint.ANCHOR_ABS, AnchorConstraint.ANCHOR_NONE, AnchorConstraint.ANCHOR_ABS, AnchorConstraint.ANCHOR_ABS));
MLibrary.setText("Library");
setButtonDefaults(MLibrary);

MScheduler = new JButton();
MenuPane.add(MScheduler, new AnchorConstraint(0, 0, 0, 110, AnchorConstraint.ANCHOR_ABS, AnchorConstraint.ANCHOR_NONE, AnchorConstraint.ANCHOR_ABS, AnchorConstraint.ANCHOR_ABS));
MScheduler.setText("Scheduler");
setButtonDefaults(MScheduler);

private void setButtonDefaults(JButton but) { //calls in JPanel init only for setting button defaults
    but.setBorderPainted(false);
    but.setBackground(Color.DARK_GRAY);
    but.setForeground(Color.WHITE);
    but.setName(but.getText().toLowerCase());
    but.setPreferredSize(buttonSize);
    but.addActionListener(this);
}

private void enableOnlyOne(JButton but) {//calls each time when one of menu buttons are pressed. but is pressed button
    // TODO Auto-generated method stub
    setButtonDisabled(MLibrary);
    setButtonDisabled(MScheduler);
    setButtonDisabled(MBudget);
    setButtonDisabled(MReports);
    setButtonDisabled(MManage);
    setButtonDisabled(MSettings);
    //enable one
    //but.setFocusPainted(true);
    but.getModel().setPressed(true);
    but.setBackground(ContentPane.getBackground());
    but.setForeground(Color.BLACK);
}

private void setButtonDisabled(JButton but) { //sets button unpressed
    but.getModel().setPressed(false);
    but.setBackground(Color.DARK_GRAY);
    but.setForeground(Color.WHITE);
}

所以,我的问题是如何编码每个按钮,当按钮失去焦点时,它将是浅灰色背景,而不是默认外观

【问题讨论】:

  • @AndrewThompson PLAF 是什么?
  • @PooLaS : LAF 代表 Look And Feel,不能说 P 代表什么 :-) 问候

标签: java swing jbutton lost-focus


【解决方案1】:

为每个按钮添加一个Focus Listener并实现函数public void focusLost(FocusEvent e)并在该函数内部设置按钮的背景。

来源: http://docs.oracle.com/javase/tutorial/uiswing/events/focuslistener.html

更新:

在focusLost(FocusEvent e)里面,输入以下代码:

JButton button = (JButton) e.getSource();
e.setBackground(Color.Gray);

【讨论】:

  • @PooLaS :在您的 focusLost 方法中执行此操作。 JButton 按钮 = (JButton)e.getSource(); if (button == MLibrary) {Mlibrary.setBackground(Color.LIGHT_GRAY);} else if (button == MScheduler){MScheduler.setBackground(Color.LIGHT_GRAY);} 希望能有所帮助。
  • 只有一个问题,为什么你把 if 语句 if 放在 if 和 else 你做同样的工作?
  • @AdelBoutros e.getComponent().setBackground(ContentPane.getBackground()); 我用它来设置背景,但我无法访问 ContentPane,因为它在 FocusListener 之外。我怎样才能做到这一点? ContentPane 是主菜单下方的 jpanel。这次我尝试制作相同颜色的按钮和 contentPane 背景,所以它看起来像标签按钮
  • @Adel Boutros :没错,今天好像我的大脑卡住了,呵呵。他可以简单地在他的 focusLost 方法中写这个。 e.getSource().setBackground(Color.LIGHT_GRAY);一切都会奏效。大声笑,我的坏,看来我今天不应该回答任何人。问候
  • @GagandeepBali 你不能只写e.getSource().setBackground(Color.LIGHT_GRAY);。我先试了一下(在你发帖之前),eclipse不让我,所以我把它改成了e.getComponent().setBackground(Color.LIGHT_GREY);
猜你喜欢
  • 2015-07-05
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 2013-05-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多