java hangman swing_JavaSwing:对JLabor添加图像的理解困难

我希望每个人在这美好的一天都过得很好。

我在学习摇摆舞,我对如何参考一个图像感到困惑。我知道我应该使用jLabel,然后使用此.add();,将jLabel添加到框架中,但即使查看下面的Oracle文档也可以:

如何引用一个文件而不给出完整的路径仍然不清楚

c:\users\someuser\eclipse workspace\andsoon.png

我做不到。完成后,我必须将我的工作发送给我的老师,代码不会像在我的系统上那样引用文件。我尝试了几件事,最后在Eclipse中的SRC中创建了一个名为ImageAssets的新文件夹,并将文件移动到那里,但似乎没有任何效果。这是它的样子

nputs.png%E2%80%9Dalt=%E2%80%9Ci

以下是我尝试在包中显示图像的示例。

>代码>导入Java.AWT.*;

导入javax.swing.*;

公共类机库扩展JFrame

{

jButton PlayGameButton按钮,

OptionsButton;

私有图像图标hangman7;

专用jLabel主标签;

公共静态void main(string[]参数)

{

新杭曼();

}

公共绞刑架

{

本.调整尺寸(1000800);

this.setLocationRelativeTo(空);

此.setVisible(真);

this.setDefaultCloseOperation(jframe.exit在关闭时退出);

此.settitle(“hangman”);

this.setResizable(错误);

playGameButton=new jButton(“开始游戏”);

optionsButton=新建jButton(“选项”);

//hangman7=new imageicon(getclass().getresource(“images\\hangman7.png”));//just a try at something

mainLabel=新jLabel();

mainlabel.seticon(new imageicon(“hangman7.png”));

jpanel somepanel=new jpanel();

somepanel.setlayout(new borderlayout());

somepanel.add(游戏按钮,borderlayout.west);

somepanel.add(选项按钮,borderlayout.east);

somepanel.add(主标签,borderlayout.center);

添加(somepanel);

此.validate();

}

非常感谢你抽出时间来帮助我。我尽量说得很详细,如果有什么不清楚的,请问。

如何引用一个文件而不给出完整的路径仍然不清楚

C:\users\someuser\eclipse workspace\andsoon.png

我做不到。完成后,我必须将我的工作发送给我的老师,代码不会像在我的系统上那样引用文件。我尝试了几件事,最后在Eclipse中的SRC中创建了一个名为ImageAssets的新文件夹,并将文件移动到那里,但似乎没有任何效果。这是它的样子

3c724893123503a6009b6868cfab4d03.png

下面是我尝试在包中显示图像的示例。

import java.awt.*;

import javax.swing.*;

public class Hangman extends JFrame

{

JButton playGameButton,

OptionsButton;

private ImageIcon hangman7;

private JLabel mainLabel;

public static void main(String[] args)

{

new Hangman();

}

public Hangman()

{

this.setSize(1000,800);

this.setLocationRelativeTo(null);

this.setVisible(true);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setTitle("Hangman");

this.setResizable(false);

playGameButton = new JButton("Start Game");

OptionsButton = new JButton("Options");

//hangman7 = new ImageIcon(getClass().getResource("Images\\ hangman7.png"));//just an attempt at something

mainLabel = new JLabel();

mainLabel.setIcon(new ImageIcon("hangman7.png"));

JPanel somePanel = new JPanel();

somePanel.setLayout(new BorderLayout());

somePanel.add(playGameButton, BorderLayout.WEST);

somePanel.add(OptionsButton, BorderLayout.EAST);

somePanel.add(mainLabel, BorderLayout.CENTER);

this.add(somePanel);

this.validate();

}

非常感谢你抽出时间来帮助我。我尽量说得很详细,如果有什么不清楚的,请问。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值