在 jTable 中添加背景图片 Java
adding background image in jTable Java
我正在尝试在 jTable
中添加背景图片,但是当我 运行 编码时没有显示任何内容。只有白色,没有显示 jTable
,我的 java 应用程序也挂起。这段代码有什么问题吗?这也是预览。
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);
}
}
}
确保 /Images/user/lockscreen.png
在类路径中,并且您使用的是绝对路径 '/'
而不是使用 ../ or ./
之类的相对路径(或)确保绝对路径有效。
这是一种方法:
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();
}
});
}
}
我正在尝试在 jTable
中添加背景图片,但是当我 运行 编码时没有显示任何内容。只有白色,没有显示 jTable
,我的 java 应用程序也挂起。这段代码有什么问题吗?这也是预览。
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);
}
}
}
确保 /Images/user/lockscreen.png
在类路径中,并且您使用的是绝对路径 '/'
而不是使用 ../ or ./
之类的相对路径(或)确保绝对路径有效。
这是一种方法:
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();
}
});
}
}