在JScrolPane中显示图片

时间:2019-03-05 03:28:28

标签: jlabel jscrollpane imageicon

我正在一个个人项目上,订购我所玩的棋盘游戏的纸牌收藏。因此,我建立了卡片数据库,现在我试图在JScollPane中显示卡片图像。这是我到目前为止的内容:

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JScrollPane;

public class Main extends javax.swing.JFrame
{
private JScrollPane jScrollPane1;
static CardDatabase database;

public Main()
{
    initComponents();  
    browseCards();
}

public static void main(String[] args) 
{
    database = new CardDatabase("cardList");

    java.awt.EventQueue.invokeLater(new Runnable() 
    {
        public void run() 
        {
            new Main().setVisible(true);
        }
    });
}

private void initComponents() 
{
    jScrollPane1 = new javax.swing.JScrollPane();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup
    (
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addGap(190, 190, 190)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)
            .addContainerGap())
    );
    layout.setVerticalGroup
    (
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 578, Short.MAX_VALUE)
            .addContainerGap())
    );

    pack();
}

private void browseCards()
{
    jScrollPane1.add(new JLabel(new ImageIcon(database.database[0].img)));
    jScrollPane1.repaint();
}
}

图像位于BufferedImage中,因此在将其添加到JLabel(我尝试将其添加到Jscrollpane)之前,将其更改为ImageIcon。 This is the result i'm looking for

谢谢:D

0 个答案:

没有答案