2

这就是我希望我的 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,'Score: 30' 也是如此。分数列表是一个 JTextArea。

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

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

2 回答 2

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 centerlabelweightx和被指定以在和weighty中重新调整容器大小来响应。对于标签,仅指定以仅在中重新调整容器大小来响应。widthheightscore listweightyheight
  4. gridwidth:对于it is the center标签,此属性被指定为2采用两个网格。
  5. 填充:指定为BOTH标签it is the center以填充可用显示的宽度和高度。并且此属性设置VERTICALscore list标签以填充显示区域的高度。

这是一个为您构建的 Demo,让您一目了然:

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

在此处输入图像描述

源代码:

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);
            }
        });
    }
}
于 2013-10-31T14:23:13.367 回答
1

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

于 2013-10-31T13:19:05.920 回答