Paint another component in paintComponent
I use SwingPaintDemo2 from Java Tutorials:
http://download.oracle.com/javase/tutorial/uiswing/examples/painting/SwingPaintDemo2Project/src/painting/SwingPaintDemo2.java
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 = ne开发者_运维问答w JLabel("Label");
c.paint(g);
}
g.drawString works fine. But how can I paint JLabel from this method? It doesn't work.
I think you have to set a size to your 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. The label is eventually drawn to screen, but it is painted to BufferedImage
before ever being displayed.
The important line of the source is..
textLabel.setSize(textLabel.getPreferredSize());
精彩评论