简体   繁体   English

Java-在JTable中渲染图像

[英]Java - Rendering Image in JTable

I'm writing a program to play a game of cards. 我正在编写一个玩纸牌游戏的程序。 I already made the game work, and effectively play it, but now I have decided to add images of cards (right now it works but using the names of cards, like "As of Spades", instead of an icon to represent them). 我已经使游戏工作并可以有效地进行游戏了,但是现在我决定添加纸牌图像(现在可以使用,但是使用纸牌名称(例如“黑桃纸”),而不是使用图标来代表它们)。

In my program, I use JTable s to organize the cards and for the selection of them in the various JDialog s I put them in (one dialog for swapping the cards in hand, another to select card to discard, etc.) 在我的程序中,我使用JTable来组织卡片,并在放入它们的各种JDialog进行选择(一个对话框用于交换卡片,另一个对话框选择要丢弃的卡片,等等)。

What I tried, and I personally like how it'd work, its to make a JTable with 8 columns and 1 row of cells for each card. 我尝试过,而且我个人喜欢它的工作方式,它为每个卡制作了一个具有8列和1行单元格的JTable And inside of each cell put a image of the card. 在每个单元格的内部放一张卡片图片。 Then I'd select a cell to select a card, or use a JButtonGroup outside of the table. 然后,我将选择一个单元格来选择卡,或者在表外使用JButtonGroup

    DefaultTableModel dtModel = new DefaultTableModel(COL_NAMES, 0) {
        @Override
        public Class<?> getColumnClass(int column) {
            if (getRowCount() > 0)
                return getValueAt(0, column).getClass();
            return super.getColumnClass(column);
        }
    };

    //add the columns to the model:
    if (dtModel.getColumnCount() == 0) {
        for (int i = 0; i < COLS; i++) {
            dtModel.addColumn(COL_NAMES[i]);
        }
    }
    //add a row to the model:
    if (dtModel.getRowCount() == 0) {
        Object[] data = {new JLabel(), new JLabel(), new JLabel(), new JLabel(), new JLabel(), new JLabel(), new JLabel(), new JLabel()}; 
        dtModel.addRow(data);
    }

    jTable1.setModel(dtModel);

    //set the size of the table, but I think I got it wrong:
    jScrollPane1.setSize(400, jScrollPane1.getColumnHeader().getHeight() + jTable1.getRowHeight());

    //here is the image I'm using:
    ImageIcon ii = new ImageIcon("C:\\Users\\DeRipper\\Pictures\\Naipes\\oros_1s.jpg");

    //the loop where I set the image in all the cells. I scale the image into a smaller size:
    for (int i = 0; i < COLS; i++)
        jTable1.setValueAt(new ImageIcon(ii.getImage().getScaledInstance(50, 65, Image.SCALE_DEFAULT)), 0, i);

Where "C:\\\\Users\\\\DeRipper\\\\Pictures\\\\Naipes\\\\oros_1s.jpg" is the path to the card file. 其中"C:\\\\Users\\\\DeRipper\\\\Pictures\\\\Naipes\\\\oros_1s.jpg"是卡文件的路径。 I was first testing my code by putting the same card image for all the cells. 我首先通过为所有单元放置相同的卡映像来测试代码。

At first glance I got the desired result, the images were displayed correctly, but when clicking on them a couple of times the table would stop rendering them and instead showed the toString() value of the images: 乍一看,我得到了预期的结果,图像正确显示,但是当单击它们两次时,表格将停止渲染它们,而是显示图像的toString()值:

Card Images in JTable(2) JTable中的卡片图像(2)

Card Images in JTable(3) JTable中的卡片图像(3)

And then the images would not show on the table again. 然后图像将不再显示在桌子上。 I just need the user to be able to click on the images and not disappear. 我只需要用户能够单击图像而不会消失。

Well, thanks for reading. 好吧,感谢您的阅读。

DeR. DeR。

Object[] data = {new JLabel(), new JLabel(), new JLabel(), new JLabel(), new JLabel(), new JLabel(), new JLabel(), new JLabel()}; 
dtModel.addRow(data);

Don't add JLabel s to the model. 不要将JLabel添加到模型中。 Instead you need to add an ImageIcon . 相反,您需要添加ImageIcon

Then the JTable will use the Icon renderer to display the image. 然后, JTable将使用Icon渲染器显示图像。

Simple Example 简单的例子

but when clicking on them a couple of times the table would stop rendering them and instead showing the "toString()" value of the images: 但是当单击它们两次时,表格将停止渲染它们,而是显示图像的“ toString()”值:

If you are editing the cells, then the default editor will just save the toString() representation of the object back to the TableModel. 如果要编辑单元格,则默认编辑器将只将对象的toString()表示形式保存回TableModel中。 So you might want to override the isCellEditable(...) method to turn editing off. 因此,您可能需要重写isCellEditable(...)方法以关闭编辑。 Otherwise you would need a custom editor that knows how to edit and save an ImageIcon . 否则,您将需要一个自定义编辑器,该编辑器知道如何编辑和保存ImageIcon

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM