简体   繁体   English

Java / Swing:如何在JLabel的自定义渲染器中绘制简单的条形图

[英]Java/Swing: How to draw a simple bar graph in a custom renderer for a JLabel

I would like to dynamically create a minimal transparent bar graph to display over a canvas. 我想动态创建一个最小的透明条形图,以显示在画布上。

I was thinking of using a custom renderer for a JButton or a JLabel; 我在考虑为JButton或JLabel使用自定义渲染器; but how do I draw my bar graph in this renderer? 但是如何在此渲染器中绘制条形图?

The standard way would be to create a subclass (possibly an anonymous one, if you prefer) of JLabel or JPanel and overload the paintComponent(Graphics g) method. 标准方法是创建JLabel或JPanel的子类(如果愿意,可以是匿名类),并重载paintComponent(Graphics g)方法。 You can then use the passed Graphics object to draw whatever rectangles (and so forth) that you need. 然后,您可以使用传递的Graphics对象绘制所需的任何矩形(依此类推)。 For more information on that part of it, refer to the Java 2D Graphics Trail . 有关该部分的更多信息,请参考Java 2D Graphics Trail

EDIT: Does that answer the question? 编辑:可以回答这个问题吗? I just re-read it and now I'm not sure. 我只是重新阅读了一下,现在不确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM