JButtons on GridLayout - MineSweeper

泪湿孤枕 提交于 2019-12-14 03:06:17

问题


I'm writing MineSweeper and using JButtons 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?


回答1:


This seems to be more of a issue with JButton then with GridLayout

Here, I replaced JButton with JPanel (and some border magic)

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.

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...




回答2:


You need to set the hgap and vgap to 0 when creating the gridlayout instance.

Like,

   GridLayout myLayout = new GridLayout(0,2,0,0);


来源:https://stackoverflow.com/questions/13448535/jbuttons-on-gridlayout-minesweeper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!