如何使 Java Canvas 看起来从其容器中升起
我有一个用 Java 编写的简单 GUI 组件。 该类在 java.awt.canvas 中绘制模拟时钟。
然后,该画布包含在 JFrame 中 - 我想要做的是为画布提供 3D“凸起”效果 - 几乎就像向照片添加阴影一样。 有没有一种简单的方法可以做到这一点?
I have a simple GUI component written in Java. The class draws an analog clock in a java.awt.canvas.
This canvas is then contained in a JFrame - What I want to do is give the canvas a 3d "raised" effect - almost like adding a drop shadow to a photo.
Is there a simple way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是 JFrame,那么您有两个选择:
首先将您自己的组件添加到 JPanel,然后将其添加到 JFrame。
您可以从 JComponent 继承,而不是从 java.awt.Canvas 继承。 然后你必须在paintComponent()方法中完成所有的绘画,而不仅仅是paint()(你可以重命名当前的paint方法)。
在这两种情况下,您现在都可以使用从 BorderFactory 获取的 setBorder() 方法(在 JPanel 或您的组件上)设置边框。
另请参阅:如何使用边框
If you are using a JFrame, then you have two options:
Add your own component to a JPanel first and then add this to the JFrame.
Instead of inheriting from java.awt.Canvas, you can inherit from JComponent. Then you would have to do all your painting in the paintComponent() Method instead of just paint() (you can just rename your current paint method).
In both cases you can now set a border with the setBorder() Method (on the JPanel or your component) you can get from BorderFactory.
See also: How to Use Borders
如果您使用的是 Swing 元素,则可以使用 BorderFactory 的 createRaishedBevelBorder() 方法并将画布的边框设置为结果边框。 Canvas 是一个 AWT 组件,因此您需要将其包装在可以设置边框的 Swing 组件中。
If you were using a Swing element, you'd use the createRaisedBevelBorder() method of the BorderFactory and set the canvas' border to the resulting border. Canvas is an AWT component, so you'll need to wrap it in a Swing component to which you can set the border.