我可以給你用的主要格式爲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