如何在添加其他对象的同时将 MyDrawPanel 添加到程序中?
我正在尝试制作一个简单的游戏,并且已成功将图像加载到 JFrame 上的 myDrawPanel 上。但是,我还想向 GUI 添加其他内容,例如按钮等。但是,我不能这样做,因为当我将 myDrawPanel 添加到 JPanel 时,我得到的只是窗口顶部中心的一个小方块。事实证明,这是一个很大的限制,我想知道是否有一个替代的“JObject”,我可以将 myDrawPanel 添加到其中,以便自定义我的 GUI。预先感谢,圣诞快乐!
I am attempting to make a simple game, and I have loaded an image onto a myDrawPanel, which is on the JFrame, successfully. However, I also want to add other things to my GUI, such as buttons, etc. I cannot do this, however, because when I add the myDrawPanel to a JPanel, all I get is a small square in the top center of the window. This is proving to be greatly limiting, and I was wondering if there is an alternative "JObject" that I can add the myDrawPanel to in order to allow for the customization of my GUI. Thanks in advance, and Merry Christmas!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JPanel(或子类)通常从它包含的组件中获取其大小。如果里面没有任何组件,它就会很小。
解决此问题的一种方法是在自定义面板上调用 setPreferredSize(Dimension)。
A JPanel (or sub-class) generally obtains its size from the components it contains. If no components are in it, it will be small.
One way to fix that is to call setPreferredSize(Dimension) on the customized panel.
p1.setPreferredSize(new Dimention(1100,800));
p1.setPreferredSize(new Dimention(1100,800));