JSplitPane 和 Canvas
作为我正在编写的应用程序的一部分,我需要将旧的(重量级)Canvas 与 swing 组件混合在一起 - 特别是将它们嵌套在 JSplitPane 中。然而,当我这样做时,分隔线拒绝在任何地方调整大小,就好像两个画布都不会接受尺寸的减小一样。演示这个问题的代码是这样的:
JFrame frame = new JFrame();
JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new Canvas(), new Canvas());
pane.setResizeWeight(0.5);
frame.add(pane);
frame.pack();
frame.setVisible(true);
我最初认为这是一个很容易解决的问题,但是在做了相当多的研究之后,选项似乎呈现为:
- 使用 JPanel 代替(并不总是像我的情况那样的选项)正在使用 vlcj 直接渲染到画布上)
- 插入 x 狡猾的黑客,在某些情况下可能有效
有没有更好的方法来做到这一点?或者这真的只是求助于黑客的情况吗?我已经讨论过其他选项,例如是否可以使用替代的 SplitPane 实现,但是没有重量级的实现,如果轻量级的实现可以避免这个问题,我会感到惊讶。
As part of an application I'm writing I need to mix the old (heavyweight) Canvas with swing components - specifically nesting them inside a JSplitPane. However, when I do this the divider refuses to resize anywhere as though neither canvas will accept a reduction in size. The code demonstrating the issue is thus:
JFrame frame = new JFrame();
JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new Canvas(), new Canvas());
pane.setResizeWeight(0.5);
frame.add(pane);
frame.pack();
frame.setVisible(true);
I initially assumed this was a simple thing to solve, however after doing a fair bit of research the options seem to present themselves as:
- Use a JPanel instead (not always an option as in my case where I'm using vlcj to render directly onto a canvas)
- Insert x dodgy hack that might work in some cases
Is there a better way of doing this? Or is it really just a case of resorting to hacks? I've debated other options such as if an alternative SplitPane implementation might be available that works, but there's no heavyweight implementation and I'd be surprised if a lightweight one avoided the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JSplitPane 使用组件的“最小大小”来确定组件在使用分隔符时是否可以缩小。
我以前从未使用过 Canvas,但看起来最小尺寸始终默认为首选尺寸。
重写 Canvas 的 getMinimumSize(...) 大小方法以返回合理的最小值。
对于快速文本,您可以使用:
并且您将能够向左移动分隔线,但永远不能向右移动。
A JSplitPane uses the "minimum size" of the component to determine if the component can shrink when using the divider.
I've never used a Canvas before, but itt appears that the minimum size always defaults to the preferred size.
Override the getMinimumSize(...) size method of the Canvas to return a reasonable minimum.
For a quick text you can use:
and you will be able to move the divider left, but never back to the right.