gpt4 book ai didi

java - 强制 paintAll() 绘制不可见的 JPanel 及其组件?

转载 作者:搜寻专家 更新时间:2023-11-01 03:09:06 25 4
gpt4 key购买 nike

我正在努力将一系列 JPanel 打印到 Printable , 基本打印接口(interface),它提供一个 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 捕获到图形。当然,这会在屏幕上闪烁一个 JFrame 以打印它。

有什么方法可以将从未添加到 UI 中的 JPanel 渲染到 Graphics 对象中?谢谢!

最佳答案

来自@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 进行更改,结果将不可预测。参见 Concurrency in Swing尤其是 Initial Threads更多细节。

关于java - 强制 paintAll() 绘制不可见的 JPanel 及其组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14216115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com