2016-03-09 148 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