JPanels 除了第一个未绘制
以下代码假设读取包含一组分子结构的文件,然后添加一堆 JPanel(等于分子数量)并在每个面板上创建一个分子。我在运行时得到了正确的面板数量。然而,第一个面板上只绘制了第一个分子?
The following code supposed to read a file containing a set of molecular structures, then add a bunch of JPanels (equal to the number of molecules) and create a molecule on each panel. I get the right no of panels at the runtime. However only the first molecule is drawn on the first panel?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
drawMolViewPanel()
函数似乎有点过度设计。首先,panes
列表似乎主要是临时的(您向其中添加对象,然后从该列表中将它们添加到MolTable
自己的面板集合中;我不知道我认为你不需要它)。如果我正确理解了这个函数,这会做同样的事情,并且对我来说更有意义:不幸的是,我不完全确定这是你的问题。
The
drawMolViewPanel()
function seems a little overengineered. For one, thepanes
list seems to be mostly temporary (you add objects to it, then from that list, add them to theMolTable
's own panel collection; I don't think you need it). If I understand the function correctly, this does the same thing and makes more sense to me:I'm not entirely sure that's your issue, unfortunately.
ReadSDF 工作正常吗?如果不了解更多信息,片段可能没有正确初始化,因此当您访问元素时,它无法正常工作,会引发异常,该异常会被更高层的东西捕获并忽略。
Cory Larson 的代码似乎对我有用。逻辑上是一样的。
我注意到在 MolViewer 中重写“paintComponent”的地方,您调用 super.paintComponents(g) (Container 的方法)而不是 PaintComponent (JComponent 的方法)。我对 Swing 中的图形还没有做足够的工作,不知道这是否正确,所以请随意忽略这一点。
另外,一件(非常)小事:您正在使用 LinkedList 进行随机访问。如果您使用索引号进行访问,ArrayList 通常会是一个更好的实现。
Is ReadSDF working properly? Without knowing much more, it might be possible that fragments is not being initialised properly, so when you go to access the elements it doesn't work properly, an exception is thrown which is caught and ignored by something higher up.
Cory Larson's code seems like it should work to me. It is logically the same.
I notice where you override 'paintComponent' in MolViewer, you call super.paintComponents(g) (a method of Container) rather than paintComponent (the method of JComponent). I haven't done enough with graphics in swing, to know if this is correct though, so feel free to ignore this.
Also, a (very) small thing: You're using LinkedLists for random access. ArrayList would be a better implementation in general if you're accessing with an index number.