我正在尝试创建一个字符表程序,根据框架的宽度排列 3 个面板。如果框架不是很宽,我希望面板垂直排列,但如果用户选择使框架更宽,我希望水平排列面板。
我还有一个scrollPanel,上面排列了3个面板,因此scrollPanel被添加到scrollPane中。
我看过一些帖子说事件监听器可以工作,但是其中大多数都是用于按钮的,并且我需要框架大小在布局更改之前通过某个阈值。我看到其他推荐 html 的帖子,但我现在认为没有必要。
因此,在这张图片中,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.
摘要:
我可以根据 Frame 的大小动态更改 GridLayout 吗?
如何监控帧大小变化?事件? if 语句?
我可以对 ScrollPane 执行相同的操作,根据面板是垂直还是水平设置水平和垂直滚动条永远不会出现吗?
最佳答案
您可以按如下方式更改 GridLayout:
gr.setRows(newR);
gr.setColumns(newC);
myFrame.validate();
其中 gr 是您在某处创建的布局,newR/newC 是新的行/列数。
或者你可以说
gr=new GridLayout(newR, newC);
myFrame.setLayout(gr);
然后
if(myFrame.getWidth()>thershold) {
.. do the above
}
关于Java GridLayout 如何动态改变网格布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36048745/