-1

I've tried countless tutorials and looked at countless websites (including stack overflow) but none of the code had worked for me. So I've ended up asking my own question, what is wrong with my code?

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

public class titlescreen extends JFrame {


/**
 * 
 */
private static final long serialVersionUID = 1L; 
JPanel jp = new JPanel();
JButton jb = new JButton();



public titlescreen() throws IOException {

    JFrame f = new JFrame("MacroDom");
    //keep everything working   
    f.setTitle("Don't Read Me :P");
    f.setSize(5000, 5000);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);

    jb.setIcon(new ImageIcon("button.png"));
    jp.add(jb);
    add(jp);

    validate();

}

public static void main(String[] args) throws IOException {
    titlescreen ts = new titlescreen();
}

}

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Sam
  • 1
  • 4
  • In the future, please post much more information to help us understand your problem, including what the class does, what it doesn't do, what errors you might see, what else you've tried, and less information about your plight. Statements such as `"I've tried countless tutorials and looked at countless websites (including stack overflow) but none of the code had worked for me."` tell us nothing of use that can help us help you solve your problem. – Hovercraft Full Of Eels Jun 25 '16 at 21:47
  • Please go through the [tour] and do take a look at the [help] section as well as the [how to ask good questions](http://stackoverflow.com/help/how-to-ask) section, so that your questions and answers will be better making your future experiences here better. – Hovercraft Full Of Eels Jun 25 '16 at 21:47
  • 1
    `I've tried countless tutorials` - I would suggest you are starting with the wrong tutorials. You should be starting with the Swing tutorial for better examples. Maybe the tutorial on [How to Use Labels]() would be a good place to start for an example with an Icon. Notice how the code is structured differently (and better) than your code. Start with the working example and customize it. – camickr Jun 25 '16 at 22:01

2 Answers2

3

You're creating two JFrames, one, which is the object of the class itself, the this if you will, you add the JButton to, and the other that you display and held by the variable f. Solution: don't do this -- create and display only one JFrame, and add the button to it.

I would strongly urge you to almost never extend JFrame. You are painting yourself in a corner by having your class extend JFrame, forcing you to create and display JFrames, when often more flexibility is called for. In fact, I would venture that most of the Swing GUI code that I've created and that I've seen does not extend JFrame, and in fact it is rare that you'll ever want to do this. More commonly your GUI classes will be geared towards creating JPanels, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. This will greatly increase the flexibility of your GUI coding.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Alright so I got rid of the JFrame f = new JFrame(); and I also deleted all of the f.'s . Now whenever I run the program I am getting a very tiny little box with a few dots in it, does this mean my image is too small? (I think its around 200px by 300px or something like that – Sam Jun 25 '16 at 21:48
  • you should not get rid of the `JFrame f = new JFrame();`, instead you should get rid of `extends JFrame` and `add` all the `f`'s ;) – mcCat Jun 25 '16 at 21:50
  • 1
    Ouch. AGAIN please go through the [tour] and do take a look at the [help] section as well as the [how to ask good questions](http://stackoverflow.com/help/how-to-ask) section. – Hovercraft Full Of Eels Jun 25 '16 at 21:51
  • You're probably not looking in the correct location for the image file, something that "countless tutorials and looked at countless websites (including stack overflow)" will tell you. – Hovercraft Full Of Eels Jun 25 '16 at 21:51
  • Hmm hate to bother you again but now it giving me two errors in the part with "add(jp)" and "validate();" it's also giving me the option to fix them but when I do choose the quick fix and run the program there is not an image to be seen. – Sam Jun 25 '16 at 21:55
  • Do you know what the difference between the two versions is? Whether you `extend JFrame`, or create one using `JFrame f = new JFrame();`? And do you know the difference between `f.add(something);` and `add(something);`? – mcCat Jun 25 '16 at 21:57
  • according to http://stackoverflow.com/questions/13212431/jpanel-vs-jframe-in-java JFrame extends the Component and the Container – Sam Jun 25 '16 at 22:03
  • @Sam: if you're running into additional problems, you should edit your question, post your current pertinent code and error messages. But it sounds as if you would do well to go through the Swing tutorials a bit. You can find links to the Swing tutorials and to other Swing resources here: [Swing Info](http://stackoverflow.com/tags/swing/info) – Hovercraft Full Of Eels Jun 25 '16 at 22:05
0

Hooray I figured out the problem I just had the wrong directory and I was clearly uneducated with swing in java, and Thanks for all of the help in the comments! (Here is my code for reference) -

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

public class titlescreen extends JFrame {



private static final long serialVersionUID = 1L; 
JPanel jp = new JPanel();
JButton jb = new JButton();


public titlescreen() throws IOException {

    ImageIcon jbImage = new ImageIcon("/Users/Sammmy/Documents/workspace/Macrodom/src/button.jpg"); 
    jb = new JButton(jbImage); 

    //keep everything working   
    setTitle("MacroDom");
    setSize(5000, 5000);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(true);

    jp.add(jb);
    add(jp);

    setVisible(true);





}





public static void main(String[] args) throws IOException {

    new titlescreen();
}


}
Sam
  • 1
  • 4