2015-04-02 88 views
0

我正尝试使用GridBagLayout构建一个小型Java游戏的GUI。最后,它应该是这样的:使用GridBagLayout构建GUI(Java)

Box Drawing

这是我的代码:

private GridBagConstraints c; 

    ... 

    c.gridx = 1; 
    c.gridy = 0; 
    c.weightx = 0; 
    add(playButton, c); 

    c.gridx = 1; 
    c.gridy = 1; 
    c.weightx = 0; 
    add(optionsButton, c); 

    c.gridx = 1; 
    c.gridy = 2; 
    c.weightx = 0; 
    add(manualButton, c); 

    c.gridx = 1; 
    c.gridy = 3; 
    c.weightx = 0; 
    add(exitButton, c); 

    c.gridx = 0; 
    c.gridy = 4; 
    c.weightx = 1;  
    c.anchor = GridBagConstraints.SOUTHWEST; 
    add(creditsButton, c); 

    c.gridx = 2; 
    c.gridy = 4; 
    c.weightx = 1; 
    c.anchor = GridBagConstraints.SOUTHEAST; 
    add(legalNoticeButton, c); 

目前,它看起来像这样:

Button Layout

我的问题是我是谁可以将两个按钮设置到底部而不将其他四个底部设置到顶部?

+2

您可以尝试使用BorderLayout,其中位于中心位置的JPanel中的游戏按钮,位于南部位置的JPanel中的底部按钮以及在北部,东部和西部位置中没有内容的虚拟JPanel。 – 2015-04-02 16:31:19

+0

使用“weighty”和“anchor”属性有没有更简单的解决方案? – 2015-04-02 16:53:25

+0

另一种方法是创建一个3列乘8行矩阵,使用带有空白文本的虚拟JLabel填充不需要JButton的地方。 – 2015-04-02 16:57:03

回答

1

像这样的东西是多还是少@Gilbert勒布朗解释(有细微差别):

基本上使用其他面板来你的两个按钮移动到南部和传播他们西部和东部:

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

import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TestGridBagLayout { 

    protected void initUI() { 
     JFrame frame = new JFrame("test"); 
     final JPanel centerPanel = new JPanel(); 
     centerPanel.setLayout(new GridBagLayout()); 
     JButton playButton = new JButton("Play"); 
     JButton optionsButton = new JButton("Options"); 
     JButton manualButton = new JButton("Manual"); 
     JButton exitButton = new JButton("Exit"); 
     JButton creditsButton = new JButton("Credits"); 
     JButton legalNoticeButton = new JButton("Legal"); 
     GridBagConstraints c = new GridBagConstraints(); 
     c.gridwidth = GridBagConstraints.REMAINDER; 
     centerPanel.add(playButton, c); 
     centerPanel.add(optionsButton, c); 
     centerPanel.add(manualButton, c); 
     centerPanel.add(exitButton, c); 

     JPanel bottomPanel = new JPanel(new BorderLayout()); 
     bottomPanel.add(creditsButton, BorderLayout.WEST); 
     // Filler component that avoids having the bottom panel too small 
     bottomPanel.add(new JComponent() { 
      @Override 
      public Dimension getPreferredSize() { 
       return new Dimension(centerPanel.getPreferredSize().width, 0); 
      } 
     }); 
     bottomPanel.add(legalNoticeButton, BorderLayout.EAST); 
     frame.add(centerPanel); 
     frame.add(bottomPanel, BorderLayout.SOUTH); 
     frame.pack(); 
     frame.setMinimumSize(frame.getPreferredSize()); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, 
      UnsupportedLookAndFeelException { 
     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new TestGridBagLayout().initUI(); 
      } 
     }); 
    } 
}