【发布时间】:2012-12-22 08:56:34
【问题描述】:
我正在将一系列 JPanel 打印到 Printable,这是基本打印界面,它提供了一个图形对象,您可以绘制您想要打印的内容。如果我有一个“实时”的 JPanel,它在 UI 的某个地方,一切都很好。
但是,如果我创建一个 JPanel 并且从未将其添加到 UI 中,那么 printAll() 似乎什么都不做。将代码简化为 SSCCE:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class SSCCEPaintInvisible
{
public static void main(String[] args)
{
/* Create an JPanel with a JLabel */
JPanel panel = new JPanel();
//panel.setLayout(new FlowLayout());
JLabel label = new JLabel("Hello World");
panel.add(label);
//label.invalidate();
//panel.invalidate();
/* Record a picture of the panel */
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_4BYTE_ABGR);
Graphics g = image.getGraphics();
/* Draw something to ensure we're drawing */
g.setColor(Color.BLACK);
g.drawLine(0, 0, 100, 100);
/* Attempt to draw the panel we created earlier */
panel.paintAll(g); // DOES NOTHING. :(
/* Display a frame to test if the graphics was captured */
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label2 = new JLabel( new ImageIcon(image) );
frame.add(label2);
frame.pack();
frame.setVisible(true);
// shows ONLY the black line we drew in the Graphics
}
}
如果我为面板创建 JFrame 并将面板添加到 JFrame 并在调用 paintAll() 之前使 JFrame 可见,则代码会按预期将 UI 捕获到 Graphic。当然,这会在您的屏幕上闪烁一个 JFrame 来打印它。
有没有什么方法可以将一个从未添加到 UI 中的 JPanel 渲染成一个 Graphics 对象?谢谢!
【问题讨论】:
-
“但是,如果我创建了一个 JPanel 并且从未将它添加到 UI 中,printAll() 似乎什么都不做” 请查看 this thread 以获取有关绘制未实现组件的提示。
-
+1 到 SSCCE。虽然当您创建一个 SSCCE 时,请务必遵守使用 Event Dispatch Thread 的 Swing 编程最佳实践,而不是将所有内容都放在 main 方法上。
-
顺便说一句 - 除了从左上角到 100,100 的黑线之外,您希望在图像中看到什么?
-
@AndrewThompson,JPanel 中 JLabel 的“Hello World”。
-
哦,对了,请原谅我的愚蠢。 :P
标签: java swing graphics printing bufferedimage