JButton() only working when mouse hovers

家住魔仙堡 提交于 2019-11-26 19:07:29

Likely you have a layout problem where you're trying to add JButtons with absolute bounds into a container that uses a non-null layout manager. Suggestions

  • Do not use setBounds and absolute positioning to size and place your components.
  • Read up on and use the layout managers to do this heavy lifting for you: Lesson: Laying Out Components Within a Container
  • Don't forget to call pack() on your JFrame after adding all components
  • Call setVisible(true) after calling pack() and again call both only after adding all components to your GUI.
  • A null layout is available if you absolutely need to use absolute positioning of components, but regardless, you should strive to avoid using it.

add Icon/ImageIcon directly to the JButton instead of paint() for AWT or paintComponent() for Swing JComponents

Contructor JButton(Icon) knows Icon or ImageIcon

from code

import java.awt.*;
import javax.swing.*;

public class ButtonsIcon extends JFrame {

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

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                ButtonsIcon t = new ButtonsIcon();
            }
        });
    }

    public ButtonsIcon() {
        setLayout(new GridLayout(0, 2, 4, 4));

        JButton button = new JButton();
        button.setBorderPainted(false);
        button.setBorder(null);
        button.setFocusable(false);
        button.setMargin(new Insets(0, 0, 0, 0));
        button.setContentAreaFilled(false);
        button.setIcon((errorIcon));
        button.setRolloverIcon((infoIcon));
        button.setPressedIcon(warnIcon);
        button.setDisabledIcon(warnIcon);
        add(button);

        JButton button1 = new JButton();
        button1.setBorderPainted(false);
        button1.setBorder(null);
        button1.setFocusable(false);
        button1.setMargin(new Insets(0, 0, 0, 0));
        button1.setContentAreaFilled(false);
        button1.setIcon((errorIcon));
        button1.setRolloverIcon((infoIcon));
        button1.setPressedIcon(warnIcon);
        button1.setDisabledIcon(warnIcon);
        add(button1);
        button1.setEnabled(false);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }
}

It looks like you're never repainting after you add your buttons.

I would add a repaint in there after you add them.

Just had a similar problem...

I believe that the glitch is caused by overriding the paint() method. Default paint() method automatically calls repaint() on all component, but by overriding the paint() method, components stop being repainted. So, the solution is to call repaint() on all components int the overriden paint() method.

Worked for me, hope it'll work for others ;)..

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!