2017-08-18 446 views
0

我正在關注一個試圖在2d中製作Java遊戲的視頻教程。我發現課程的作者的方法無法正常工作,因爲更改JFrame的顏色不顯示!更改JFrame的背景顏色

他的做法,在窗口類:

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

但是我的方法:

窗口:

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 我認爲這是由於作者沒有使用的getContentPane創建主要的JFrame時,它的背景設置爲黑色,和我做了()。

我都跟着話題:JFrame.setBackground() not working -- why?

此外,我不知道如何代碼將正常工作和其他不這樣做,考慮到我們做到既使用的是NetBeans 8.1。

任何意見/建議/解釋?

+0

當然,您應該感謝我們閱讀這麼多的代碼。 – Blasanka

+0

遵循[oracle swing教程](https://docs.oracle.com/javase/tutorial/uiswing/start/index.html),而不是堅持進入視頻教程。 – Blasanka

+1

默認情況下,面板不透明。你的方法永遠不會將面板添加到框架,這就是爲什麼它保持黑色。 –

回答

0

您永遠不會將任何控件添加到JPanel容器....灰色是JPanel的默認顏色背景,黑色是爲JFrame設置的。到底你想要做什麼?