当前位置: 技术问答>java相关
有没有办法把image插入JTable中??
来源: 互联网 发布时间:2015-03-28
本文导语: | 例子我也没有你自己写吧 class MyTableModel extends AbstractTableModel { final String[] columnNames = {"First Name", "Last Name", ...
|
例子我也没有你自己写吧
class MyTableModel extends AbstractTableModel {
final String[] columnNames = {"First Name",
"Last Name",
"photo",
"# of Years",
"Vegetarian"};
final Object[][] data = {
{"Mary", "Campione",
new ImageIcon("filename");new Integer(5), new Boolean(false)},
{"Alison", "Huml",
new ImageIcon("filename"),new Integer(3), new Boolean(true)};
//只需要生成ImageIcon对象就可以
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
return false;
}
public void setValueAt(Object value, int row, int col) {
...
}
class MyTableModel extends AbstractTableModel {
final String[] columnNames = {"First Name",
"Last Name",
"photo",
"# of Years",
"Vegetarian"};
final Object[][] data = {
{"Mary", "Campione",
new ImageIcon("filename");new Integer(5), new Boolean(false)},
{"Alison", "Huml",
new ImageIcon("filename"),new Integer(3), new Boolean(true)};
//只需要生成ImageIcon对象就可以
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
return false;
}
public void setValueAt(Object value, int row, int col) {
...
}
|
试试这个,图片名字,自己还!
import java.awt.*;
import java.awt.Graphics;
import java.awt.Image.*;
import javax.swing.*;
class showImage extends JPanel{
public static void main(String[] args){
int width;
int height;
ImageIcon icon = new ImageIcon("insect.jpg");
width = icon.getIconWidth();
height = icon.getIconHeight();
JLabel label = new JLabel(icon);
label.setHorizontalAlignment(SwingConstants.LEFT);
label.setVerticalAlignment(SwingConstants.TOP);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setContentPane(label);
f.setSize(width,height);
f.setVisible(true);
System.out.println("Hello World!");
}
}
import java.awt.*;
import java.awt.Graphics;
import java.awt.Image.*;
import javax.swing.*;
class showImage extends JPanel{
public static void main(String[] args){
int width;
int height;
ImageIcon icon = new ImageIcon("insect.jpg");
width = icon.getIconWidth();
height = icon.getIconHeight();
JLabel label = new JLabel(icon);
label.setHorizontalAlignment(SwingConstants.LEFT);
label.setVerticalAlignment(SwingConstants.TOP);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setContentPane(label);
f.setSize(width,height);
f.setVisible(true);
System.out.println("Hello World!");
}
}