在 Java 中向 JFrame 添加图像
我的项目的路径是: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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论