简体   繁体   English

如果BorderLayout约束是CENTER,JScrollPane不显示?

[英]JScrollPane doesn't show if BorderLayout constraint is CENTER?

Been developing a game for a while, and currently re working the GUI, or at least trying to. 正在开发游戏一段时间,目前正在重新使用GUI,或者至少尝试过。 Had the massive problem of not being able to resize the frame (without issues), as I didn't understand layout managers very well. 有一个无法调整框架大小的问题(没有问题),因为我不太了解布局管理器。 A few projects later, and time to come back and do some more on the game, and I hit a problem... 之后有几个项目,还有时间回来再做一些游戏,我遇到了问题...

The basic layout of the main frame is, mainPane, containing one gameScrollPane and one controlPanel. 主框架的基本布局是mainPane,包含一个gameScrollPane和一个controlPanel。 The scroll pane is a scroll pane, and the control panel a normal panel. 滚动窗格是滚动窗格,控制面板是普通面板。 The scroll pane contains the main game panel. 滚动窗格包含主游戏面板。

As I wanted the scroll pane to take up most of the screen, with the control panel taking up a small lower area, much the same as many Sim like games, so chose the Border layout for the mainPane. 因为我希望滚动窗格占据屏幕的大部分,控制面板占用较小的较小区域,与许多类似Sim的游戏大致相同,因此选择了mainPane的Border布局。 I added the scroll pane and set the constraints CENTER and the control panel added and constriants SOUTH. 我添加了滚动窗格并设置了约束CENTER和添加的控制面板以及constriants SOUTH。 This didn't show the scroll pane, so I played around trying different constraints, and it seems that only when I set the scroll pane constraint to North, does it display at all. 这没有显示滚动窗格,所以我玩了尝试不同的约束,似乎只有当我将滚动窗格约束设置为North时,它才会显示。

To demonstrate this, I have created a quick video... http://screenjel.ly/q5RjczwZjH8 为了证明这一点,我创建了一个快速视频... http://screenjel.ly/q5RjczwZjH8

As you can see, when I change the value of NORTH to CENTER and re run, it's like its not there! 正如你所看到的,当我将NORTH的值更改为CENTER并重新运行时,它就像它不在那里!

Bonus points for anyone who can see a clear second problem which I may start another question for after this issue is solved! 对于能够看到明确的第二个问题的人,我可以在此问题解决后开始另一个问题获得奖励积分!

I thank you for your time to read this. 感谢您抽出时间阅读本文。

Thanks in advance for any ideas or thoughts :) 提前感谢任何想法或想法:)

Rel 相对

If you'd posted some code to start with then you might have gotten a really quick answer. 如果您已经发布了一些代码,那么您可能会得到一个非常快速的答案。 Luckily, you posted a link in the comments to the other response. 幸运的是,您在评论中发布了其他回复的链接。

The setContentPane() stuff is weird, especially after doing some things to it that will then get wiped out. setContentPane()的东西很奇怪,特别是在对它做了一些事情后会被消灭掉。 However, that's not your problem. 但是,这不是你的问题。

The issue is that you are adding levelMaker and personMover right to mainPane without any constraints. 问题是您正在将levelMaker和personMover添加到mainPane,没有任何约束。 These will then be blowing away anything you set for CENTER... in this case the previously set gameScrollPane. 这些将会吹掉您为CENTER设置的任何东西......在这种情况下,先前设置的gameScrollPane。

That's why you see it for NORTH and not for CENTER. 这就是为什么你看到北方而不是中心的原因。

I can't get the video to show. 我无法播放视频。 It's been buffering for ages. 它已经缓冲了很多年。

My guess would be that the scrollpane is in fact filling the center; 我的猜测是,滚动板实际上正在填充中心; it's just your game panel that's not being shown. 它只是你的游戏面板没有显示。

Your game panel needs to return reasonable values for getPreferredSize() . 您的游戏面板需要为getPreferredSize()返回合理的值。

Update 更新

Another thing you may want to do is have your game panel implement the Scrollable interface. 您可能想要做的另一件事是让您的游戏面板实现Scrollable界面。 You can then override getScrollableTracksViewportWidth and ...height to return true so your panel will be forced to the scrollpane's dimensions. 然后,您可以覆盖getScrollableTracksViewportWidth...height以返回true这样您的面板将被强制转换为scrollpane的尺寸。

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

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