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");
}
}
}
}