簡體   English   中英

如何正確使用GridLayout在JFrame中定位元素?

[英]How to properly use GridLayout to position elements in a JFrame?

這就是我希望JFrame看起來像的樣子:

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

目前,每個元素的大小均相等,基本上只是將中間部分和頂部/底部分開。 Play是Frame的標題,%s表示用戶可以在其中玩游戲的JPanel。 “狀態:就緒”是JLabel,“得分:30”也是。 分數列表是一個JTextArea。

那么,如何設置這些元素的大小和位置? 到目前為止,我將為您提供我的代碼,也許您可​​以告訴我我要去哪里了。

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

GridBagLayout應該是您的理想解決方案。 它支持遵循組件的首選大小以及GridBagConstraints巧妙使用。 使用GridBagLayout's GridBagConstraint屬性:

  1. gridx, gridy :您的組件將放置在grid(X, Y) 在添加任何組件之前,請使用這些屬性指定網格,然后將網格添加到其中。
  2. anchorFIRST_LINE_START, PAGE_START, FIRST_LINE_END, LINE_START, LINE_END等用於在每個網格的可用空間中定向組件。
  3. weightx, weighty :因為it is the center標簽,所以weightxweighty被指定為響應容器的widthheight調整大小。 對於score list標簽,僅指定weighty以僅在height調整容器大小。
  4. gridwidth :因為it is the center標簽,所以將此屬性指定為2以采用兩個網格。
  5. fill :指定為BOTHit is the center填充可用顯示it is the center標簽的寬度和高度。 並將此屬性設置為“ VERTICAL以使score list標簽填充高度的顯示區域。

這是為您構建的演示,可讓您乍一看:

沒有真正編碼為標准( 例如,不應該通過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);
            }
        });
    }
}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM