带 JTable 的 JScrollpane 中的背景图像

发布于 2024-10-13 12:30:11 字数 139 浏览 7 评论 0原文

我试图在 JScrollPane 中的 JTable 后面添加居中背景图像。 背景相对于视口的位置应该居中且静态。

我尝试将 JScrollPane 添加到带有绘制图像的 JPanel 中,并使其他所有内容变为半透明,但结果很丑陋并且存在渲染问题。

Im trying to add a centered background image behind a JTable in a JScrollPane.
The position of the background relative to the Viewport should be centered and static.

Ive tried adding the JScrollPane to a JPanel with a drawn image and making everything else translucent, but the result was ugly and had rendering issues.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如歌彻婉言 2024-10-20 12:30:11

查看文章末尾的 WatermarkDemo 以获取完整示例。

Check out the WatermarkDemo at the end of the article for a complete example.

挽心 2024-10-20 12:30:11

您应该继承 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);

You should subclass JTable and override its paint method so that it draws your background image. Here is some sample code:

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);
娇女薄笑 2024-10-20 12:30:11

不确定这是否是您所需要的,但看看支持水印的 Substance 外观和感觉:
https://substance.dev.java.net/docs/watermarks.html

Not sure if this is what you need, but have a look at the Substance Look and Feel which supports watermarks:
https://substance.dev.java.net/docs/watermarks.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文