在Swing中绘制组件顶部? [英] Painting over the top of components in Swing?

查看:90
本文介绍了在Swing中绘制组件顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 JPanel 添加到 JViewport ,并且面板上添加了其他几个面板。我正在尝试实现拖动选择,您可以通过拖动鼠标选择多个组件。我面临的唯一问题是选择矩形被绘制在添加到主 JPanel 的组件后面。如何在它们上方绘画?

I have a JPanel added to a JViewport, and the panel has several other panels added to it. I'm trying to implement a dragging selection, where you can select more than one component by dragging the mouse. The only problem I'm facing is that the selection rectangle is being painted behind the components added to the main JPanel. How can I paint over the top of them?

我的结构如下:
JFrame - > ContentPane - > JLayeredPane - > JScrollPane - > JPanel - > JPanel []

My structure is as follows:
JFrame -> ContentPane -> JLayeredPane -> JScrollPane -> JPanel -> JPanel [].

设计大学任务草案:

如您所见,矩形位于其他面板后面。

推荐答案


这就是我现在正在做的事情(显然在更简单的层面上),并且Swing在添加到其上的组件下面绘制矩形。

This is what I'm already doing (on a much simpler level obviously), and Swing paints the rectangle underneath the components added to it.

这是一种情况,你应该覆盖面板的paint()方法而不是paintComponent()方法。然后自定义绘画将在所有子组件被绘制后完成。

This is one case where you should override the paint() method of the panel and not the paintComponent() method. Then the custom painting will be done AFTER all the child components have been painted.

这篇关于在Swing中绘制组件顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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