禁用JButtons的选择发光

时间:2013-04-30 12:57:14

标签: java swing button jbutton

我想知道如何禁用使用鼠标光标悬停在JButton上时出现的发光效果。

2 个答案:

答案 0 :(得分:6)

我认为你在寻找:

button.setRolloverEnabled(false);

答案 1 :(得分:3)

  

我想知道如何禁用出现的发光效果   当你用鼠标光标悬停在JButton上时。

JButton.setFocusable(false);

修改

MysticKiller 98 wrote that doesn't work, 

然后发光效果等于JButton.setBorderPainted(false);

import java.awt.Insets;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class MyButtonWithIcon extends JFrame {

    private static final long serialVersionUID = 1L;
    private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
    private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
    private Icon warnIcon = UIManager.getIcon("OptionPane.warningIcon");

    public MyButtonWithIcon() {
        final JButton toggleButton = new JButton();
        toggleButton.setBorderPainted(false);
        //toggleButton.setBorder(null);
        //toggleButton.setFocusable(false);
        //toggleButton.setMargin(new Insets(0, 0, 0, 0));
        //toggleButton.setContentAreaFilled(false);
        toggleButton.setIcon((errorIcon));
        toggleButton.setSelectedIcon(infoIcon);
        toggleButton.setRolloverIcon((infoIcon));
        toggleButton.setPressedIcon(warnIcon);
        toggleButton.setDisabledIcon(warnIcon);
        add(toggleButton);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                MyButtonWithIcon t = new MyButtonWithIcon();
            }
        });
    }
}