Basically I've created two classes; Main and JFrameOptions. However, it seems that I cannot draw a background whether I use JLabels, setContentPane or setBackground all of them doesn't work. What am I doing wrong?
Main:
package game;
public class Main{
public static void main(String[] args) {
JFrameOptions.Options();
TSDTDir.Directory();
}
}
JFrameOptions:
package game;
import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class JFrameOptions{
static String setTitle = ("Game");;
public static int width = 920;
public static int height = 517;
public static void Options() {
JFrame window = new JFrame(setTitle);
window.setSize(width, height);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
window.setLocationRelativeTo(null);
window.toFront();
window.setBackground(Color.black);
}
}
Edit:
I got the fundamental thing to work through your answers: window.getContentPane().setBackground( Color.PINK );
But how can I load an image that has to function to have a login field for example?
Edit 2: It is not working. The background doesn't draw.
public static void Options() {
//Displays the title
JFrame window = new JFrame(setTitle);
window.setSize(width, (int) height);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setIconImage(favIcon);
window.setResizable(false);
window.setLocationRelativeTo(null);
window.toFront();
//window.getContentPane().setBackground(Color.black);
BufferedImage img = null;
try {
img = ImageIO.read(new File("Background.png"));
window.setContentPane(new JLabel(new ImageIcon(img)));
} catch (IOException e) {
e.printStackTrace();
}
}
}
And of course the Background.png file is in the directory of the game folder: http://prntscr.com/9kxc4i