简体   繁体   English

鼠标悬停时更改JButton背景颜色(不翻转)

[英]Change JButton background color when mouse on it (not rollover)

I created a JFrame with 5 buttons, 1 of them is a play button which user could press to play an audio file, the rest of them are the choices which user are required to choose the correct one after listening to the audio. 我创建了一个具有5个按钮的JFrame,其中1个是播放按钮,用户可以按该按钮来播放音频文件,其余的则是用户在听完音频后需要选择正确的按钮的选择。

I want the buttons to change color when the mouse is over them. 我希望按钮在鼠标悬停于其上方时更改颜色。

I disabled the choice buttons then play the audio file, used SwingWorker to wait until the audio file is finished then enable the buttons again. 我禁用了选择按钮,然后播放音频文件,并使用SwingWorker等到音频文件完成后再启用按钮。

Firstly I used ButtonModel and changeListener to set background color. 首先,我使用ButtonModel和changeListener设置背景色。 Of course they worked well when mouse rollover them after the audio clip finished playing. 当然,当音频剪辑播放完毕后,将鼠标悬停在鼠标上时,它们会很好地工作。

But I found a problem is that, if i moved my mouse to the button while the audio was playing, the button was enabled after the audio finished but it did not change its color. 但是我发现一个问题是,如果在播放音频时将鼠标移到按钮上,则在音频播放完成后启用了按钮,但它没有改变颜色。

Appreciate for any help! 感谢您的帮助!

TLDR: There are two situations TLDR:有两种情况

(1) I pressed the play button, did not move the mouse pointer such that the pointer stay over the play button. (1)我按下了播放按钮,但没有移动鼠标指针,使指针停留在播放按钮上方。 AFTER the 4 buttons were enabled, I moved the mouse pointer to any one of them, the changelistener works prefectly. 启用4个按钮之后 ,将鼠标指针移至其中任意一个,更改侦听器便能正常工作。

(2) I pressed the play button, moved the mouse pointer to any one of the 4 buttons BEFORE the 4 buttons were enabled, the color of the buttons did not change after the buttons were enabled. (2)我按下了播放按钮,然后将鼠标指针移到了这四个按钮中的任何一个上,在启用这四个按钮之前 ,在启用这些按钮之后,按钮的颜色没有改变。

The complete demo of the JFrame is attached, it could be run perfectly with MigLayout libraries. 随附了JFrame的完整演示,可以与MigLayout库完美地运行。

public class demo extends JFrame {

private JPanel contentPane, panelBtn, panelWord;
private JButton btnPlay, btnStop, btnWord0, btnWord1, btnWord2, btnWord3;
public static void main (String args[]){
    demo testing = new demo();
}

public demo(){
    super("Demonstrating problem");
    init();
    setVisible(true);
}

private void init(){
    customizedChangeListener clBtn = new customizedChangeListener();
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setBounds(100, 100, 673, 512);
    contentPane = new JPanel();
    setContentPane(contentPane);
    contentPane.setLayout(new BorderLayout(0, 0));
    panelBtn = new JPanel();
    panelBtn.setLayout(new MigLayout("", "[grow][]", ""));
    contentPane.add(panelBtn, BorderLayout.SOUTH);
    panelWord = new JPanel();
    panelWord.setLayout(new MigLayout("", "[grow,fill][grow,fill]", "[grow,fill][grow,fill]"));
    contentPane.add(panelWord, BorderLayout.CENTER);

    btnPlay = new JButton("Play");
    btnPlay.addActionListener(new playAcitonListener());
    panelBtn.add(btnPlay, "cell 0 0, align center");

    btnWord0 = new JButton("A");
    btnWord0.addChangeListener(clBtn);
    panelWord.add(btnWord0, "cell 0 0");

    btnWord1 = new JButton("B");
    btnWord1.addChangeListener(clBtn);
    panelWord.add(btnWord1, "cell 1 0");

    btnWord2 = new JButton("C");
    btnWord2.addChangeListener(clBtn);
    panelWord.add(btnWord2, "cell 0 1");

    btnWord3 = new JButton("D");
    btnWord3.addChangeListener(clBtn);
    panelWord.add(btnWord3, "cell 1 1");

    enableBtn(false);
}

private class customizedChangeListener implements ChangeListener{
    JButton rolledBtn;
    @Override
    public void stateChanged(ChangeEvent e) {
        rolledBtn = (JButton)e.getSource();
        if (rolledBtn.getModel().isRollover())
            rolledBtn.setBackground(Color.green);
        else
            rolledBtn.setBackground(UIManager.getColor("Button.background"));
    }
}

private class playAcitonListener implements ActionListener{
    JButton pressedBtn;
    @Override
    public void actionPerformed(ActionEvent e) {
        pressedBtn = (JButton)e.getSource();
        if (pressedBtn.equals(btnPlay)){
            btnPlay.setEnabled(false);
            new SwingWorker<Void ,Void>(){

                @Override
                protected Void doInBackground() throws Exception {
                    Thread.sleep(2000);
                    return null;
                }

                @Override
                protected void done() {
                    enableBtn(true);
                }
            }.execute();
        }
    }
}

private void enableBtn (boolean idx) {
    if (idx == true){
        btnWord0.setEnabled(true);
        btnWord1.setEnabled(true);
        btnWord2.setEnabled(true);
        btnWord3.setEnabled(true);
    }
    else if (idx == false){
        btnWord0.setEnabled(false);
        btnWord1.setEnabled(false);
        btnWord2.setEnabled(false);
        btnWord3.setEnabled(false);
    }
}

} }

You can detect when mouse is over button or some other element with this code: 您可以使用以下代码检测鼠标何时位于按钮或其他元素上:

    button.addMouseListener( new MouseAdapter() {
    public void mouseEntered( MouseEvent e ) {
        // your code here (color of button)
    }
} );

And like @DebilsHnd said change color back with mouseExited(). 就像@DebilsHnd所说的那样,可以使用mouseExited()改变颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM