如何像Windows计算器一样无间隙地将JButton彼此相邻添加? [英] How to add JButtons next to each other without gap/space like in Windows Calculator?

查看:50
本文介绍了如何像Windows计算器一样无间隙地将JButton彼此相邻添加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于实践目的,我正在制作与Windows 10中提供的计算器相同的计算器,但无法删除 JButtons 之间的空格.我正在使用Netbeans设计器视图执行此操作.我尝试通过将两个按钮的-2都设为0来设置margin,但是每当我调整按钮的大小并将其拖动到另一个按钮时,另一个按钮都会自动消失.

I'm making a calculator same as provided in windows 10 for practice purpose, but I am unable to remove the space between the JButtons. I'm using Netbeans designer view to do this. I have tried by doing margin as 0 even doing -2 of both the buttons but whenever I resize the button and drag it to the other one, the other button goes away automatically.

这是我想要做的屏幕截图:

Here is the screen shot what I want to do:

这是设计视图:

推荐答案

  1. 给持有JButton的JPanel一个GridLayout,一个具有适当的行和列(调用构造器,该构造器使用2个参数,再次用于行和列),但不使用任何其他参数-因此布局的水平和垂直间隙为设置为默认大小0. GridLayout API
  2. 将您的JButton添加到此JPanel中.
  3. 添加组件后,请确保对JFrame(或其他顶级窗口)进行 pack()
  4. 打包后调用 setVisible(true)

这实际上就是全部.

这篇关于如何像Windows计算器一样无间隙地将JButton彼此相邻添加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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