我希望每个人在这美好的一天都过得很好。
我在学习摇摆舞,我对如何参考一个图像感到困惑。我知道我应该使用jLabel,然后使用此.add();,将jLabel添加到框架中,但即使查看下面的Oracle文档也可以:
如何引用一个文件而不给出完整的路径仍然不清楚
c:\users\someuser\eclipse workspace\andsoon.png
我做不到。完成后,我必须将我的工作发送给我的老师,代码不会像在我的系统上那样引用文件。我尝试了几件事,最后在Eclipse中的SRC中创建了一个名为ImageAssets的新文件夹,并将文件移动到那里,但似乎没有任何效果。这是它的样子
以下是我尝试在包中显示图像的示例。
>代码>导入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的新文件夹,并将文件移动到那里,但似乎没有任何效果。这是它的样子
下面是我尝试在包中显示图像的示例。
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();
}
非常感谢你抽出时间来帮助我。我尽量说得很详细,如果有什么不清楚的,请问。