是否可以在JFrame上绘制图像或文本? [英] Is it possible to draw an image or text on a JFrame?

查看:65
本文介绍了是否可以在JFrame上绘制图像或文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在不添加JPanel的情况下绘制JFrame?

Is it possible to draw on a JFrame without adding a JPanel to it?

我覆盖了paintComponents(),但是它什么也没显示.

i override paintComponents() but it didn't show anything.

 @Override
public void paintComponents(Graphics g) {
    super.paintComponents(g);
    g.drawString("for test", 10, 10);
}

推荐答案

是的.您将需要使用JFrame中的一个窗格,例如内容窗格或玻璃窗格,您可以通过

Yes, it is. You'll want to work with the one of the panes in the JFrame such as the content pane or the glass pane, which you can access via getContentPane, etc.

例如,内容窗格是容器,具有多种添加方法.为此,您可以添加任何 Component -不必专门成为JPanel.有关更多信息,请参见使用顶级容器.

For example the content pane is a Container, with a variety of add methods. To that you can add any Component - doesn't have to be a JPanel specifically. More at Using Top Level Containers.

不过,通常情况下,绘制是通过重写paint(对于AWT)或paintComponent(对于Swing)完成的.这意味着您需要某种Component或 JComponent 放在框架中.有关更多信息,请参见 2D图形教程.您为什么不想更改它?

Usually, though, drawing is done via overriding paint (for AWT) or paintComponent (for Swing). This means you need some sort of Component or JComponent that you put in your frame. More at the 2D Graphics tutorial. Why do you not want to change that?

您还可以覆盖JFrame及其内容窗格,并使内容窗格具有重写paintComponent方法.

You can also override JFrame and its content pane and have a content pane with an override paintComponent method.

但是,我质疑直接绘制JFrame的必要性和智慧.

I question, however, the necessity and wisdom of directly drawing on a JFrame.

这篇关于是否可以在JFrame上绘制图像或文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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