1 year ago

#268843

test-img

Diego Uribe

Adding an Image to JFrame in Java

The path of my project is: IdeaProjects/Poker2/src/poker/one

The path of my images I wish to use is: IdeaProjects/Poker/src/poker/one/images [a folder inside my project]

I used enumeration on creating the cards and so for example a queen of hearts is: Hearts_Queen

I have renamed every image according to the enumeration such as: Hearts_Queen.png

I have created a toString() method that formats the card into my desired format (suit + "_" + number)

public void pokerTableFrame(){
        pokerTableFrame = new JFrame();
        pokerTableFrame.setSize(1200,800);
        pokerTableFrame.setLocationRelativeTo(null);

        newGame = new JButton("New Game");
        newGame.setBounds(70,70,70,70);
        JPanel buttonNG = new JPanel();
        buttonNG.setSize(new Dimension(500, 500));
        buttonNG.add(newGame);
        pokerTableFrame.add(buttonNG, BorderLayout.SOUTH);

        //Here are my attempts
        //String icon1s = Poker.flop1.toString();
        //Icon icon = new ImageIcon(Poker.flop1.toString() + ".png");
        //getContentPane().add((Component) icon);
        //JLabel icon1s = new JLabel();
        //icon1s.setIcon(icon);
        //add(icon1s, BorderLayout.EAST);

        //pokerTableFrame.add(new JLabel(new ImageIcon(Poker.flop1.toString() + ".png")));
        pokerTableFrame.add(new JLabel(new ImageIcon("/Poker2/src/poker/one/images" + Poker.flop1.toString() + ".png")));



        pack();
        pokerTableFrame.setVisible(true);
        pokerTableFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

I wish to add the image of a card(s) to my JFrame, how can I go about doing that?

java

image

swing

jframe

imageicon

0 Answers

Your Answer

Accepted video resources