使JButton隐形,但可点击?

时间:2021-11-11 11:18:57

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

如何在java中创建一个不可见但可点击的JButton ?

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.

但这也不管用。我想要这样做,因为我想要一个有图像的按钮,如果我把看不见的JButton放在图像上,当你点击图像或看不见的按钮时,按钮会做出响应。

2 个解决方案

#1


19  

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


0  

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:

没有点,因为没有点,所以没有标准的方法,但是可以重写JButton的paint方法,什么都不做,比如:

class InvisibleButton extends JButton {

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

Try playing around with this.

试试这个。

#1


19  

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


0  

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:

没有点,因为没有点,所以没有标准的方法,但是可以重写JButton的paint方法,什么都不做,比如:

class InvisibleButton extends JButton {

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

Try playing around with this.

试试这个。