0

I am trying to build a window with one picture that covers up the screen. The picture is a JLabel and the window is a JFrame. After trying countless ways and looking up multiple tutorials for hours, I have not figured out how to do this. I agree, this is a very simple question, but I simply do not understand how I can approach this problem. Here is my code I have tried(I commented out some things that I tried earlier):

package Buttons;

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.GridLayout;

 public class Mewindow extends JFrame {

private JFrame mewindow;
private JLabel mepic = new JLabel(new ImageIcon("me.png"));

public Mewindow() {

    super("Here is a picture of ME!");

    mewindow.setLayout(new GridLayout(1, 0, 0, 0));

    // Icon me = new ImageIcon(getClass().getResource("me.png"));

    add(mepic);

    mewindow.setVisible(true);
    mewindow.setSize(250, 250);
    mewindow.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}

}

Thank you so much for the time you are taking for reading this, I really appreciate the effort you are putting into helping a fellow programmer!

Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83

1 Answers1

2

Problem #1...

You have no main method, so unless you're creating the class from another class, it won't run...

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            }

            Mewindow frame = new Mewindow();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
}

Problem #2...

Now, you will run into a NullPointerException, because mewindow is not initialised, but, you don't actually need it, because you're using the values within the constructor of the class, so you would end up with a StackOverflowException if you tried to intiialise it ... but it doesn't make sense to use it anyway...

public class Mewindow extends JFrame {

    private JLabel mepic
    public Mewindow() {

        super("Here is a picture of ME!");

        setLayout(new GridLayout(1, 0, 0, 0));

        mepic = new JLabel(new ImageIcon(getClass().getResource("me.png")));
        add(mepic);

        setVisible(true);
        setSize(250, 250);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }
}

Now your code assumes that me.png is stored within the same package as the Mewindow, just beware of that.

And, the resulting code actually running (replace with my own picture)

MeWindow

Suggestions...

Don't extend directly from JFrame, use a JPanel instead and then add that to an instance of JFrame, your code will be more re-usable

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank you, this was very well explained. I guess I should have directly put the image into a JLabel itself! What do you mean "Don't extend directly from JFrame, use a JPanel instead and then add that to an instance of JFrame, your code will be more re-usable" ? Also, my image isn't filling up the whole window, why is that? It may be the image itself though...? – Ruchir Baronia Aug 26 '15 at 01:40
  • It's generally discouraged to extend from a top level container as it will lock you into a single use case, and you're not really adding any new functionality to the class. It also comes with a bunch of other management issues which are best left to the implementation. `JLabel` doesn't scale the image, ever, in fact, no Swing component does, instead, you need to implement the functionality yourself – MadProgrammer Aug 26 '15 at 01:44
  • Have a look at [this example](http://stackoverflow.com/questions/14548808/scale-the-imageicon-automatically-to-label-size/14553003#14553003) and [this example](http://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959928#11959928) and [this example](http://stackoverflow.com/questions/14115950/quality-of-image-after-resize-very-low-java/14116752#14116752) – MadProgrammer Aug 26 '15 at 01:44