使用JButton在Java中创建自定义按钮 [英] Creating a custom button in Java with JButton
问题描述
我正在尝试创建一个具有自定义形状(六边形)的按钮,但是其他方式就像普通的JButton一样(也就是说,与ActionListener一起使用)。
<我创建了一个扩展AbstractButton的类,但是当我单击它时它似乎没有向ActionListener发送事件。如果我更改类以扩展JButton它可以很好地工作,但它会搞砸按钮的显示方式。我假设有一个方法需要覆盖以使其触发事件,但我无法弄清楚它是什么。
您必须扩展 JButton
类而不是 AbstractButton
。尝试以下事项,你就会明白。
第一步是继承 JButton
。
然后,在您的子类中,首先重新定义 paintComponent(Graphics)
方法。如果你想要任何改变。
然后,覆盖 paintBorder(Graphics)
给它一个六边形。
I am trying to create a button that has a custom shape (hexagon), but otherwise acts like a normal JButton would (that is, works with an ActionListener).
I have created a class that extends AbstractButton, but it doesn't seem to be sending events to the ActionListener when I click it. If I change the class to extend JButton it works perfectly, but it screws up the way the button is displayed. I'm assuming that there is a method that I need to override to get it to fire events, but I can't figure out what it is.
You will have to extend JButton
class not AbstractButton
. Try the following things and you will get idea.
The first move is to subclass JButton
.
Then, in your subclass, start by redefining the paintComponent(Graphics)
method. If you want any changes.
Then, override paintBorder(Graphics)
to give it a shape of hexagon.
这篇关于使用JButton在Java中创建自定义按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!