gpt4 book ai didi

java - JTable中不同单元格中的不同imageIcon

转载 作者:太空宇宙 更新时间:2023-11-04 08:09:13 25 4
gpt4 key购买 nike

我认为 imageIcon 在每个单元格中显示不同,但由于某种原因,当我编译它时,图像没有显示。它显示图像的名称,但图像本身不显示。这是一张图片。 http://i49.tinypic.com/r9ibrn.jpg

public class movies extends JFrame {

public movies() {
initComponents();

}

private void initComponents() {

panel = new JPanel();
logo = new JLabel();
pane = new JScrollPane();


setDefaultCloseOperation(EXIT_ON_CLOSE);
setBackground(new Color(255, 255, 204));
setResizable(false);

panel.setBackground(new Color(51, 51, 51));
panel.setPreferredSize(new Dimension(290, 75));

logo.setIcon(new ImageIcon(getClass().getResource("logo.png")));
logo.setName("logo");
logo.setRequestFocusEnabled(false);
logo.setVerifyInputWhenFocusTarget(false);
logo.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));


ImageIcon icon1 = new ImageIcon("1.jpg");
ImageIcon icon2 = new ImageIcon("2.jpg");
ImageIcon icon3 = new ImageIcon("3.jpg");

String[] columnNames = {"Section 1", "Section 2"};
Object[][] data =
{
{icon1 + " Music", icon2 + " News"},
{icon2 + " Movies"},
{icon3 + " Games"},
};

DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable( model )

{
public Class getColumnClass(int column)
{
return getValueAt(0, column).getClass();
}
};


table.setPreferredScrollableViewportSize(table.getPreferredSize());

table.setBackground(new Color(255, 255, 204));
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );

table.setRowHeight(50);
pane.setViewportView(table);
table.getColumnModel().getColumn(0).setResizable(false);
table.getColumnModel().getColumn(1).setResizable(false);
}


public static void main(String args[]) {


public void run() {
new movies().setVisible(true);

}
});
}

private JLabel logo;
private JScrollPane pane;
private JPanel panel;

}

最佳答案

您可以在调用 new ImageRenderer 构造函数 ( read this ) 时传入图像的名称。

public class Movies extends javax.swing.JFrame {
public Movies() {
initComponents();
table.getColumnModel().getColumn(1).setCellRenderer(new ImageRenderer("1.jpg"));
table.getColumnModel().getColumn(0).setCellRenderer(new ImageRenderer("2.jpg"));
}
}

class ImageRenderer extends DefaultTableCellRenderer {
ImageIcon icon = null;

ImageRenderer(String iconName) {
icon = new ImageIcon(getClass().getResource(iconName));
}
}

关于java - JTable中不同单元格中的不同imageIcon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11527477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com