在 Java 中的 JPanel 周围创建灰色空间
我正在用Java开发一个简单的艺术程序,我很好奇是否有可能像大多数艺术程序一样在画布周围创建一个灰色的空白空间(意思是,一个未锁定且可滚动的空白空间)。这可能吗?我该如何去做呢?
例子:
I'm developing an simple art program in Java and I was curious if it was possible to create an gray empty space around the canvas like is most art programs(Meaning, an empty space that isn't locked down and is scrollable). Is this possible and how could I go about doing this?
Example:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您是否考虑过在 JPanel 中使用线条边框。
Have you considered using a Line Border within the JPanel.
如何使用边框。创建一个带有 EmptyBorder 的面板,然后向该面板添加第二个 JPanel。
不要在 Swing 应用程序中混合使用 AWT 组件。 Canvas 是一个 AWT 组件。只需使用 JPanel,您仍然可以访问所有 Graphics 方法。
编辑:
不要再将 JPanel 称为 Canvas。 Canvas 是一个 AWT 组件,它的工作方式与 JPanel 不同。
如果您不希望 JPanel 调整大小,请为其指定首选大小并使用 FlowLayout 将其添加到 JPanel。您可以控制 FlowLayout 的水平/垂直间隙以提供边框的外观。
发布您的 SSCCE (http://sscce.org),展示您尝试过的操作以及遇到的问题,而不是我们发布所有代码并猜测您的问题是什么。
How to Use Borders. Create a panel with an EmptyBorder and then add a second JPanel to the panel.
Don't mix AWT components in a Swing application. Canvas is an AWT component. Just use a JPanel, you still have access to all the Graphics methods.
Edit:
Quit calling your JPanel a Canvas. A Canvas is an AWT component and it works differently than a JPanel.
If you don't want your JPanel to resize then give it a preferred size and add it to a JPanel using a FlowLayout. You can control the horizontal/vertical gaps of a FlowLayout to give the appearance of a Border.
Post your SSCCE (http://sscce.org) showing what you have tried and the problems you have encountered rather than us posting all the code and guessing what your problems are.
也许将画布添加到 JScrollPane?
Perhaps add the canvas into a JScrollPane?
有几种方法可以做到这一点。我想到的最简单的方法是创建一个 JPanel,然后将 Canvas 放入 JPanel 中。将画布中的插图设置为灰色空间的大小。将 JPanel 的背景设置为灰色。像这样的东西:
There are several ways to do this. The easiest that comes to my head is to create a JPanel and then put the Canvas in the JPanel. Set the insets in the Canvas to the size of your gray space. Set the background of the JPanel to Gray. Something like this:
BoxLayout 非常适合这个!我最近也经历过你的处境。
现在是重要的部分:
无论您如何缩放窗口,这都会使内部面板保持居中并保持固定大小!
这就是它的样子
BoxLayout is great for this! I've been in your shoes very recently.
And now for the important part:
And that will keep the inner panel centered and at a fixed size no matter how you scale the window!
Here's what it looks like