一度にコンポーネントを印刷するクラスがあります。印刷したいものを、JPanelにまとめます。したがって、4 つの JScrollPanes (同期化されたもの) を持つ JPanel (画面に表示するための jframe 内) があります。これらのスクロールペインのそれぞれに、JList があります。私が印刷したいのは、画面上にある JLists です。ただし、スクロールペインがスクロールできるほど多くの要素がリストに含まれている場合、リスト全体を印刷したい場所に表示されている部分のみが印刷されます。最初に JPanel、次に JScrollPanes、最後に JList のサイズを変更しようとしましたが、うまくいきません。
これは、サイズ変更に使用するコードです。
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 に追加して 1 つのコンポーネントを印刷するようにすると、レイアウトがかなり奇妙になります! 最後のリストの画像の追加は常に (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);