2016-03-09 214 views
0

所以我想在自己的業餘時間製作我的第一個小型java遊戲。我遇到了佈局問題。Java swing佈局,3個面板

我想要遊戲是一個特定的大小(600高度,800寬度),我想在主框架內的3個「面板」。一個是主要的遊戲框架,它的高度爲500,寬度爲600,右側的庫存/信息面板應該有500高度和200寬度,最後在底部有一個文本面板,用於保存高度爲100的信息寬度爲800.到目前爲止,這裏是我所擁有的。 (我沒有發現面板高度,因爲我沒有發現任何變化)。

我會如何去製作一個框架,裏面有3個面板。我看了一下如何使用GridBagLayout(),但看起來我已經變得更糟,並且不完全理解如何使用GridBagLayout(),即使是文檔(是的,我也是愚蠢的)。

LMK,如果你不明白我的代碼的部分或我的帖子。謝謝。

package Frame; 

import java.awt.BorderLayout; 
import java.awt.Canvas; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class frame { 

//Sets variables such as height, width and title 
private JFrame frame; 
private Canvas canvas; 
private JPanel mainWindow, infoWindow, textWindow; 

//main constructor to create the frame of the game. Is called in Launcher 
//Sets the parameters of the frame. User defined in main. 
public frame(){ 
    createDisplay(); 
} 
//sets the properties of the display 
private void createDisplay() { 
    frame = new JFrame(); 
    frame.setLayout(new GridBagLayout()); 
    GridBagConstraints c = new GridBagConstraints(); 
    frame.setTitle("Island Man"); 
    frame.setSize(800, 600); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setResizable(false); 
    frame.setVisible(true); 

    canvas = new Canvas(); 
    canvas.setSize(new Dimension(800, 600)); 
    canvas.setMaximumSize(new Dimension(800, 600)); 
    canvas.setMinimumSize(new Dimension(800, 600)); 

    mainWindow = new JPanel(); 
    mainWindow.setBackground(Color.CYAN); 
    c.fill = GridBagConstraints.HORIZONTAL; 
    c.gridx = 0; 
    c.gridy = 0; 
    c.weightx = 2; 
    c.weighty = 2; 
    frame.add(mainWindow, c); 

    infoWindow = new JPanel(); 
    infoWindow.setBackground(Color.GREEN); 
    c.fill = GridBagConstraints.HORIZONTAL; 
    c.gridx = 3; 
    c.gridy = 0; 
    c.weightx = 0; 
    c.weighty = 2; 
    frame.add(infoWindow, c); 

    textWindow = new JPanel(); 
    textWindow.setBackground(Color.MAGENTA); 
    c.fill = GridBagConstraints.HORIZONTAL; 
    c.gridx = 3; 
    c.gridy = 0; 
    c.weightx = 0; 
    c.weighty = 3; 
    frame.add(textWindow, c); 

    frame.add(canvas); 
    frame.pack(); 
} 
} 
+1

什麼是你的問題? – user3437460

+0

如何製作具有這些尺寸的這3個面板的框架?我看了一下,看到GridBagLayout()會很好用,但我遇到了一些麻煩。 –

+0

由於FredK提到,使用setPreferredSize。不要在面板上調用setSize;佈局經理負責這個大小,並且會消除你的設置。 – VGR

回答

1

一種方法是在CENTER中使用BorderLayout,EAST中的清單面板和SOUTH中的文本面板。一定要設置每個面板的首選尺寸。

+0

所以我嘗試了,但我得到了一個巨大的灰色空間在中間和大小不正確。 –

+0

徘徊多一點,並完善它。謝謝,這對我來說是最簡單的。 –

1

如何製作具有這些尺寸的3個面板的框架?

有太多的方法來做到這一點。我最直接的方式將相應地設置大小爲每個面板,並將它們添加到主面板,然後在主面板添加到幀:

的準確佈局使用取決於..

  • 您希望如何安排子面板和
  • 當您調整幀大小時,您希望它們如何反應,並且您希望排列面板中的組件的方式爲

下面顯示了通過利用的FlowLayout的一種可能的方式。

enter image description here

class MainPanel extends JPanel 
{ 
    public MainPanel(){ 
     setPreferredSize(new Dimension(800, 600)); 
     setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0)); 
     add(new GamePanel()); 
     add(new InventoryPanel()); 
     add(new TextPanel());    
    } 
} 

class GamePanel extends Panel 
{ 
    public GamePanel(){ 
     setPreferredSize(new Dimension(500, 600)); 
     setBackground(Color.ORANGE); 
    } 
} 

class InventoryPanel extends Panel 
{ 
    public InventoryPanel(){ 
     setPreferredSize(new Dimension(200, 600)); 
     setBackground(Color.YELLOW);  
    } 
} 

class TextPanel extends Panel 
{ 
    public TextPanel(){ 
     setPreferredSize(new Dimension(100, 600)); 
     setBackground(Color.CYAN);  
    } 
} 

添加主面板的框架:

public static void main(String[] args) 
{ 
    SwingUtilities.invokeLater(new Runnable(){ 
     @Override 
     public void run(){ 
      JFrame frame = new JFrame("Game"); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.add(new MainPanel()); 
      frame.pack(); 
      frame.setLocationRelativeTo(null); 
      frame.setVisible(true);    
     } 
    }); 
} 
+1

加1,但更好的是BoxLaoyut,因爲FlowLayout尊重getPreferredSize,並且no不能與容器一起調整大小,請注意[setPreferredSize應始終覆蓋getPreferredSize](http:// stackoverflow。com/a/33399110/714968) – mKorbel

+0

我將如何獲得最後一個青色盒子在橙色和黃色框下? –

+0

@AmirAC青色面板不在任何面板下面。他們只是並排放置。它在右側,因爲我最後添加了它。 – user3437460