简体   繁体   English

Java GridLayout如何动态更改网格布局

[英]Java GridLayout how to dynamically change grid layout

I am trying to create a character sheet program that arranges 3 panels based on how wide the frame is. 我正在尝试创建一个字符表程序,该程序根据框架的宽度安排3个面板。 If the frame is not very wide, I want the panels to be arranged vertically, but if the user chooses to make the frame wider, I would like to arrange the panels horizontally. 如果框架不是很宽,我希望将面板垂直放置,但是如果用户选择使框架更宽,我想将面板水平放置。

I also have a scrollPanel that the 3 panels are arranged on, so the scrollPanel is being added to a scrollPane. 我还具有一个安排了3个面板的scrollPanel,因此将scrollPanel添加到了scrollPane中。

I have seen posts that say that an eventlistener would work, however most of these are for buttons, and I need the Frame size to pass a certain threshold before the layout changes. 我看到过一些帖子说事件监听器可以工作,但是其中大多数都是用于按钮的,在布局更改之前,我需要框架大小通过一定的阈值。 I seen other posts that recommend html which I don't see as necessary right now. 我看到了其他推荐html的帖子,但我认为现在没有必要。

在此处输入图片说明

So in this picture the 3 panels are arranged vertically, and are able to be scrolled through. 因此,在此图片中,这3个面板垂直排列,并且可以滚动浏览。

    scrollPanel.add(leftPanel); //this is the yellow panel that is being added
    scrollPanel.add(centerPanel); //this one is farther down
    scrollPanel.add(rightPanel); //even farther down
    scrollPanel.setBackground(Color.CYAN);
    scrollPanel.setLayout(new GridLayout(0, 1)); //so here I would like to have an if statement or event swap the 0 and the 1.

Summary: 摘要:

  • Can I change a GridLayout dynamically based on the size of a Frame? 我可以根据框架的大小动态更改GridLayout吗?

  • How do I monitor for a Frame size change? 如何监控镜架尺寸的变化? event? 事件? if statement? 如果声明?

  • Can I do the same with a ScrollPane with setting the horizontal and vertical scroll bars to never appear based on if the panels are vertical or horizontal? 我可以使用ScrollPane进行相同的操作,将水平和垂直滚动条设置为从不显示为垂直还是水平,而不显示面板?

You can change the GridLayout as follows: 您可以如下更改GridLayout:

gr.setRows(newR);
gr.setColumns(newC);
myFrame.validate();

where gr is the layout you created somewhere and newR/newC are the new numbers of rows/columns. 其中gr是您在某处创建的布局,而newR / newC是新的行数/列数。

Or you can say 或者你可以说

gr=new GridLayout(newR, newC);
myFrame.setLayout(gr);

Then 然后

if(myFrame.getWidth()>thershold) {

.. do the above

}

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

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