0

I am new to trying to create a java interface and am wanting to create one as part of a college project.

at the moment i'm still just working on the opening interface but cant seem to set a back ground image to my frame. Ive watched all the youtube videos i could find and looked through all the forums but still nothing seems to work.

all the examples ive seen havent had buttons and textboxes already put on it so im not sure if this is the issue but in my 'try and catch' i just constantly get 'image doesn't exist' even though i have put the image with the correct file name in.

like i said i am new to working with interfaces so for all i know it could be really simple or i havent really messed it all up but if someone can help out it would be really apreciated.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;

import javax.imageio.*;

public class CoopWelcome extends JFrame {

    private ImageIcon image1;
    private JButton b1;
    private JTextField userName;
    private static String password = "";
    private JPanel backGround;

    CoopWelcome() {
        setLayout(new FlowLayout());
        //creating username textbox
        userName = new JTextField("");
        userName.setText("Username");
        userName.setForeground(Color.GRAY);
        userName.setColumns(10);
        getContentPane().add(userName);
        //creating password textbox
        JPasswordField passWord = new JPasswordField(10);
        passWord.setEchoChar('*');
        passWord.addActionListener(new AL());
        getContentPane().add(passWord);
        //adding the button and the label to the panel
        b1 = new JButton("something");
        getContentPane().add(b1);
        //getting the image and displaying to the label
    }

    public static void main(String[] Args) {
        //Creating the interface
        CoopWelcome gui = new CoopWelcome();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setVisible(true);
        gui.pack();
        gui.setTitle("The Co-operative");
        try {
            gui.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("img.jpg")))));

        } catch (IOException e) {
            System.out.println("image doesn't exist");
        }
    }

    static class AL implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            JPasswordField input = (JPasswordField) e.getSource();
            char[] passy = input.getPassword();
            String p = new String(passy);
            if (p.equals(password)) {
                JOptionPane.showMessageDialog(null, "Correct");

            } else {
                JOptionPane.showMessageDialog(null, "Incorrect");
            }
        }
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Andrew Stevenson
  • 13
  • 1
  • 1
  • 4
  • That has to be some of the worst code formatting I've seen in code here in 2 weeks. But seriously, your file path is not correct. You want to use a path that is relative to the user directory which can be found by either creating a file object and then printing out its path or by this line: `System.out.println(System.getProperty("user.dir"));`. Also you will probably want to get the image not as a file but as a resource. Have a Google on that for examples of just what I mean. – Hovercraft Full Of Eels Oct 26 '13 at 04:29
  • For more on retrieving resources, please see: [StackOverflow: Jar get image as resource](http://stackoverflow.com/questions/10163012/jar-get-image-as-resource) and [Tutorial: Retrieving Resources](http://docs.oracle.com/javase/tutorial/deployment/webstart/retrievingResources.html). – Hovercraft Full Of Eels Oct 26 '13 at 04:38
  • *"..college project .. set a back ground image.."* For this project direct extra effort into design and documentation, rather than glitz. – Andrew Thompson Oct 26 '13 at 06:45

3 Answers3

3

gui.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("img.jpg")))));

Reasonable approach, but the problem is that by default a JLabel doesn't use a layout manager to you can't easily add components to it. Try:

JLabel background = new JLabel(...);
background.setLayout( new BorderLayout() );
gui.setContentPane( background );
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Don't forget to make the background JLabel opaque since a contentPane should always be opaque, and JLabels are not opaque by default. 1+. – Hovercraft Full Of Eels Oct 26 '13 at 04:35
  • I think i understand what you mean but now i have the issue of having an interface that is just grey with no text boxes or buttons on it now. I'm sorry if i seem really stupid with this i am still just getting to grips with interfaces but this is the code i have entered just incase ive messed it up. backGround = new JLabel(new ImageIcon("img.jpg")); backGround.setLayout( new BorderLayout() ); gui.setContentPane( backGround ); backGround.setOpaque(true); – Andrew Stevenson Oct 27 '13 at 02:33
0

The problem is now solved.

I basically completely redid my JFrame and labels and stuff and managed to get it to work. probably still terribly formatted but hey at least it works now and it is now a hell of a lot easier for future to know where i might be going wrong.

    CoopWelcome() {

setLayout (new FlowLayout());

//username field

userName = new JTextField("");
userName.setText("Username");
userName.setForeground(Color.GRAY);
userName.setColumns(10);
getContentPane().add(userName);

    //password field

JPasswordField passWord = new JPasswordField(10);
passWord.setEchoChar('*');
passWord.addActionListener(new AL());
getContentPane().add(passWord);

//button

b1 = new JButton("something");
getContentPane().add(b1);


//frame

setSize(500,600);
    setVisible(true); setLayout(new BorderLayout());
    JLabel background=new JLabel(new ImageIcon("img.jpg"));
    add(background);
background.setLayout(new FlowLayout());

}

public static void main(String[] Args){

    new CoopWelcome();

}
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Andrew Stevenson
  • 13
  • 1
  • 1
  • 4
-1
JFrame f = new JFrame();
f.setLayout(new BorderLayout());
f.setContentPane(new JLabel(new ImageIcon("someImage.png")));
f.setSize(300,300);
f.setSize(301,301); //just a refresh
mkaminsky
  • 353
  • 2
  • 10