我正在尝试在JScrollPane后面添加一个居中的背景图片。该背景图相对于视口的位置应保持静态且居中。
我已经尝试将JScrollPane添加到带有绘制图像的JPanel中,并使其他所有内容透明,但结果很难看并且存在渲染问题。
我已经尝试将JScrollPane添加到带有绘制图像的JPanel中,并使其他所有内容透明,但结果很难看并且存在渲染问题。
JTable
并覆盖其 paint
方法,以便它绘制您的背景图像。以下是一些示例代码:final JTable table = new JTable(10, 5) {
final ImageIcon image = new ImageIcon("myimage.png");
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
final Component c = super.prepareRenderer(renderer, row, column);
if (c instanceof JComponent){
((JComponent) c).setOpaque(false);
}
return c;
}
@Override
public void paint(Graphics g) {
//draw image in centre
final int imageWidth = image.getIconWidth();
final int imageHeight = image.getIconHeight();
final Dimension d = getSize();
final int x = (d.width - imageWidth)/2;
final int y = (d.height - imageHeight)/2;
g.drawImage(image.getImage(), x, y, null, null);
super.paint(g);
}
};
table.setOpaque(false);
final JScrollPane sp = new JScrollPane(table);
final JFrame f = new JFrame();
f.getContentPane().add(sp);
f.setSize(200,200);
f.setVisible(true);