我有一个问题,已经在eclipse java neon中编程,并且我正在对表进行操作,并且实际上是JTable的组件。现在,我需要向表中的组件添加图像,并创建一个名为PanelImagen的JPanel,它会指导我添加带有鲁特标记的图像,等等。当我将程序运行到表中时,它说:interfaz.PanelImagen[,0,0,0x0,invalid,layout=java.awt.BorderLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]
我不知道这是什么我来自哥伦比亚,对不起我的英语。这是我初始化表的代码:
matriz = new JTable(Circuito.TAMANO_PANEL,Circuito.TAMANO_PANEL);
luces = new PanelImagen[Circuito.TAMANO_PANEL][Circuito.TAMANO_PANEL];
for (int i = 0; i < luces.length; i++) {
for (int j = 0; j < luces[0].length; j++) {
luces[i][j] = new PanelImagen("data/imagenes/white.gif");
}
}
最佳答案
如果要将图像添加到表中,则需要:
将Icon
添加到TableModel
覆盖getColumnClass(...)
的TableModel
方法以告知表正在显示图标,以便表可以使用适当的渲染器
例如:
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableIcon extends JPanel
{
public TableIcon()
{
Icon aboutIcon = new ImageIcon("about16.gif");
Icon addIcon = new ImageIcon("add16.gif");
Icon copyIcon = new ImageIcon("copy16.gif");
String[] columnNames = {"Picture", "Description"};
Object[][] data =
{
{aboutIcon, "About"},
{addIcon, "Add"},
{copyIcon, "Copy"},
};
DefaultTableModel model = new DefaultTableModel(data, columnNames)
{
// Returning the Class of each column will allow different
// renderers to be used based on Class
public Class getColumnClass(int column)
{
return getValueAt(0, column).getClass();
}
};
JTable table = new JTable( model );
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane( table );
add( scrollPane );
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Table Icon");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TableIcon());
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}