简体   繁体   English

JButton如何设置翻转图标边界

[英]JButton how to set roll over icon bounding

So I've got code that will set a JButton equal to an ImageIcon. 所以我有代码将JButton设置为ImageIcon。 As well, when I hover over the image it will display a new ImageIcon, but the way I have it is that the new IconImage is actually a bit larger than the original. 同样,当我将鼠标悬停在图像上时,它将显示一个新的ImageIcon,但我的方式是新的IconImage实际上比原始图像大一点。 Now the problem is that it still exists within the bounds of the button, so say my button size is 100, 250 and the roll over IconImage is 120, 270 well then the issue is that 20 pixels from each side is cut off. 现在的问题是它仍然存在于按钮的范围内,所以说我的按钮大小是1​​00,250并且在IconImage上滚动是120,270然后问题是每边的20个像素被切断。

My solution thus far is actually making the button equal to the size of the roll over IconImage, but my issue with that is that even if I mouse over, up to 20 pixels, beside the image the rollOver effect still happens. 到目前为止我的解决方案实际上是使按钮等于在IconImage上滚动的大小,但我的问题是,即使我鼠标悬停,最多20个像素,在图像旁边仍然会发生rollOver效果。 Now that's obviously because I changed the bounds of my JButton. 现在,这显然是因为我改变了JButton的界限。

If anyone has any solutions they can think of, I'd greatly appreciate it. 如果有人有任何他们能想到的解决方案,我会非常感激。

Here is an example of what I'm doing: 这是我正在做的一个例子:

singlePlayerButton = new ImageIcon("GUIImages\\singlePlayerButton.png");
rollOverSinglePlayerButton = new ImageIcon("GUIImages\\rollOverSinlgePlayerButton.png");
btnSinglePlayer = new JButton(singlePlayerButton);
btnSinglePlayer.setOpaque(false);
btnSinglePlayer.setContentAreaFilled(false);
btnSinglePlayer.setBorderPainted(false);
btnSinglePlayer.setFocusPainted(false);
btnSinglePlayer.setBounds(320, 25, 275, 130);
btnSinglePlayer.setRolloverIcon(rollOverSinglePlayerButton);
mainMenu.add(btnSinglePlayer);

Imagine that I've declared the variables I'm using else where. 想象一下,我已经声明了我在其他地方使用的变量。

I would simply make sure both icons have exactly the same size. 我只想确保两个图标的大小完全相同。 Add a transparent border to the smaller one if necessary. 如有必要,请为较小的边框添加透明边框。

This will make everything easier. 这将使一切变得更容易。

You can add a "MouseListener" instance to your button and implement the "mouseEntered" method. 您可以在按钮中添加“MouseListener”实例并实现“mouseEntered”方法。 Then you can change your button (bouds, imange, ...) and redraw it on the screen if neccessary. 然后你可以改变你的按钮(bouds,imange,...)并在必要时在屏幕上重绘它。 See the javadoc of JButton and MouseAdapter class for furter information. 有关更多信息,请参阅JButton和MouseAdapter类的javadoc。

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

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