我可以给你用的主要格式为JPG,PNG 3个exmples和你想找的PIC格式工作代码的一些和平是为GIF
在这里你拥有了它,并确保你有正确的路径和在哪里与项目文件夹或src文件夹中的图片文件夹,如果图像文件夹位于src文件夹中必须添加之前的图像/ linux.gif作为SRC /图片另一个目录路径/ linux.gif
public class AnimatedIconTableExample extends JFrame {
private static final long serialVersionUID = 1L;
public AnimatedIconTableExample() {
super("AnimatedIconTable Example");
final Object[][] data = new Object[][] {
// Here is the looking for gif pictures
{ new ImageIcon("images/game.gif"),
new ImageIcon("images/linux.gif") },
// And here is the others pictures examples png and jpg
{ new ImageIcon("images/folderGreen.png"),
new ImageIcon("images/apple.jpg") } };
final Object[] column = new Object[] { "Example image gif and png",
"Example image gif and jpg" };
AbstractTableModel model = new AbstractTableModel() {
public int getColumnCount() {
return column.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return (String) column[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public Class getColumnClass(int col) {
return ImageIcon.class;
}
};
JTable table = new JTable(model);
table.setRowHeight(50);
setImageObserver(table);
JScrollPane pane = new JScrollPane(table);
getContentPane().add(pane);
}
private void setImageObserver(JTable table) {
TableModel model = table.getModel();
int colCount = model.getColumnCount();
int rowCount = model.getRowCount();
for (int col = 0; col < colCount; col++) {
if (ImageIcon.class == model.getColumnClass(col)) {
for (int row = 0; row < rowCount; row++) {
ImageIcon icon = (ImageIcon) model.getValueAt(row, col);
if (icon != null) {
icon.setImageObserver(new CellImageObserver(table, row,
col));
}
}
}
}
}
class CellImageObserver implements ImageObserver {
JTable table;
int row;
int col;
CellImageObserver(JTable table, int row, int col) {
this.table = table;
this.row = row;
this.col = col;
}
public boolean imageUpdate(Image img, int flags, int x, int y, int w,
int h) {
if ((flags & (FRAMEBITS | ALLBITS)) != 0) {
Rectangle rect = table.getCellRect(row, col, false);
table.repaint(rect);
}
return (flags & (ALLBITS | ABORT)) == 0;
}
}
public static void main(String[] args) {
AnimatedIconTableExample frame = new AnimatedIconTableExample();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(300, 150);
frame.setVisible(true);
}
}
* “请回答一个简单的解决方案。”*请问一个问题。 –
阅读[该帖子](http://stackoverflow.com/questions/14653967/animation-in-jtable) – alex2410
Swing没有问题显示.gif,.png或.jpg文件。你的代码应该没有区别。只要您正在正确阅读图像,应显示任何类型的图像。看一个简单的例子[Table Icon](http://stackoverflow.com/questions/5614875/how-to-set-icon-in-a-column-of-jtable/5615516#5615516)。 – camickr