Java打印完整的滚动窗格不仅仅是可见部分

时间:2013-02-15 11:50:18

标签: java swing printing jpanel jscrollpane

我有一个类一次打印一个组件。我要打印的东西我,我在JPanel分组。因此,我有一个JPanel(在一个jframe内显示在屏幕上),有4个JScrollPanes(同步的)。在每个滚动窗格中,我都有一个JList。我要打印的东西是JLists,因为它们在屏幕上。但是,如果列表包含滚动窗格能够滚动的那么多元素,那么它将只打印它的可见部分,我希望它打印整个列表。我试图首先调整JPanel,然后调整JScrollPanes,最后调整JLists,但它不起作用。

这是我用于调整大小的代码:

    int x,y,width,height;

    //jPanel1 setBounds
    x = jPanel1.getBounds().x;
    y = jPanel1.getBounds().y;
    width = jPanel1.getBounds().width;
    height = lm1.getSize() * jList1.getFixedCellHeight() + 20;
    jPanel1.setBounds(x, y, width, height);

    //jList1 setBounds
    x = jList1.getBounds().x;
    y = jList1.getBounds().y;
    width = jList1.getBounds().width;
    height = lm1.getSize() * jList1.getFixedCellHeight();
    jScrollPane1.setBounds(x, y, width, height);
    jList1.setBounds(x, y, width, height);
    jList1.setVisibleRowCount(lm1.getSize());

    //jList2 setBounds
    x = jList2.getBounds().x;
    y = jList2.getBounds().y;
    width = jList2.getBounds().width;
    height = lm2.getSize() * jList2.getFixedCellHeight();
    jScrollPane2.setBounds(x, y, width, height);
    jList2.setBounds(x, y, width, height);
    jList2.setVisibleRowCount(lm2.getSize());

    //jList3 setBounds
    x = jList3.getBounds().x;
    y = jList3.getBounds().y;
    width = jList3.getBounds().width;
    height = lm3.getSize() * jList3.getFixedCellHeight();
    jScrollPane3.setBounds(x, y, width, height);
    jList3.setBounds(x, y, width, height);
    jList3.setVisibleRowCount(lm3.getSize());

    //jList4 setBounds
    x = jList4.getBounds().x;
    y = jList4.getBounds().y;
    width = jList4.getBounds().width;
    height = lm4.getSize() * jList4.getFixedCellHeight();
    jScrollPane4.setBounds(x, y, width, height);
    jList4.setBounds(x, y, width, height);
    jList4.setVisibleRowCount(lm4.getSize());

    PrintUtil.printComponent(jPanel1);

修改

部分工作。我可以将列表转换为图像并且它工作得很好但是当我想将它添加到JPanel以使一个组件打印出来时,布局变得非常奇怪!最后一个列表图像添加将始终从(0,0)开始,它不应该因为我设置了它的界限。

我所制作的代码:

    BufferedImage bi1 = componentToImage(jList1, false);
    BufferedImage bi2 = componentToImage(jList2, false);
    BufferedImage bi3 = componentToImage(jList3, false);
    BufferedImage bi4 = componentToImage(jList4, false);

    ImagePanel ip1 = new ImagePanel(bi1);
    ip1.setBounds(20, 20, bi1.getWidth(), bi1.getHeight());

    ImagePanel ip2 = new ImagePanel(bi2);
    ip2.setBounds(30 + bi1.getWidth(), 20, bi2.getWidth(), bi2.getHeight());

    ImagePanel ip3 = new ImagePanel(bi3);
    ip3.setBounds(40 + bi1.getWidth() + bi2.getWidth(), 20, bi3.getWidth(), bi3.getHeight());

    ImagePanel ip4 = new ImagePanel(bi4);
    ip4.setBounds(50 + bi1.getWidth() + bi2.getWidth() + bi3.getWidth(), 20, bi4.getWidth(), bi4.getHeight());

    JLabel jl1 = new JLabel();
    JLabel jl2 = new JLabel();
    JLabel jl3 = new JLabel();
    JLabel jl4 = new JLabel();

    jl1.setBounds(20, 0, bi1.getWidth(), bi1.getHeight());
    jl2.setBounds(30 + bi1.getWidth(), 0, bi2.getWidth(), bi2.getHeight());
    jl3.setBounds(40 + bi1.getWidth() + bi2.getWidth(), 0, bi3.getWidth(), bi3.getHeight());
    jl4.setBounds(50 + bi1.getWidth() + bi2.getWidth() + bi3.getWidth(), 0, bi4.getWidth(), bi4.getHeight());

    jl1.setText(jLabel1.getText());
    jl2.setText(jLabel2.getText());
    jl3.setText(jLabel3.getText());
    jl4.setText(jLabel4.getText());

    JPanel jp = new JPanel();
    jp.add(jl1);
    jp.add(ip1);
    jp.add(jl2);
    jp.add(ip2);
    jp.add(jl3);
    jp.add(ip3);
    jp.add(jl4);
    jp.add(ip4);
    jp.setVisible(true);

0 个答案:

没有答案