简体   繁体   English

GridLayout 大小不会填满所有空间

[英]GridLayout size doesn't fill all space

I need your help for a project that I need to finish tomorrow.我需要你的帮助来完成我明天需要完成的项目。 I'm trying to implement a tab which will put some value from database.我正在尝试实现一个选项卡,它将从数据库中放入一些值。 But I'm facing with an issue that I can't resolve.但我面临一个我无法解决的问题。 I want to add several layout one by one.我想一一添加几个布局。 Each layout is splitted into 3 columns but these layout (and columns) doesn't take all available space as you can see on this screen:每个布局都分为 3 列,但这些布局(和列)不会占用您在此屏幕上看到的所有可用空间:

在此处输入图像描述

Here's my code:这是我的代码:

package pizzeria;

import java.sql.SQLException;

import javax.swing.*;

import java.awt.*;

public class CarteDesPizzas extends JPanel{

    static GridLayout grid1 = new GridLayout(1,1);

    static GridLayout grid2 = new GridLayout(1,3);

    static GridLayout grid3 = new GridLayout(1,1);

    public static JPanel affichageCarte() throws SQLException ,Exception {
        JLabel label = new JLabel("Carte des pizzas", JLabel.CENTER);
        label.setForeground(new Color(120, 90, 40));

        JPanel selection = new JPanel();
        GridBagLayout grid = new GridBagLayout();
        selection.setLayout(grid);
         
        JPanel header;
        JPanel content;
         
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1; 
        c.weighty = 0.1;
        c.gridy = 0;
         
        header = new JPanel(new GridBagLayout());
        header.add(label);
        selection.add(header,c);
         
        content = new JPanel();
        c.weightx = 1; 
        c.weighty = 0.9;
        c.gridy = 1;



        JPanel selection2 = new JPanel(grid2);

        JPanel pinkSelection = new JPanel();
        pinkSelection.setBackground(Color.PINK);

        JPanel redSelection = new JPanel();
        redSelection.setBackground(Color.RED);

        JPanel greenSelection = new JPanel();
        greenSelection.setBackground(Color.GREEN);

        selection2.add(pinkSelection);
        selection2.add(redSelection);
        selection2.add(greenSelection);

        content.add(selection2);

        selection.add(content,c);
        
        return selection;
    }
}

Thank you for your help谢谢您的帮助

A JPanel is a container. JPanel是一个容器。 It's purpose is to contain other Component s.它的目的是包含其他Component

GridBagLayout places components in rectangles (cells) in a grid, and then uses the components' preferred sizes to determine how big the cells should be. GridBagLayout将组件放置在网格中的矩形(单元格)中,然后使用组件的首选大小来确定单元格的大小。

A JPanel , by default, has no preferred size.默认情况下, JPanel没有首选大小。 Its size is determined by the sizes of the components it contains.它的大小取决于它所包含的组件的大小。 Since the JPanel s in your code contain no components, their preferred size is very small.由于代码中的JPanel不包含任何组件,因此它们的首选大小非常小。 Hence they appear very small in your GUI.因此它们在您的 GUI 中显得非常小。

The simplest solution would be to explicitly assign a preferred size to the JPanel .最简单的解决方案是为JPanel显式分配首选大小。

In the below code I only set the preferred size for pinkSelection .在下面的代码中,我只设置了pinkSelection的首选大小。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class CarteDesPizzas extends JPanel {
    static GridLayout grid1 = new GridLayout(1,1);
    static GridLayout grid2 = new GridLayout(1,3);
    static GridLayout grid3 = new GridLayout(1,1);

    public static JPanel affichageCarte() {
        JLabel label = new JLabel("Carte des pizzas", JLabel.CENTER);
        label.setForeground(new Color(120, 90, 40));

        JPanel selection = new JPanel();
        GridBagLayout grid = new GridBagLayout();
        selection.setLayout(grid);
         
        JPanel header;
        JPanel content;
         
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1; 
        c.weighty = 0.1;
        c.gridy = 0;
         
        header = new JPanel(new GridBagLayout());
        header.add(label);
        selection.add(header,c);
         
        content = new JPanel();
        c.weightx = 1; 
        c.weighty = 0.9;
        c.gridy = 1;

        JPanel selection2 = new JPanel(grid2);

        JPanel pinkSelection = new JPanel();
        pinkSelection.setPreferredSize(new Dimension(100, 60));
        pinkSelection.setBackground(Color.PINK);

        JPanel redSelection = new JPanel();
        redSelection.setBackground(Color.RED);

        JPanel greenSelection = new JPanel();
        greenSelection.setBackground(Color.GREEN);

        selection2.add(pinkSelection);
        selection2.add(redSelection);
        selection2.add(greenSelection);

        content.add(selection2);

        selection.add(content,c);
        
        return selection;
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(affichageCarte());
                frame.pack();
                frame.setLocationByPlatform(true);
                frame.setVisible(true);
            }
        });
    }
}

Here is a screen capture:这是一个屏幕截图:

屏幕截图

You are not using GridLayout although you declared such three variables.尽管您声明了这三个变量,但您没有使用 GridLayout。 You are using GridBagLayout with some specific values that lead to what you get.您正在使用带有一些特定值的 GridBagLayout,这些值会导致您得到什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM