java 改变 GridLayout 的宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10332608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing width of GridLayout
提问by user1170330
I use the GridLayout for my JPanel and was wondering, if it's possible to change the width of the columns:
我为我的 JPanel 使用了 GridLayout 并且想知道是否可以更改列的宽度:
GridLayout grid = new GridLayout(5,2);
panel.setLayout(grid);
Is there a simple way?
有没有简单的方法?
采纳答案by Tasawer Khan
You can use separate grid layout for each column.
您可以为每列使用单独的网格布局。
GridLayout grid = new GridLayout(1,2);
GridLayout grid1 = new GridLayout(5,1);
GridLayout grid2 = new GridLayout(5,1);
grid.add(grid1);
grid.add(grid2);
You can now set widths of grid1 one and grid2.
您现在可以设置 grid1 one 和 grid2 的宽度。
回答by Mayank Pandya
Check the GridLayout api to see that the grid is made so all cells are the same size. If you want a component to be shown at it's preferred size, like (15,15), add it to a JPanel and add the JPanel to the GridLayout. The JPanel will expand to fill the gird cell and allow the component to be shown at it's preferred size.
检查 GridLayout api 以查看网格是否已制作,因此所有单元格的大小都相同。如果您希望组件以其首选大小显示,例如 (15,15),请将其添加到 JPanel 并将 JPanel 添加到 GridLayout。JPanel 将扩展以填充网格单元并允许组件以其首选大小显示。
For a row of different size you'll have to try something else. Nested layouts would be a good starting place...
对于不同大小的行,您必须尝试其他方法。嵌套布局将是一个很好的起点......
You could always use the dreaded GridBagLayout.
你总是可以使用可怕的 GridBagLayout。