GridBagLayout固定空间按权重x/y设置

编程入门 行业动态 更新时间:2024-10-12 12:33:03
本文介绍了GridBagLayout固定空间按权重x/y设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个具有以下3个JPanel布局的框架.

I have a frame with the following layout of 3 JPanels.

---------------------------- | | | | l | | | e | //toppanel | | f | | | t |----------------------| | p | | | n | //bottompanel | | l | | | | | ----------------------------

我通过使用GridBagLayout

这是该布局的代码

public class UITests extends JFrame{ public UITests(){ setLayout(new GridBagLayout()); //the three main panels JPanel leftPanel = new JPanel(); JPanel topPanel = new JPanel(); JPanel bottomPanel = new JPanel(); leftPanel.setBackground(Color.YELLOW); topPanel.setBackground(Color.GREEN); bottomPanel.setBackground(Color.PINK); //position the three main panels GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.gridheight = 2; gbc.gridwidth = 1; gbc.fill = gbc.BOTH; gbc.weightx = .2; gbc.weighty = 1; getContentPane().add(leftPanel, gbc); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 0; gbc.gridheight = 1; gbc.gridwidth = 1; gbc.fill = gbc.BOTH; gbc.weightx = .8; gbc.weighty = .5; getContentPane().add(topPanel, gbc); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 1; gbc.gridheight = 1; gbc.gridwidth = 1; gbc.fill = gbc.BOTH; gbc.weightx = .8; gbc.weighty = .5; getContentPane().add(bottomPanel, gbc); setSize(600,600); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args){ new UITests(); } }

问题是,当我在顶部面板中添加JTable时,底部面板的高度会缩小,就像其某些高度被顶部面板占据一样.这是顶部面板中表格的代码,是在设置了三个主面板的背景之后添加的.

Problem is, when I add a JTable in the top panel, the bottom panel's height is shrinked, like some of its height is taken by the top panel. Here is the code for the table in the top panel , added after setting the background of three main panels.

Object[][] contents = {{"haha","hehe", "hihi", "hoho", "huhu"}, {"haha","hehe", "hihi", "hoho", "huhu"}, {"haha","hehe", "hihi", "hoho", "huhu"}, {"haha","hehe", "hihi", "hoho", "huhu"}}; Object[] heads = {"Haha Head", "Hehe Head", "Hihi Head", "Hoho Head", "Huhu Head"}; JTable table = new JTable(contents, heads); JScrollPane scrollTable = new JScrollPane(table); //add components to topPanel GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.gridheight = 1; gbc.gridwidth = 1; gbc.weightx = 1; gbc.weighty = 1; gbc.fill = gbc.BOTH; topPanel.setLayout(new GridBagLayout()); topPanel.add(scrollTable, gbc);

框架现在看起来像

---------------------------- | | | | l | | | e | //toppanel | | f | //new table created | | t | | | p | | | n | | | l |----------------------| | | //bottompanel | ----------------------------

那么,如何固定'weightx'或'weighty'创建的空间?

So, how can I make the space created by 'weightx' or 'weighty' fixed?

推荐答案

weightx/weighty用于分配额外的空间.因此,实际上,容器会询问孩子自己的首选尺寸,如果尺寸小于可用尺寸,则多余的像素会根据权重进行分配.

weightx/weighty are used to distribute extra space. So in fact container asks children for their preferred size and if it's smaller than available the extra pixels are distributed according to the weights.

因此您可以定义容纳JTable

更多推荐

GridBagLayout固定空间按权重x/y设置

本文发布于:2023-11-22 01:45:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615493.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:权重   空间   GridBagLayout

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!