问题描述
我正在编写MineSweeper,并在GridLayout
上使用JButton
.用户输入的行数和列数,因此设置固定大小可能会引起一些问题.
I'm writing MineSweeper and using JButton
s on a GridLayout
. The numbers of rows and columns are entered by the user, so setting fixed size may cause several problems.
如何在不设置面板固定尺寸的情况下删除按钮之间的空间?
How can I remove the space between the buttons without setting the fixed size of the panel?
推荐答案
使用JButton
然后使用GridLayout
在这里,我将JButton
替换为JPanel
(还有一些边界魔术)
Here, I replaced JButton
with JPanel
(and some border magic)
我尝试的另一件事是将负h/vgap传递给GridLayout
The other thing I tried was passing a negative h/vgap to the GridLayout
但是,我不建议这样做,因为它可能会对其他平台产生无法预料的影响.
I'd however not recommend this, as it may have unforeseen effects on other platforms.
我个人很想使用自定义的JLabel
(附带自己的鼠标侦听器)代替按钮.单击它甚至可以触发事件和动作事件.
I personally, would be tempted to use a custom JLabel
(with it's own mouse listener attached) to take the place of the buttons. You could even fire and action event when it's clicked...
这篇关于GridLayout上的JButton-MineSweeper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!