3

我有一堂课可以一次打印一个组件。我想打印的东西,我在 JPanel 中分组。因此,我有一个带有 4 个 JScrollPanes(同步的 JScrollPanes)的 JPanel(在 jframe 内以将其显示在屏幕上)。在每个滚动窗格中,我都有一个 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);
4

0 回答 0