如何用 Swing 制作画布? [英] How to make canvas with Swing?

查看:37
本文介绍了如何用 Swing 制作画布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Java 制作一个绘图编辑器,其中我有一个工具栏,其中包含我想粘贴到画布中的对象.我正在使用 Swing 组件来制作 GUI,但是当我寻找制作画布的方法时,我只从 AWT 中找到了类画布.

I'm trying to make a paint editor with Java in which I have a toolbar with the objects that I would like to paste in the canvas. I'm using Swing components to make the GUI, but when I looked for the way of making the canvas, I only found the class canvas from AWT.

有没有办法用 Swing 制作类似于画布的东西?(例如,JPanel?)我读过,使用 AWT 的类画布和用 Swing 制作的 GUI 将无法正常工作,是真的吗?

Is there any way to make something similar to canvas with Swing? (for example, JPanel?) I have read that using the class canvas from AWT with a GUI made with swing won't work correctly, is that true?

推荐答案

为了在 Swing 中制作自定义的画布",您通常会编写 JPanel.然后,你必须覆盖JPanelprotectedpaintComponent(Graphics g)方法.

In order to make a custom 'Canvas' in swing you usually write a subclass of a JPanel. Then, you must overwrite the protected paintComponent(Graphics g) method of JPanel.

在paint方法中,可以调用Graphics 对象在 JPanel 上实际绘制.

In the paint method, you can call methods on the Graphics object to actually draw on the JPanel.

与往常一样,Java 教程对此有一个很好的参考让你开始.

As always, the Java Tutorials have a great reference on this to get you started.

这篇关于如何用 Swing 制作画布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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