简体   繁体   中英

Change JFrame's background color

I am following a video tutorial which tries to make a Java game in 2d. I have found that course's author's approach is not working properly because of changing JFrame's color doesn't show up!

His approach, the Window class:

package Modelos;
import java.awt.Color;
import java.awt.event.*;
import javax.swing.JFrame;

public class Ventana extends JFrame implements WindowListener  {

    public Ventana(String titulo){
        super(titulo);
        this.setSize(800,600);
        addWindowListener(this);
        setBackground(Color.BLACK);
    }

    @Override
    public void windowOpened(WindowEvent we) {
    }

    @Override
    public void windowClosing(WindowEvent we) {
        System.exit(0);
    }

    @Override
    public void windowClosed(WindowEvent we) {
    }

    @Override
    public void windowIconified(WindowEvent we) {
    }

    @Override
    public void windowDeiconified(WindowEvent we) {
    }

    @Override
    public void windowActivated(WindowEvent we) {
    }

    @Override
    public void windowDeactivated(WindowEvent we) {
    }
}

Panel class which is supposed to graph rectangles, triangles, circles to represent game's objects:

package Modelos;
import java.awt.*;
import java.util.ArrayList;
import javax.swing.JPanel;

public class PanelFG extends JPanel {

    ArrayList aDibujar;

    public PanelFG(ArrayList Dibujar){
        this.aDibujar=Dibujar;
    }

    public void print(Graphics g){
        Dibujable dib;
        for (int i = 0; i <aDibujar.size(); i++) {
            dib = (Dibujable) aDibujar.get(i);
            dib.dibujar(g);
        }
    }
}

And Main

package Ejecuciones;
import Modelos.*;
import java.awt.Button;
import java.awt.Color;
import java.util.ArrayList;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Ventana nuestraVentana = new Ventana("Juego de Naves");
        ArrayList ArregloDeObjetos = new ArrayList();

        PanelFG nuestroPanel = new PanelFG(ArregloDeObjetos);

        nuestraVentana.add(nuestroPanel);

        nuestraVentana.setSize(800,600);
        nuestraVentana.setVisible(true);
    }
}

RESULT: 白色JFRAME

However my approach:

WINDOW:

package Modelos;
import java.awt.Color;
import java.awt.event.*;
import javax.swing.JFrame;

public class Ventana extends JFrame implements WindowListener  {

    public Ventana(String titulo){
        super(titulo);
        this.setSize(800,600);
        addWindowListener(this);
        this.getContentPane().setBackground(Color.BLACK);
        this.setVisible(true);
    }

    @Override
    public void windowOpened(WindowEvent we) {
    }

    @Override
    public void windowClosing(WindowEvent we) {
        System.exit(0);
    }

    @Override
    public void windowClosed(WindowEvent we) {
    }

    @Override
    public void windowIconified(WindowEvent we) {
    }

    @Override
    public void windowDeiconified(WindowEvent we) {
    }

    @Override
    public void windowActivated(WindowEvent we) {
    }

    @Override
    public void windowDeactivated(WindowEvent we) {
    }
}

PANEL class is equal.

MAIN:

package Ejecuciones;
import Modelos.*;
import java.awt.Button;
import java.awt.Color;
import java.util.ArrayList;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Ventana nuestraVentana = new Ventana("Juego de Naves");
        ArrayList ArregloDeObjetos = new ArrayList();

        PanelFG nuestroPanel = new PanelFG(ArregloDeObjetos);
    }
}

黑色正确的窗口 I think it is due to the author did not used getContentPane() when creating the main JFrame to set its background to black, and I did.

I have followed the topic: JFrame.setBackground() not working -- why?

Also I do not understand how a code would work properly and other do not, considering that we do both are using Netbeans 8.1.

Any opinion/suggestion/explanation?

you never add any control to JPanel container .... gray color is default color background to de JPanel and black is set it for the JFrame. Exactly what do you want to do?

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