Making a JButton invisible, but clickable?

依然范特西╮ 提交于 2019-12-19 02:57:10

问题


How do I make a JButton in java, invisible, but clickable?

button.setVisible(false); 

makes the button invisible, but unclickable, is there any method that makes it invisible, but clickable?

I tried doing:

button.setVisible(false);
button.setEnabled(true);

but that didn't work either. I want to do this because I want to have a button with an image, if I put the invisible JButton over the image, the button will respond when you click the image, or invisible button.


回答1:


I think you mean transparent, rather than invisible.

This will make a clickable button that is not "visible", i.e. transparent:

button.setOpaque(false);
button.setContentAreaFilled(false);
button.setBorderPainted(false);

This answers your asked question, but if your intent is to make an image clickable, there is a better way for that, too:

ImageIcon myImage = new ImageIcon("images/myImage.jpg");
JButton button = new JButton(myImage);



回答2:


Well, there is no point so since there is no point there is no standard way to do this, but it's possible to override the paint method of JButton and do nothing in it like:

class InvisibleButton extends JButton {

    @Override
    public void paint(Graphics g){
          // Do nothing here
    }
}

Try playing around with this.



来源:https://stackoverflow.com/questions/5654208/making-a-jbutton-invisible-but-clickable

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