在 Java 中向 JFrame 添加图像

发布于 2025-01-12 20:39:53 字数 1423 浏览 4 评论 0原文

我的项目的路径是:IdeaProjects/Poker2/src/poker/one

我希望使用的图像的路径是:IdeaProjects/Poker/src/poker/one/images [我的项目内的文件夹]

我在创建时使用了枚举卡片,例如红心皇后是:Hearts_Queen

我已根据枚举重命名了每个图像,例如:Hearts_Queen.png

我创建了一个 toString() 方法,可将卡片格式化为我想要的格式(西装+“_”+数字)

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);

我希望将卡片的图像添加到我的JFrame中,我该如何做?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文