简体   繁体   中英

Java - Color or Image cannot be set as Background

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

Image of image file location:
在此处输入图片说明

Console image:
在此处输入图片说明

Running GUI image:
在此处输入图片说明

This is a common problem, you are calling setVisible before the UI is established. Swing is lazy when it comes to laying out changes to the UI, requiring you to decide when the UI should be updated, this is deliberate and done in order to maintain efficiency (imagine adding a dozen new components to the UI, you wouldn't want the UI to be revalidated and updated until you're done, otherwise you'd be wasting a lot of time)

The simple solution is, call setVisible last...

public static void Options() {

    //Displays the title 
    JFrame window = new JFrame(setTitle);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setIconImage(favIcon);

    //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();
    }
    window.setResizable(false);
    window.pack();
    window.setLocationRelativeTo(null);
    window.setVisible(true);
    window.toFront();
}

Remember though, a JLabel has no layout manager by default and even when you apply one, it won't use it to calculate its preferredSize , relying only on the icon and text properties.

Have a look at this discussion for more details and alternatives

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM