繁体   English   中英

Java将文件中的图像读取到ArrayList中并显示在JPanel上

[英]Java Read images from a file into an ArrayList and displaying onto a JPanel

我正在尝试获取一个充满图像的文件,并将它们读入ArrayList中以制作一副纸牌。 然后将其显示在JPanel上。 这是我的代码:

   private static class CardDealer extends JFrame 
{        
    private ImageIcon[] cards = new ImageIcon[52];
    private ArrayList<ImageIcon> deck = new ArrayList<ImageIcon>();




    private JButton deal;
    private JPanel faceDown, faceUp, button;
    private JLabel backs, fronts;
    private Random card = new Random(52);


    public CardDealer() throws FileNotFoundException
    {

       File images = new File("src/Images");
       Scanner file = new Scanner(images);
       for(int i=0; i<cards.length; i++)
       {

            cards[i] = new ImageIcon(Arrays.toString(images.list()));

            deck.add(cards[i]);
        }


        //setTitle to set the title of the window

        setTitle("Card Dealer");

        //set the application to close on exit

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Call all our build panel methods
        buildButtonPanel();
        buildFaceDownPanel();
        buildFaceUpPanel();

        setLayout(new BorderLayout());

        add(button, BorderLayout.SOUTH);
        add(faceDown, BorderLayout.WEST);
        add(faceUp, BorderLayout.EAST);

        pack();

        validate();

        setVisible(true);
    }

    private void buildButtonPanel()
    {
        button = new JPanel();

        deal = new JButton("Deal");

        deal.addActionListener(new buttonListener());

        button.add(deal);

    }

    private void buildFaceDownPanel()
    {
        faceDown = new JPanel();

        backs = new JLabel();

        backs.setText("Cards");

        backs.setIcon(new ImageIcon("Blue.bmp"));



        faceDown.add(backs);

    }

    private void buildFaceUpPanel()
    {
        faceUp = new JPanel();

        fronts = new JLabel();

        faceUp.add(fronts);
    }

    private class buttonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
           fronts.setIcon(deck.get(card.nextInt()));
        }
    }

}


public static void main(String[] args) throws FileNotFoundException
{
    new CardDealer();
}

我不知道为什么在运行程序时没有图像出现。 运行时即使背面JLabel图像也不会显示。 任何帮助表示赞赏!

让我们以File#list开头将返回包含在指定文件位置中的File对象的数组,这使得

cards[i] = new ImageIcon(Arrays.toString(images.list()));

出于两个原因,这非常令人担忧,除了您只是将文件列表转换为String ,执行此cards.length效率很低……

接下来,您永远不要在代码中的任何路径中引用src ,一旦将其构建和打包, src将不再存在,并且您将无法像普通文件一样访问“文件”(因为它们很可能嵌入在jar中)文件)。

这就提出了另一个问题,因为列出内嵌在jar文件中的资源并不容易(当您甚至不知道Jar文件的名称时),您就需要一种不同的方法。

一个可能是命名在一个共同的,顺序的方式的图像,像/images/Card0.png/images/Card1.png例如,然后简单地通过环加载图像

File images = new File("src/Images");
for(int i=0; i < cards.length; i++)
{
    cards[i] = new ImageIcon(
                       ImageIO.read(
                           getClass().getResource("/images/Card" + i + ".png")));
    deck.add(cards[i]);
}

查看阅读/加载图像以获取有关阅读/加载图像的更多详细信息

另一个解决方案可能是创建一个文本文件,该文件可以存储在应用程序上下文中,其中列出了所有卡名。 您可以使用Class#getResourceClass#getResourceAsStream加载文件,读取文件内容并加载每个图像...

暂无
暂无

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

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