gpt4 book ai didi

java - 图形是在 java swing 中绘制在 UI 之上的,

转载 作者:行者123 更新时间:2023-11-30 04:03:48 27 4
gpt4 key购买 nike

当我在 paint() 方法中在 Jpanel 上绘图时(通过继承 JPanel)
图形出现在我的按钮和其他 UI 组件的顶部,
在不可见的组件中,那么如何在图形之上绘制组件呢?

最佳答案

首先查看 Performing Custom PaintingPainting in AWT and Swing

The Paint Methods
The rules that apply to AWT's lightweight components also apply to Swing components -- for instance, paint() gets called when it's time to render -- except that Swing further factors the paint() call into three separate methods, which are invoked in the following order:
<br/>protected void paintComponent(Graphics g)<br/>
protected void paintBorder(Graphics g) <br/>
protected void paintChildren(Graphics g)<br/>

Swing programs should override paintComponent() instead of overriding paint(). Although the API allows it, there is generally no reason to override paintBorder() or paintComponents() (and if you do, make sure you know what you're doing!). This factoring makes it easier for programs to override only the portion of the painting which they need to extend. For example, this solves the AWT problem mentioned previously where a failure to invoke super.paint() prevented any lightweight children from appearing.

关于java - 图形是在 java swing 中绘制在 UI 之上的,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21273533/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com