当前位置: 首页 > 知识库问答 >
问题:

使用GridLayout来创建一个智能布局,以响应正在调整大小的窗口?

沙海
2023-03-14

共有1个答案

岳迪
2023-03-14

我创建了一个示例Vaadin7 web应用程序,展示了GridLayout的两种用法。这两种用法都在布局的四个角中放置一个和虚拟数据。当窗口的大小调整为较大或较小时,四个表相应地改变大小。

一种用法是由四个单元格、两列和两行组成的简单网格。另一种用法包括在中间一列和中间一行中有按钮的嵌套布局,在GridLayout中有三列和三行,总共有九个单元格,其中三个单元格为空。

每种用法的屏幕截图…

/**
 * 
 */
package com.example.quadrantgridlayout;

import com.vaadin.server.Sizeable;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Table;

/**
 * An example use of GridLayout in Vaadin 7.1.
 * 
 * Each quadrant of the layout contains a table. Each table resizes in both width and height to fill any available space.
 * 
 * @author Basil Bourque
 * 
 *         Copyright © 2013 Basil Bourque.
 * 
 *         This example source code may be used freely forever by anyone taking full responsibility for doing so.
 * 
 */
public class QuadrantGridLayout extends GridLayout {

    /**
     * Constructor
     */
    public QuadrantGridLayout() {
        super();
        this.setMargin( true ); // Add space around the perimeter.
        this.setSpacing( true ); // Add space between widgets.

        // Make this Layout fill all available space in its container.
        // In this case its container is a UI.
        // In this case, that UI happens to fill its container, the web browser's tab/window.
        this.setWidth( 100, Sizeable.Unit.PERCENTAGE );
        this.setHeight( 100, Sizeable.Unit.PERCENTAGE );

        // Create four cells for our four tables.
        this.setColumns( 2 );
        this.setRows( 2 );

        // Create widgets.
        Table upperLeft = new AstronomersTable( "Upper Left " + new java.util.Date() ); // In real work I would use Joda Time, not j.u.Date.
        Table upperRight = new AstronomersTable( "Upper Right" );

        Table lowerLeft = new AstronomersTable( "Lower Left" );
        Table lowerRight = new AstronomersTable( "Lower Right" );

        // Compose layout.
        upperLeft.setWidth( 100, Sizeable.Unit.PERCENTAGE );
        upperLeft.setHeight( 100, Sizeable.Unit.PERCENTAGE );
        this.addComponent( upperLeft );

        upperRight.setSizeFull(); // Alternate syntax for setting both width and height to 100%, instead of two lines seen earlier above.
        this.addComponent( upperRight );

        // Cursor automatically moved to next row upon reaching the row's last cell.

        lowerLeft.setSizeFull();
        // lowerLeft.setHeight( 72 * 2, Sizeable.Unit.POINTS ); // Use this if you want to play with fixed sizing. 72 points per inch.
        this.addComponent( lowerLeft );

        lowerRight.setSizeFull();
        this.addComponent( lowerRight );

    }

}
/**
 * 
 */
package com.example.quadrantgridlayout;

import com.vaadin.server.Sizeable;
import com.vaadin.ui.Button;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;

/**
 * An example use of GridLayout in Vaadin 7.1.
 * 
 * Each quadrant of the layout contains a table. Each table resizes in both width and height to fill any available space.
 * 
 * @author Basil Bourque
 * 
 *         Copyright © 2013 Basil Bourque.
 * 
 *         This example source code may be used freely forever by anyone taking full responsibility for doing so.
 */
public class QuadrantWithButtonsGridLayout extends GridLayout {

    /**
     * Constructor
     */
    public QuadrantWithButtonsGridLayout() {
        super();
        this.setMargin( true ); // Add space around the perimeter.
        this.setSpacing( true ); // Add space between widgets.

        // Make this Layout fill all available space in its container.
        // In this case its container is a UI.
        // In this case, that UI happens to fill its container, the web browser's tab/window.
        this.setWidth( 100, Sizeable.Unit.PERCENTAGE );
        this.setHeight( 100, Sizeable.Unit.PERCENTAGE );

        // Create 9 cells, like Tic-Tac-Toe. A table goes in each corner.
        this.setColumns( 3 );
        this.setRows( 3 );

        // Create tables.
        Table upperLeft = new AstronomersTable( "Upper Left " + new java.util.Date() ); // In real work I would use Joda Time, not j.u.Date.
        Table upperRight = new AstronomersTable( "Upper Right" );

        Table lowerLeft = new AstronomersTable( "Lower Left" );
        Table lowerRight = new AstronomersTable( "Lower Right" );

        // Create buttons, and collect into a Layout.
        Button alphaButton = new Button( "Alpha" );
        Button betaButton = new Button( "Beta" );
        VerticalLayout upperButtonsLayout = new VerticalLayout();
        upperButtonsLayout.setCaption( " " ); // Add an empty caption (Space character, actually) to force the buttons downwards to line up with tables.
        upperButtonsLayout.setSpacing( true ); // Add space between widgets.
        upperButtonsLayout.addComponent( alphaButton );
        upperButtonsLayout.addComponent( betaButton );

        Button gammaButton = new Button( "Gamma" );
        Button deltaButton = new Button( "Delta" );
        HorizontalLayout leftButtonsLayout = new HorizontalLayout();
        leftButtonsLayout.setSpacing( true );
        leftButtonsLayout.addComponent( gammaButton );
        leftButtonsLayout.addComponent( deltaButton );

        // Compose layout.

        // ----| ROW 1 |------------------------
        // Vaadin 6 & 7 seem to suffer a bug that makes one row wider than another despite being assigned the same ratio.
        // As a workaround, divide the wide column's ratio by half (give or take) to compensate.
        this.setRowExpandRatio( this.getCursorY(), 0.5F / 1.5F );
        // Column 1
        // Vaadin 6 & 7 seem to suffer a bug that makes one column wider than another despite being assigned the same ratio.
        // As a workaround, divide the wide column's ratio by half (or more) to compensate.
        this.setColumnExpandRatio( this.getCursorX(), 1F / 1.5F ); // Notice first argument is soft-coding the column position. Also, index (zero-based) counting .
        upperLeft.setWidth( 100, Sizeable.Unit.PERCENTAGE );
        upperLeft.setHeight( 100, Sizeable.Unit.PERCENTAGE );
        // upperLeft.setHeight( 72 * 2, Sizeable.Unit.POINTS ); // Use this if you want to play with fixed sizing. 72 points per inch.
        this.addComponent( upperLeft );

        // Column 2
        // should not expand or contract with window re-sizing. So set expansion ratio to zero.
        upperButtonsLayout.setSizeUndefined(); // Setting size to be "undefined" is the trick to getting this column to collapse to its own minimal size.
        this.addComponent( upperButtonsLayout );

        // Column 3
        this.setColumnExpandRatio( this.getCursorX(), 1F );
        upperRight.setSizeFull(); // Alternate syntax for setting both width and height to 100%, instead of two lines seen earlier above.
        this.addComponent( upperRight );
        // Cursor automatically moved to next row upon reaching the row's last cell.

        // ----| ROW 2 |------------------------
        // Column 1
        leftButtonsLayout.setSizeUndefined(); // Setting size to be "undefined" is the trick to getting this row to collapse to its own minimal size.
        this.addComponent( leftButtonsLayout );
        this.newLine(); // Move cursor to next row. We have nothing to place in the remaining two cells.

        // ----| ROW 3 |------------------------
        this.setRowExpandRatio( this.getCursorY(), 0.5F );
        // // Column 1
        lowerLeft.setSizeFull();
        this.addComponent( lowerLeft );

        // Column 2
        this.space(); // Move cursor to next cell. We have nothing to place in this middle cell.

        // Column 3
        lowerRight.setSizeFull();
        this.addComponent( lowerRight );

    }

}
package com.example.quadrantgridlayout;

import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.Layout;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.UI;

/**
 * 
 * Main UI class
 * 
 * This example app demonstrates the use of GridLayout to build a smart resizing layout.
 * 
 * Built in Vaadin 7.1 (pre-release), but the GridLayout & Table portions should work in Vaadin 6 as well.
 * 
 * * @author Basil Bourque
 * 
 * Copyright © 2013 Basil Bourque.
 * 
 * This example source code may be used freely forever by anyone taking full responsibility for doing so.
 */
public class QuadrantGridLayoutUI extends UI {

    @Override
    protected void init( VaadinRequest request ) {
        // This app demonstrates two versions of a GridLayou made of quadrants, each containing a table.
        // The simpler version contains only the four tables.
        // The other version includes a middle column and row, each containing a Layout with buttons.
        final Layout layoutQuadrant = new QuadrantGridLayout();
        final Layout layoutQuadrantWithButtons = new QuadrantWithButtonsGridLayout();

        // Display content on screen.

        // If for simplicity you want to remove the TabSheet from the situation, swap this next line for all remaining code below.
        // this.setContent( layoutQuadrant ); // Copy-paste either Layout variable to try each version.

        TabSheet tabs = new TabSheet();
        tabs.setSizeFull(); // Make the TabSheet fill all available space. By default the height is fixed.
        tabs.addTab( layoutQuadrant, "Simple" );
        tabs.addTab( layoutQuadrantWithButtons, "With Buttons" );
        this.setContent( tabs );
    }

}
/**
 * 
 */
package com.example.quadrantgridlayout;

import com.vaadin.ui.Table;

/**
 * 
 * Creates a simple Vaadin table with some dummy data.
 * 
 * @author Basil Bourque
 * 
 *         Copyright © 2013 Basil Bourque, except where noted below.
 * 
 *         This source code may be used freely forever by anyone taking full responsibility for doing so.
 * 
 */
public class AstronomersTable extends Table {

    /**
     * 
     */
    public AstronomersTable() {
        super();
        this.configure();
    }

    /**
     * @param caption
     */
    public AstronomersTable( String caption ) {
        super( caption );
        this.configure();
    }

    private void configure() {
        // This method's source code taken from "The Book of Vaadin 7", plus I added an earlier astronomer.
        // https://vaadin.com/book/vaadin7/-/page/components.table.html

        // Configure options.
        this.setSelectable( true );

        /*
         * Define the names and data types of columns. The "default value" parameter is meaningless here.
         */
        this.addContainerProperty( "First Name", String.class, null );
        this.addContainerProperty( "Last Name", String.class, null );
        this.addContainerProperty( "Year", Integer.class, null );

        /* Add a few items in the this. */
        this.addItem( new Object[] { "Hypatia", "of Alexandria", new Integer( -370 ) }, new Integer( 1 ) );
        this.addItem( new Object[] { "Nicolaus", "Copernicus", new Integer( 1473 ) }, new Integer( 2 ) );
        this.addItem( new Object[] { "Tycho", "Brahe", new Integer( 1546 ) }, new Integer( 3 ) );
        this.addItem( new Object[] { "Giordano", "Bruno", new Integer( 1548 ) }, new Integer( 4 ) );
        this.addItem( new Object[] { "Galileo", "Galilei", new Integer( 1564 ) }, new Integer( 5 ) );
        this.addItem( new Object[] { "Johannes", "Kepler", new Integer( 1571 ) }, new Integer( 6 ) );
        this.addItem( new Object[] { "Isaac", "Newton", new Integer( 1643 ) }, new Integer( 7 ) );

    }

}
 类似资料:
  • 我真的开始使用LWJGL(我刚刚开始),我专注于一件事:当我创建一个窗口并且我想将其设置为不可调整大小时,我使用: 然而,即使我想在创建窗口后设置它,我也不知道如何设置。我只是尝试在创建窗口后放置命令,但它不起作用: 那么,我该如何解决这个问题呢?

  • 我的场景中有一个对象。我希望能够调整窗口的大小,并与它一起调整窗格的大小。我还想在窗格旁边放置一个。 到目前为止,我已经成功地生成了一个完全可调整大小的或一个,其中包含和对象,但当我调整窗口大小时,窗格不会随之调整大小。 以下是带有锚烷的FXML: 只有的版本基本上就是版本中使用的代码,但具有和属性。 如何制作一个场景,使其具有可调整大小的和?

  • 问题内容: 如何自动缩放HTML5 元素以适合页面? 例如,我可以通过将和属性设置为100%来缩放,但是不会缩放吗? 问题答案: 我相信我已经找到了一个优雅的解决方案: JavaScript CSS 到目前为止,对我没有太大的负面性能影响。

  • 我正在为自己的一个项目一个游戏叫集合。它是一个与不同的牌相匹配的游戏,每一张牌都有形状、颜色或形状的数量。我试图创建一个网格窗格,它可以灵活地容纳一个形状,或者最多容纳3个形状。我创建了一个3×3的gridpane,并且已经创建了数组来选择随机的形状、颜色或数字。然而,我一直在苦苦思索如何创建一个gridpane,其中每个网格可以灵活地容纳形状的数量。这是我当前的代码。方法setCards扩展了B

  • 我正在尝试构建一个包含6个窗格(作为父级添加到GridPane布局中)的简单Java项目。我必须在开始时设置窗口大小,并通过参考根布局的宽度和高度,设法将它们均匀地拆分。 但我想要他们调整大小,因为我改变了窗口的大小(使用鼠标,现在他们得到固定的大小)。 下面是我的代码:

  • 我试图创建一个简单的页面示例,顶部有一个菜单栏,底部有一个BorderLayoutContainer。我不知道如何让底部自动适应剩余的尺寸。 我有这样的想法: 菜单栏显示正确,但底部(BorderLayoutContainer)被完全挤压。如果我只是将BorderLayoutContainer添加到视口中,它会正确显示,即填充整个屏幕。 为什么大小没有从NorthSouthContainer的南部