java - 在 jTable Java 中添加背景图像

标签 java swing background jtable netbeans-8

我试图在 jTable 中添加背景图片,但是当我运行代码时没有显示任何内容。只有白色,没有显示 jTable,我的 java 应用程序也挂起。这段代码有什么问题吗?这也是预览。

enter image description here


import javax.swing.table.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;


public class Main extends JFrame
{
  public Main() {
  JTable table = new JTable(100, 5) {
     public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
        Component c = super.prepareRenderer(renderer, row, column);
        if (c instanceof JComponent) {
            ((JComponent) c).setOpaque(false);
        }
        return c;
     }
  };

  ImageJScrollPane isp = new ImageJScrollPane(table);

  isp.setBackgroundImage(new ImageIcon("/Images/user/lockscreen.png"));

  getContentPane().add(isp);

  addWindowListener(new WindowAdapter() {
     public void windowClosing(WindowEvent we) {
        System.exit(0);
     }
  });
 }

 public static void main(String [] args) {
  Main main = new Main();
  main.setSize(400, 400);
  main.setVisible(true);
  } 
 } 

 class ImageJScrollPane extends JScrollPane 
{
  private ImageIcon image = null;

  public ImageJScrollPane() {
  this(null, VERTICAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_SCROLLBAR_AS_NEEDED);
  }

  public ImageJScrollPane(Component view) {
  this(view, VERTICAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_SCROLLBAR_AS_NEEDED);
  }

  public ImageJScrollPane(Component view, int vsbPolicy, int hsbPolicy) {
  super(view, vsbPolicy, hsbPolicy);
  if (view instanceof JComponent) {
     ((JComponent) view).setOpaque(false);
   }
   }

  public ImageJScrollPane(int vsbPolicy, int hsbPolicy) {
  this(null, vsbPolicy, hsbPolicy);
  }

  public void setBackgroundImage(ImageIcon image) {
  this.image = image;
  }

  public void paint(Graphics g) {
  // Do not use cached image for scrolling
   getViewport().setBackingStoreEnabled(true);

    if (image != null) {
     Rectangle rect = getViewport().getViewRect();
     for (int x=0; x<rect.width; x+=image.getIconWidth()) {
     for (int y=0; y<rect.height; y+=image.getIconHeight()) {
           g.drawImage(image.getImage(), x, y, null, null); 
        }
      }

     super.paint(g);
    }
   }
}

最佳答案

这是一种方法:

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class SSCCE extends JPanel
{
    public SSCCE()
    {
        setLayout( new BorderLayout() );

        JTable table = new JTable(5, 5);
        table.setOpaque( false );
        DefaultTableCellRenderer renderer =
            (DefaultTableCellRenderer)table.getDefaultRenderer(Object.class);
        renderer.setOpaque(false);

        JScrollPane scrollPane = new JScrollPane( table );
        scrollPane.setOpaque( false );
        scrollPane.getViewport().setOpaque( false );

        final ImageIcon icon = new ImageIcon("mong.jpg");

        JPanel background = new JPanel( new BorderLayout() )
        {
            @Override
            protected void paintComponent(Graphics g)
            {
                super.paintComponent(g);

                g.drawImage(icon.getImage(), 0, 0, getWidth(), getHeight(), this);
            }
        };
        background.add( scrollPane );
        add(background);
    }

    private static void createAndShowGUI()
    {
        JPanel panel = new JPanel();

        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new SSCCE());
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

关于java - 在 jTable Java 中添加背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30380739/

相关文章:

java - 在序列图中显示多个 boolean 值

java - 单击按钮时关闭特定的 JInternalFrame

ios - 设置RubyMotion应用以播放背景音频

java - Joda 日期时间、格式化和 Mysql 时间戳

java - Eclipse/Java 新手 : package not found now, 但那是昨天

java - 限制 jtextfield 不接受空格作为第一个字符

ios - 如何在后台将一个 iOS 应用程序的 NSString 对象的值更改为另一个 iOS 应用程序?

css - 不同页面上的相同元素显示不同的 CSS

java - 如何将一个对象传递给一个类,以便当对象在类外更新时,它在类内也会过时? ( java )

java - MigLayout JTextArea 在与 linewrap=true 一起使用时不会缩小