-7

All I am trying to do is add a picture to a JFrame.

I am really confused and don't really understand... I have looked up every possible question on this site, looked on other java stuff, such as forums. I tried my best and now I must ask guys for help. I hope the code is clean and easy to read. Thanks for the help.

package zeus;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;

public class Main extends JFrame{

    public static final int WIDTH = 800;
    public static final int HEIGHT = 600;
    public static final int SCALE = 1;

    public static void Launch(){

        JFrame xF = new JFrame("xFrame");
        xF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        xF.setResizable(false);
        xF.setVisible(true);
        xF.setSize(WIDTH*SCALE,HEIGHT*SCALE);
        xF.setLocationRelativeTo(null);
        xF.add(new JLabel(new ImageIcon("/Clicker/xS/cow.png")));

    }

    public static void main(String[] args){

        Launch();

    }

}

Very sorry for the confusion, eclipse shows no error, also I am trying to open a JFrame with an image on it, that I can eventually use to create into a button to change a value of a int value.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Can you please let us know what is the error message that you are getting? – MansoorShaikh Jan 01 '14 at 05:29
  • Please describe the difficulty you are having, also please include what it is you are trying to do. As it stands now your question is going to be closed. – robbmj Jan 01 '14 at 05:30
  • 2
    If you have looked up every possible question, surely you know how to make a question yourself. – BLaZuRE Jan 01 '14 at 05:30
  • 1
    BTW - I edited your question to replace the title with a statement of intent - what you are trying to achieve. Please don't use titles like your original title in future. I suspect that is in large part why the 8 down-votes were added. It is just silly. Another factor might be claiming you have checked *"every possible question"* when.. you obviously haven't. – Andrew Thompson Jan 01 '14 at 05:54
  • Please have a look at this thread, [Load Icon Image Exception](http://stackoverflow.com/a/9866659/1057230), especially the eclipse link and the last link, which might can give you the whole idea, as to how to go about it :-) – nIcE cOw Jan 02 '14 at 13:15

3 Answers3

3

The biggest issues I can see are...

  • Extending from JFrame, but not actually using it...
  • Reliance on static when not really required...
  • Calling setVisible before anything has actually begin added. In fact, generally trying to manipulate the frame properties before anything was added to it and after it was made visible...

    public class Main {

     public static void main(String[] args){
    
         EventQueue.invokeLater(new Runnable() {
             public void run() {
    
                 JFrame xF = new JFrame("xFrame");
                 xF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                 xF.add(new JLabel(new ImageIcon("/Clicker/xS/cow.png")));
                 xF.setResizable(false);
                 xF.setSize(WIDTH*SCALE,HEIGHT*SCALE);
                 xF.setLocationRelativeTo(null);
                 xF.setVisible(true);
    
              }
         }
     }
    

    }

But since you never actually described what problems you were having, these are all guesses...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

I have a couple of tips for you:

  • If you know your frame size then there is no need to over-complicate it
  • Try using frame as the JFrame's name rather than xF so it is easier to look at.
  • Rearrange your methods so that setVisible(true); is at the end.

Now, as for your code I suggest you use two classes: One for the frame and one for the panel.

The frame class

import javax.swing.JFrame;

public class Apollo
{
    public static void main(String[] args)
    {
    Jframe frame = new JFrame("xFrame");
    frame.setSize(800,600);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new Poseidon());
    frame.setVisible(true);
    }
}

The panel class

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

public class Poseidon extends JPanel
{
    public void paintComponent(Graphics g)
    {
    g.setColor(Color.WHITE);
    g.fillRect(0,0,800,600);

    ImageIcon clicker = new ImageIcon("/Clicker/xS/cow.png");
    /*The following are two methods for image sizing,
     *Use the one that best fits your code:
     *
     *g.drawImage(clicker.getImage(), x, y, null); 
     *Fill in the arguments for x and y to locate your upper left corner
     *The image will be in it's original size
     *
     *g.drawImage(clicker.getImage(), x, y, w, h, null);
     *Fill in the arguments for w and h to set the width and height of your image
     *The image will be in it's scaled size
     */
    }
}
JavaDude
  • 75
  • 1
  • 3
  • 8
1

You can use xF.setContentPane(new JLabel(new ImageIcon(image_path)));