[英]Paint another component in paintComponent
I use SwingPaintDemo2 from Java Tutorials:我使用来自 Java 教程的 SwingPaintDemo2:
http://download.oracle.com/javase/tutorial/uiswing/examples/painting/SwingPaintDemo2Project/src/painting/SwingPaintDemo2.java http://download.oracle.com/javase/tutorial/uiswing/examples/painting/SwingPaintDemo2Project/src/painting/SwingPaintDemo2.Z93F725A0748B33FE1C8486Z4F
I modified it like this:我这样修改它:
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw Text
g.drawString("This is my custom Panel!",10,20);
JLabel c = new JLabel("Label");
c.paint(g);
}
g.drawString works fine. g.drawString 工作正常。 But how can I paint JLabel from this method?
但是我怎样才能用这种方法绘制 JLabel 呢? It doesn't work.
它不起作用。
I think you have to set a size to your label.我认为你必须为你的 label 设置一个大小。
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw Text
g.drawString("This is my custom Panel!",10,20);
JLabel c = new JLabel("Label");
c.setBounds(0, 0, 400, 30);
c.paint(g);
}
See the LabelRenderTest.java
source on this thread .请参阅此线程上的
LabelRenderTest.java
源。 The label is eventually drawn to screen, but it is painted to BufferedImage
before ever being displayed. label 最终被绘制到屏幕上,但在显示之前它被绘制到
BufferedImage
。
The important line of the source is..来源的重要线路是..
textLabel.setSize(textLabel.getPreferredSize());
JLabel label_name = new JLabel("Some text");
label_name.setBounds(position_x, position_y, width, height);
label_name.setFont(new Font("Dialog", Font.PLAIN, 10));
add(label_name);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.