调整JPanel的大小以准备打印而不将其从原始位置移除 [英] Resizing JPanel to prepare for printing without removing it from its original position

查看:174
本文介绍了调整JPanel的大小以准备打印而不将其从原始位置移除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,我经常需要打印各种JComponents(通常是JPanels),我喜欢它们是整页的。我现在的方法是使用以下代码:

In my program I frequently need to print various JComponents (generally JPanels) and I like them to be full-page. The way I do it now is by using the following code:

g2d.scale(pf.getImageableWidth()/componentToPrint.getWidth(), pf.getImageableHeight()/componentToPrint.getHeight());

但这通常会延伸或以其他方式改变我想要打印的东西,我更愿意这样做智能重新调整大小的东西,可能是以下功能版本:

but this often stretches or otherwise deforms whatever I am trying to print, and I would much prefer to do something that re-sized intelligently, perhaps a functional version of:

componentToPrint.setSize(pf.ImageableWidth(), pf.ImageableHeight);

或者说将组件添加到新的JFrame然后设置帧大小(问题是组件可以' t一次存在于两个地方)。我不在乎调整大小是否会使GUI的其余部分看起来很糟糕,只要它是可以轻松重置的东西。

or say adding the component into a new JFrame and then setting the frame size (problem is components can't exist in two place at once). I wouldn't care if the resizing would make the rest of the GUI look terrible, as long as it is something that can easily be reset.

有什么方法可以这样做?

Is there any way to do this?

推荐答案

我认为您正在寻找的解决方案是构建一个包含所需内容的新JPanel而是打印副本。如果你使用CellRendererPane这样做,你可以得到你正在寻找的确切的调整大小行为。

I think the solution you're looking for would be to construct a new JPanel that contains the desired contents and to print the copy instead. If you do so using the CellRendererPane you can get the exact resizing behaviour it sounds like you're looking for.

如果你的JComponents编写得相当好,那么它不应该成为一个新问题并将其模型设置为与原始模型相同的问题。

If your JComponents are reasonably well written then it shouldn't be a problem to new up a fresh one and to set its model to be the same as the original's.

CellRendererPane cellRendererPane = new CellRendererPane();
// It's important to add the cell renderer pane to something
// you can use the same one for all of your exporting if you like and just
// add it to your main frame's content pane - it won't show up anywhere.
add(cellRendererPane);

JPanel printPanel = createCopy(panel);
cellRendererPane.paintComponent(g, printPanel, null, 0, 0, exportDim.width, exportDim.height, true);

这是一个完整的工作示例。 createPanel()方法应该创建您想要渲染的任何组件。一个真实的例子应该确保使用相同的模型,而不是为一次性组件重新创建一个新模型。

Here's a complete working example. The createPanel() method should create whatever component it is that you want rendered. A real example should be sure to use the same model rather than recreating a new model for a throwaway component.

public class SCCE {

    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        final JFrame f = new JFrame("SCCE");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        f.getContentPane().setLayout(new BorderLayout());
        f.getContentPane().add(SCCE.createPanel());

        final CellRendererPane backgroundRenderer = new CellRendererPane();

        // Add the renderer somewhere where it won't be seen
        f.getContentPane().add(backgroundRenderer, BorderLayout.NORTH);
        f.getContentPane().add(createSaveButton(backgroundRenderer), BorderLayout.SOUTH);

        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    // Create your custom component from whatever model here..
    private static final Component createPanel() {
        DefaultListModel model = new DefaultListModel();
        for (int i = 0; i < 10; i++) {
            model.addElement("Item number " + i);
        }
        return new JList(model);
    }

    private static JButton createSaveButton(final CellRendererPane backgroundRenderer) {
        return new JButton(new AbstractAction("Save image to file") {
            @Override
            public void actionPerformed(ActionEvent e) {
                Dimension d = new Dimension(400, 300);

                BufferedImage img = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_ARGB);
                Graphics2D g = img.createGraphics();
                backgroundRenderer.paintComponent(g, createPanel(), null, 0, 0, d.width, d.height, true);
                g.dispose();

                try {
                    File output = new File("test.png");
                    System.err.println("Saved to " + output.getAbsolutePath());
                    ImageIO.write(img, "png", output);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

}

这篇关于调整JPanel的大小以准备打印而不将其从原始位置移除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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