如何正确使用GridLayout来定位JFrame中的元素?

时间:2013-10-31 13:13:39

标签: java swing grid textarea grid-layout

这就是我希望我的JFrame看起来像:

________________________________________________________________________________
|Play                                                                     - 0 x |
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Score List:     | 
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Mario: 100      | 
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Luigi: 50       |        
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Waluigi: 20     | 
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Wario: 10       |       
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                 | 
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                 |       
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                 | 
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                 |       
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                 | 
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                 |       
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                 | 
|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                 |    
|Status: Ready!                                                       Score: 30 |
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾

目前,每个元素的大小相同,基本上只是将中间和上/下分开。 Play是Frame的标题,%s表示用户可以在其中玩游戏的JPanel。 'Status:Ready'是JLabel,'得分:30'也是如此。分数列表是JTextArea。

那么,我该如何设置这些元素的大小和位置?到目前为止我会给你我的代码,也许你可以告诉我哪里出错了。

this.add(playPanel);
this.add(scoreArea);
this.add(statusLabel);
this.add(scoreLabel);
this.setLayout(new GridLayout(2,2));

2 个答案:

答案 0 :(得分:3)

GridBagLayout应该是您的完美解决方案。它支持服从组件的首选大小和巧妙使用GridBagConstraints。使用GridBagLayout's GridBagConstraint属性:

  1. gridx, gridy :您的组件将被放置在grid(X, Y)中。在添加任何组件之前,使用这些属性指定网格,将添加到哪个组件。
  2. anchor FIRST_LINE_START, PAGE_START, FIRST_LINE_END, LINE_START, LINE_END等用于定位组件 每个网格的可用空间。
  3. weightx, weighty :对it is the center标签weightxweighty进行回复,并在width中重新调整容器大小和height。对于score list标签,仅指定weighty进行响应,仅在height中重新调整容器大小。
  4. gridwidth :对于it is the center标签,此属性已指定为2以获取两个网格。
  5. 填充:被指定为BOTHit is the center标签用于填充可用显示的宽度和高度。并且此属性设置为VERTICAL score list标签,以填充高度显示区域。
  6. 这是一个专为您提供的演示乍一看:

    没有真正编码为标准(,例如setPreferredSize()设置首选大小不应该),但我认为它符合演示目的。

    enter image description here

    源代码:

    import java.awt.*;
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.HeadlessException;
    import java.awt.Insets;
    import javax.swing.*;
    import javax.swing.SwingUtilities;
    
    /**
     *
     * @author Rashed
     */
     class GridBagLayoutDemo extends JFrame{
    
    
        public JLabel createLabel(String txt, int width, int height, Color color)
        {
            JLabel label = new JLabel(txt);
            label.setOpaque(true);
            label.setBackground(color);
            label.setPreferredSize(new Dimension(width, height));
            return label;
        }
    
        public GridBagLayoutDemo() throws HeadlessException {
           setSize(400,400);
           setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
           JPanel panel = new JPanel(new java.awt.GridBagLayout());
           GridBagConstraints labCnst = new GridBagConstraints();
    
           labCnst.fill = GridBagConstraints.NONE;
           labCnst.insets = new Insets(3, 3, 3, 3);
           labCnst.anchor = GridBagConstraints.FIRST_LINE_START;
           labCnst.gridx  = 0;
           labCnst.gridy = 0;
           panel.add(createLabel("play", 100, 30, new Color(0x359DBD)), labCnst);
    
          // labCnst.anchor = GridBagConstraints.LAST_LINE_START;
           labCnst.gridx  = 0;
           labCnst.gridy = 2;
           panel.add(createLabel("Status: Ready!", 100, 30, new Color(0x359DBD)), labCnst);
    
           labCnst.anchor = GridBagConstraints.FIRST_LINE_END;
           labCnst.gridx  = 2;
           labCnst.gridy = 0;
           panel.add(createLabel("-0x", 100, 30, new Color(0x359DBD)), labCnst);
    
           labCnst.anchor = GridBagConstraints.LAST_LINE_END;
           labCnst.gridx  = 2;
           labCnst.gridy = 2;
           panel.add(createLabel("score:30", 100, 30, new Color(0x359DBD)), labCnst);
    
    
           labCnst.anchor = GridBagConstraints.LINE_START;
           labCnst.fill = GridBagConstraints.VERTICAL;
           labCnst.gridx  = 2;
           labCnst.gridy = 1;
           labCnst.gridwidth = 1;
    
           labCnst.weightx = 0.7;
           labCnst.weighty = 0.7;
           panel.add(createLabel("ScoreList", 100, 200, new Color(0xFFAA00)), labCnst);
    
    
           labCnst.gridx = 0;
           labCnst.gridy = 1;
           //labCnst.anchor = GridBagConstraints.LIN;
           labCnst.gridwidth = 2;
    
           labCnst.weightx = 0.8;
           labCnst.weighty = 0.8;
           labCnst.fill = GridBagConstraints.BOTH;
           panel.add(createLabel("It is the center", 200, 200, new Color(0xFFD47E)), labCnst);
    
           //labCnst.anchor = GridBagConstraints.LINE_END;
    
    
           add(panel);
    
        }
    
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new GridBagLayoutDemo().setVisible(true);
                }
            });
        }
    }
    

答案 1 :(得分:1)

您可能需要查看Gridbaglayout。 它可能更适合您的需求。