更改JFrame的背景颜色

时间:2017-08-18 15:52:37

标签: java swing jframe jpanel

我正在关注一个试图在2d制作Java游戏的视频教程。我发现当然作者的方法工作不正常,因为改变JFrame的颜色没有显示出来!

他的方法, 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);
        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) {
    }
}

面板 这个应该用矩形,三角形,圆形来表示游戏对象的类:

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

主要

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

结果: White JFRAME

然而我的方法是:

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课程相同。

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

BLACK correct window 我认为这是因为作者在创建主JFrame时没有使用getContentPane()来将其背景设置为黑色,而且我做了。

我已经关注了主题:JFrame.setBackground() not working -- why?

此外,我不明白代码如何正常工作而其他代码不能正常工作,因为我们两者都在使用Netbeans 8.1。

任何意见/建议/解释?

1 个答案:

答案 0 :(得分:0)

你永远不会向JPanel容器添加任何控件....灰色是de JPanel的默认颜色背景,黑色是为JFrame设置的。究竟你想做什么?