JLabel 不会在 class 中显示图像
JLabel won't display image in class
我目前正在尝试创建一个 GUI,其中两个硬币图像在边框布局的中心彼此相邻显示。我有一个 class(图像),我在其中尝试使用 JLabel 显示图标,另一个 class 处理 GUI 框架 (GameGUI)。然而,尽管我尽了最大努力,标签仍未显示图像。我浏览了无数教程,但找不到任何解决方法。相对路径是正确的,我该如何解决?
图片Class
public class Images extends JLabel{
private JLabel heads, tails;
public Images() {
heads = new JLabel(new ImageIcon("img/heads.png"));
tails = new JLabel(new ImageIcon("img/tails.png"));
add(heads);
add(tails);
}
}
游戏界面 Class
public class GameGUI extends JFrame{
public GameGUI() {
super("");
setLayout(new BorderLayout(10,10));
Menu menu = new Menu();
ToolBar toolBar = new ToolBar();
Images images = new Images();
setJMenuBar(menu);
add(BorderLayout.NORTH, toolBar);
add(BorderLayout.CENTER, images);
setSize(1000, 500);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
问题:
public class Images extends JLabel{
private JLabel heads, tails;
public Images() {
heads = new JLabel(new ImageIcon("img/heads.png"));
tails = new JLabel(new ImageIcon("img/tails.png"));
add(heads);
add(tails);
}
}
JLabels 使用空布局,这意味着您的内部 JLabel 组件的大小为 0、0,而图像 JLabel 本身的首选大小相同,并且不会显示任何内容,因此您永远不想将 JLabel 添加到 JLabel。不要让图像扩展 JLabel,而是扩展 JPanel,并为其提供合适的布局,也许是 GridLayout。
例如,
public class ImagePanel extends JPanel {
private JLabel headsLabel = new JLabel();
private JLabel tailsLabel = new JLabel();
public ImagePanel(Icon headsIcon, Icon tailsIcon) {
headsLabel.setIcon(headsIcon);
tailsLabel.setIcon(tailsIcon);
setLayout(new GridLayout(1, 0);
add(headsLabel);
add(tailsLabel);
}
}
同样,我建议将图像作为资源获取,而不是作为使用 ImageIO 读取的文件,例如
Image headImage = ImageIO.read(getClass().getResource(resourcePath));
Icon headIcon = new ImageIcon(headImage);
资源路径很重要,取决于图像相对于 class 文件的位置。
我目前正在尝试创建一个 GUI,其中两个硬币图像在边框布局的中心彼此相邻显示。我有一个 class(图像),我在其中尝试使用 JLabel 显示图标,另一个 class 处理 GUI 框架 (GameGUI)。然而,尽管我尽了最大努力,标签仍未显示图像。我浏览了无数教程,但找不到任何解决方法。相对路径是正确的,我该如何解决?
图片Class
public class Images extends JLabel{
private JLabel heads, tails;
public Images() {
heads = new JLabel(new ImageIcon("img/heads.png"));
tails = new JLabel(new ImageIcon("img/tails.png"));
add(heads);
add(tails);
}
}
游戏界面 Class
public class GameGUI extends JFrame{
public GameGUI() {
super("");
setLayout(new BorderLayout(10,10));
Menu menu = new Menu();
ToolBar toolBar = new ToolBar();
Images images = new Images();
setJMenuBar(menu);
add(BorderLayout.NORTH, toolBar);
add(BorderLayout.CENTER, images);
setSize(1000, 500);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
问题:
public class Images extends JLabel{
private JLabel heads, tails;
public Images() {
heads = new JLabel(new ImageIcon("img/heads.png"));
tails = new JLabel(new ImageIcon("img/tails.png"));
add(heads);
add(tails);
}
}
JLabels 使用空布局,这意味着您的内部 JLabel 组件的大小为 0、0,而图像 JLabel 本身的首选大小相同,并且不会显示任何内容,因此您永远不想将 JLabel 添加到 JLabel。不要让图像扩展 JLabel,而是扩展 JPanel,并为其提供合适的布局,也许是 GridLayout。
例如,
public class ImagePanel extends JPanel {
private JLabel headsLabel = new JLabel();
private JLabel tailsLabel = new JLabel();
public ImagePanel(Icon headsIcon, Icon tailsIcon) {
headsLabel.setIcon(headsIcon);
tailsLabel.setIcon(tailsIcon);
setLayout(new GridLayout(1, 0);
add(headsLabel);
add(tailsLabel);
}
}
同样,我建议将图像作为资源获取,而不是作为使用 ImageIO 读取的文件,例如
Image headImage = ImageIO.read(getClass().getResource(resourcePath));
Icon headIcon = new ImageIcon(headImage);
资源路径很重要,取决于图像相对于 class 文件的位置。