0
package main;

import java.awt.Dimension;
import java.awt.Image;
import java.io.InputStream;
import java.awt.Dimension; import java.awt.Image; import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.*;

public class main extends JFrame{

    public static void main(String[] args) {
        
        JFrame frame = new JFrame("My JFrame"); // Declaring and instantiated
        // Exit the app when the frame is closed
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // Set the pixel size using a dimension object
        frame.setPreferredSize(new Dimension(400, 200));

        // add the frame pack make sure everything is in order
        // Running the app to resize and see flowlayout.
        frame.pack();
        // This is for the button the user will click for a flag
        
        JPanel panel = new JPanel();
        JButton button = new JButton("Click me for a flag");
        button.addActionListener(e ->
        {
            JOptionPane.showMessageDialog(null, "Click me for a flag");
        });
        panel.add(button);
        frame.add(panel); // This is what makes the button pop up, if you miss this the button wont pop up
        frame.setVisible(true); 
    }

}

I am trying to get the image from the folder to pop up in the GUI/ Dialogue box when you click "Click me for a flag". I am able to get the GUI with the prompt to click the button but when clicking the button I am not able to get the flag to pop up. Is there a way to pull the image from the folder and display it in the GUI box?

Edit: The image is located in an img folder I created within the project

  • Your question is missing key information, where the image is located relative to the class files and relative to the user's directory. Have you searched on this prior to asking, as this has been asked many times here and elsewhere. – Hovercraft Full Of Eels Mar 25 '22 at 01:21

0 Answers0