2017-06-30 76 views
0

我真的很困惑這個。我製作遊戲「Microtrip」。這是手機遊戲,但我正在爲個人電腦做。我有一個擴展JPanel的背景類,它只繪製一個從0,0開始並具有屏幕大小(我的遊戲是全屏)的矩形。我有一個擴展JPanel的主菜單類。我想爲主菜單添加所有內容。然後,我將所有內容添加到擴展JFrame的GameFrame類中。我有一個主類,只是調用GameFrame類。這裏是我的代碼: 背景類:在將JPanel添加到JPanel時,第一個JPanel的大小變得更低

import java.awt.*; 
import javax.swing.*; 

@SuppressWarnings("serial") 

public class Background extends JPanel{ 

    int width, height; 
    Color backgroundBlue; 

    public Background(int width, int height){ 

     this.width = width; 
     this.height = height; 
     backgroundBlue = new Color(25, 159, 229); 

    } 

    @Override 

    protected void paintComponent(Graphics g){ 
     super.paintComponent(g); 
     g.setColor(backgroundBlue); 
     g.fillRect(0, 0, this.width, this.height); 
    } 

} 

的MainMenu類:

import javax.swing.*; 

@SuppressWarnings("serial") 

public class MainMenu extends JPanel{ 

    Background background; 

    public MainMenu(int WW, int WH){ 
     //WW = window width 
     //WH = widow height 
     this.setLocation(0, 0); 
     this.setSize(WW, WH); 
     background = new Background(WW, WH); 
     this.add(background); 
    } 

} 

GameFrame類:

import main_menu.MainMenu; 
import main_menu.*; 

import java.awt.Dimension; 
import java.awt.GraphicsDevice; 
import java.awt.GraphicsEnvironment; 
import java.awt.Toolkit; 

import javax.swing.JFrame; 

import java.awt.event.*; 

@SuppressWarnings("serial") 

public class GameFrame extends JFrame{ 

    private GraphicsDevice vc; 
    private MainMenu mainMenu; 
    //private Background background; 

    public GameFrame(){ 

     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
     mainMenu = new MainMenu((int)screenSize.getWidth(), (int) screenSize.getHeight()); 
     /*background = new Background((int)screenSize.getWidth(), (int) screenSize.getHeight());*/ 
     GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
     vc= e.getDefaultScreenDevice(); 
     this.setTitle("Mictrotrip"); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setBounds(0, 0, (int)screenSize.getWidth(), (int)screenSize.getHeight()); 
     this.add(mainMenu); 
     //this.add(background); 
     this.setLocationRelativeTo(null); 
     this.setUndecorated(true); 
     this.setResizable(false); 
     vc.setFullScreenWindow(this); 
     addKeyListener(new KeyListener(){ 

      public void keyPressed(KeyEvent e){ 

       if(e.getKeyCode() == KeyEvent.VK_ESCAPE){ 
        System.exit(0); 
       } 

      } 

      public void keyReleased(KeyEvent e){ 

      } 

      public void keyTyped(KeyEvent e){ 

      } 

     }); 
    } 

} 

,我的主要類:

public class Main { 

    public static void main(String[] args) { 

     new GameFrame(); 

    } 

} 

當我運行它只是表明頂部中心的一個小矩形。 我做了以下實驗:

我忽略了mainmenu類,並直接在JFrame中添加背景(GameFrame中的註釋行),並且一切正常。爲什麼? 我讀過所有類似的問題,但沒有人幫助我。

回答

1

如果你想要背景來填充主菜單,那麼問題出在MainMenu上 - 你讓JPanel使用默認的FlowLayout,並且FlowLayouts不會考慮組件的大小(所以Jamal的解決方案將不起作用),而是其首選尺寸。如果你想要的背景,以填補主菜單,然後給MainMenu的一個BorderLayout的,然後添加你的背景組件BorderLayout.CENTER:

public MainMenu(int WW, int WH){ 
    // WW = window width 
    // WH = widow height 
    // this.setLocation(0, 0); 
    // this.setSize(WW, WH); 

    setLayout(new BorderLayout()); 
    background = new Background(WW, WH); 
    this.add(background, BorderLayout.CENTER); 
} 
+0

Thacks老兄,你幫了我很多! – FreakyOne