我有一个问题,我已经在 eclipse java neon 中编程,并且正在用表做一些事情,并且进入 swing,实际上是 JTable 的组件。现在我需要向表中的组件添加一个图像,我创建了一个名为 PanelImagen 的 JPanel,它引导我使用路径等添加图像。当我在表中运行程序时,它会显示以下内容:
interfaz.PanelImagen[,0,0,0x0,无效,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");
}
}
最佳答案
如果您想将图像添加到表格中,那么您需要:
- 将
图标
添加到TableModel
- 重写
TableModel
的getColumnClass(...)
方法来告诉表格正在显示一个图标,以便表格可以使用适当的渲染器
例如:
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();
}
});
}
}
关于java - PanelImagen[,0,0,0x0,无效,layout=java.awt.BorderLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43670491/