本文介绍了将图像作为背景放置在扩展 JFrame 上是否有任何问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在扩展 JFrame 上放置一个图像以将其设置为背景,扩展 JFrame 仅包含菜单栏.问题是,它不显示图片,我不知道我可能做错了什么.任何想法都受到高度赞赏

I would like to place an image on an extended JFrame to set it as background, the extended jframe contains menu bars only. The problem is that, it doesn't display the picture, I don't know what I might be doing wrong. Any ideas are highly appreciated

public class VirtualViewGUI extends JFrame{

    public VirtualViewGUI()
      {
         super("Virtual View");

         JMenuBar jmenuBar = new JMenuBar();
         JMenu fileMenu = new JMenu("File");
         JMenu helpMenu = new JMenu("Help");
         JMenu feel = new JMenu("Look & Feel");

         JMenu layOutMenu = new JMenu("ConfigureCells");
         JMenuItem add_files = new JMenuItem("Select Directory..");
         JMenuItem minCellSize = new JMenuItem("height 260 X  width 260");
         JMenuItem moderateCellSize = new JMenuItem("height 320 X  width 320");
         JMenuItem maxCellSize = new JMenuItem("height 360 X  width 360");
         JMenuItem exit = new JMenuItem("Exit");
         JMenuItem help = new JMenuItem("Help Content");

         fileMenu.add(add_files);
         fileMenu.add(exit);
         layOutMenu.add(minCellSize);
         layOutMenu.add(moderateCellSize);
         layOutMenu.add(maxCellSize);
         helpMenu.add(help);

         jmenuBar.add(fileMenu);
         jmenuBar.add(layOutMenu);
         jmenuBar.add(helpMenu);

         ImageIcon myImage=new ImageIcon("grid_2.png");

         JLabel icon = new JLabel(myImage);
         icon.setIcon(myImage);
         setJMenuBar(jmenuBar);

         add(icon);


         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      }
}

推荐答案

你的代码没有缺陷,因为当我在我的电脑上运行它时,它工作正常,如果图像路径是好的.因此,如果我使用 getClass().getResource("/pathToImageFolder/myImage.fileExtension"),它会给我想要的结果.似乎您需要检查您指定的路径,即grid_2.png",是否实际上是正确的路径!看看我的这个关于如何将图像添加到 NETBEANS 中的资源文件夹的答案,希望这可以帮助你更多的.这是一个有效的示例

Your code has got no flaw, since as I ran that on my computer it's working fine, if the path to the image is good. So if I use getClass().getResource("/pathToImageFolder/myImage.fileExtension"), it's giving me the desired outcome. Seems like you need to check if the path specified by you i.e. "grid_2.png", is actually the right path or not !! Have a look at this answer of mine regarding HOW TO ADD IMAGES TO YOUR RESOURCE FOLDER IN NETBEANS, hope this might can help you more. Here is one working example

import javax.swing.*;

public class VirtualViewGUI extends JFrame
{
    public VirtualViewGUI()
    {
        super("Virtual View");

        JMenuBar jmenuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenu helpMenu = new JMenu("Help");
        JMenu feel = new JMenu("Look & Feel");

        JMenu layOutMenu = new JMenu("ConfigureCells");
        JMenuItem add_files = new JMenuItem("Select Directory..");
        JMenuItem minCellSize = new JMenuItem("height 260 X  width 260");
        JMenuItem moderateCellSize = new JMenuItem("height 320 X  width 320");
        JMenuItem maxCellSize = new JMenuItem("height 360 X  width 360");
        JMenuItem exit = new JMenuItem("Exit");
        JMenuItem help = new JMenuItem("Help Content");

        fileMenu.add(add_files);
        fileMenu.add(exit);
        layOutMenu.add(minCellSize);
        layOutMenu.add(moderateCellSize);
        layOutMenu.add(maxCellSize);
        helpMenu.add(help);

        jmenuBar.add(fileMenu);
        jmenuBar.add(layOutMenu);
        jmenuBar.add(helpMenu);

        ImageIcon myImage = new ImageIcon(
            getClass().getResource(
                    "/image/cow-cartoon.jpg"));

        JLabel icon = new JLabel(myImage);
        icon.setIcon(myImage);
        setJMenuBar(jmenuBar);

        add(icon);

        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationByPlatform(true);
        setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new VirtualViewGUI();
            }
        });
    }
}

这篇关于将图像作为背景放置在扩展 JFrame 上是否有任何问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 23:21