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

查看:292
本文介绍了如何用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?

推荐答案

为了在摇摆中制作自定义'Canvas',你通常会写一个 JPanel 。然后,您必须覆盖 JPanel protected paintComponent(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方法中,您可以在 图形 实际绘制 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天全站免登陆