从JPanel(没有JFrame)创建BufferedImage时,我还可以使用布局管理器吗? [英] When creating a BufferedImage from a JPanel (w/o a JFrame), can I also use a layout manager?

查看:114
本文介绍了从JPanel(没有JFrame)创建BufferedImage时,我还可以使用布局管理器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从JPanel创建BufferedImage,而不使用JFrame。昨天,我终于能够在这个社区的帮助下看到图像(见下文),但现在我遇到了一些布局问题。 BufferedImage上的所有组件都以0,0开始绘制,而不是遵循布局管理器。有没有人知道这个问题的解决方案?

I am trying to create a BufferedImage from a JPanel, without using a JFrame. Yesterday, I was finally able to get the image to appear with help from this community (see below), but now I'm having some layout trouble. All of the components on the BufferedImage begin drawing at 0,0, instead of following the layout manager. Does anyone know of a solution for this problem?

昨天的问题:我可以从JPanel创建BufferedImage而无需在JFrame中渲染吗?

在下面的代码中,两个标签将在图片的左上角互相覆盖。

In the code below, the two labels will overwrite each other in the top left corner of the image.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class RenderTest {

        RenderTest() {
        JPanel panel = new JPanel();
        panel.setBackground(Color.RED);
        panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
        Dimension dim = new Dimension(150,50);
        panel.setSize(dim);
        panel.setMinimumSize(dim);
        panel.setMaximumSize(dim);
        panel.setPreferredSize(dim);
        JLabel label = new JLabel("hello");
        JLabel label2 = new JLabel(" world");
        label.setSize(label.getPreferredSize());
        label2.setSize(label2.getPreferredSize());
        panel.add(label);
        panel.add(label2);

        BufferedImage bi = getScreenShot(panel);
        JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));
    }

    private BufferedImage getScreenShot(JPanel panel){
        BufferedImage bi = new BufferedImage(
            panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_ARGB);
        panel.paint(bi.getGraphics());
        return bi;
    }

    public static void main(String[] args) {
        new RenderTest();
    }
}


推荐答案

更多女王提示。 '调用 addNotify()'。

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

public class RenderTest {

    RenderTest() {
        JPanel panel = new JPanel(new GridLayout(0,1,10,10));
        panel.setBackground(Color.RED);
        panel.setBorder(new EmptyBorder(5,25,5,25));

        JLabel label = new JLabel("hello");
        panel.add(label);
        JLabel label2 = new JLabel("goodbye");
        panel.add(label2);

        panel.addNotify();
        panel.setSize(panel.getPreferredSize());
        panel.validate();

        BufferedImage bi = getScreenShot(panel);
        JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));
    }

    private BufferedImage getScreenShot(JPanel panel){
        BufferedImage bi = new BufferedImage(
                panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_ARGB);
        panel.paint(bi.getGraphics());
        return bi;
    }

    public static void main(String[] args) {
        new RenderTest();
    }
}

BTW - 我不建议将尺寸设置为任意尺寸,然后尝试将其与布局相结合。只是想到我会提到,因为我得到的印象就是你想要的。在这种情况下,我们可能会确切地定位一切,这是一种方法,但选择一种方法或另一种方法。

BTW - I would not recommend setting the sizes to arbitrary dimensions and then attempt to combine that with layouts. Just thought I'd mention that since I'm getting the impression that is what you want. This is a situation where we might position everything exactly so that is one way to go, but choose one approach or the other.

这篇关于从JPanel(没有JFrame)创建BufferedImage时,我还可以使用布局管理器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆