使JButton不可见但可点击? [英] Making a JButton invisible, but clickable?

查看:783
本文介绍了使JButton不可见但可点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我试过:

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

但这也不起作用。
我想这样做是因为我想要一个带图像的按钮,如果我将不可见的JButton放在图像上,按钮会在你点击图像时响应,或者是隐形按钮。

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.

推荐答案

我认为你的意思是透明的,而不是隐形的。

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);

这篇关于使JButton不可见但可点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆