4

我正在将一系列 JPanel 打印到Printable,这是基本打印界面,它提供了一个 Graphics 对象,您可以绘制您想要打印的内容。如果我有一个“活的”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 对象?谢谢!

4

1 回答 1

3

来自@Kleopatra 答案的提示。

SSCCEPaintInvisible

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class SSCCEPaintInvisible
{
    public static void main(String[] args)
    {
        /* Create an JPanel with a JLabel */
        JPanel panel = new JPanel();

        JLabel label = new JLabel("Hello World");
        panel.add(label);
        // Next 3 are very important!
        panel.setSize(panel.getPreferredSize());
        panel.addNotify();
        panel.doLayout();

        /* 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
    }
}

正如@GagandeepBali 所指出的,这个 GUI 不是在 EDT 上创建的。如果未在 EDT 上完成对 GUI 的更改,结果将是不可预测的。有关更多详细信息,请参阅Swing 中的并发,尤其是初始线程

于 2013-01-08T13:52:33.170 回答