简体   繁体   English

如何从源文件夹添加图像并将其映像到JFrame?

[英]How do you add and image into JFrame from a source folder?

I have tried numerous ways of added a jpg image into the Jframe without success, the code will compile but the image wont appear. 我尝试了多种方法,但没有成功将jpg图像添加到Jframe中,这些代码可以编译,但是图像不会出现。 Th image is stored in a source file in the project! 图像存储在项目的源文件中! Thanks for the help. 谢谢您的帮助。

public class GordonsWindow extends JFrame {
    public GordonsWindow() {
        JMenuBar menuBar = new JMenuBar();
        JMenu menuFile = new JMenu();
        JMenu menuStores = new JMenu();
        JMenuItem menuFileExit = new JMenuItem();

        JMenuItem menuStoresArmagh = new JMenuItem();

        JPanel paneStores = new JPanel(new FlowLayout());
        paneStores = new JPanel();
        paneStores.setPreferredSize(new Dimension(500,300));
        paneStores.setBackground(Color.blue);
        JButton ArmaghButton = new JButton();
        ImageIcon icon = new ImageIcon("Gordons.jpg"); 
        JLabel label = new JLabel(); 
        label.setIcon(icon);        

        ArmaghButton.setText("Armagh");

        paneStores.add(ArmaghButton);
        paneStores.add(label);

        add(paneStores);

        menuFile.setText("File");
        menuFileExit.setText("Exit");

        menuStores.setText("Stores");
        menuStoresArmagh.setText("Armagh");

        ArmaghButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Armagh.windowActivated();
            }
        });

        menuFileExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                GordonsWindow.this.windowClosed();
            }
        });

        menuFile.add(menuFileExit);
        menuBar.add(menuFile);

        menuBar.add(menuStores);
        menuStores.add(menuStoresArmagh);

        setTitle("Gordons Chemists Application");
        setJMenuBar(menuBar);
        setSize(new Dimension(800, 800));

        //Add Window Listener
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                GordonsWindow.this.windowClosed();
            }
        });
    }

    protected void windowClosed() {
        System.exit(0);
    }
}

You want to load it through your class path. 您想通过类路径加载它。 Simply 只是

ImageIcon icon = new ImageIcon(GordonsWindow.class.getResource("/images/Gordons.jpg");

Where images is located directly in the src images直接位于src

ProjectRoot
         src
            images
                 Gordons.jpg

sorry; 抱歉; missed that label.seticon() the other code not needed for the example confuzled me. 错过了label.seticon()这个示例不需要的其他代码使我困惑。

Next time use an SSCCE 下次使用SSCCE

and then you need to draw it on a component somehow; 然后您需要以某种方式将其绘制在组件上;

Method i use is: 我使用的方法是:

JPanel imageholder=new JPanel()
        {
            protected void paintComponent(Graphics g)
            {
                super.paintComponent( g)
                g.drawImage(ImageVariable,0,0);
            }
        };
        j.setBounds(<insert size here>);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM