JPanels 除了第一个未绘制

发布于 2024-12-12 06:07:06 字数 97 浏览 2 评论 0原文

以下代码假设读取包含一组分子结构的文件,然后添加一堆 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喜爱皱眉﹌ 2024-12-19 06:07:06

drawMolViewPanel() 函数似乎有点过度设计。首先,panes 列表似乎主要是临时的(您向其中添加对象,然后从该列表中将它们添加到 MolTable 自己的面板集合中;我不知道我认为你不需要它)。如果我正确理解了这个函数,这会做同样的事情,并且对我来说更有意义:

public void drawMolViewPanel(String sdf) throws FileNotFoundException, CDKException
{
    ReadSDF(sdf);
    this.removeAll();

    for (int i = 0; i < this.fragments.size(); i++)
    {    
        MolViewer mv = new MolViewer();
        mv.setMolecule((Molecule)this.fragments.get(i));
        this.add(mv);
    }

    this.revalidate();
    this.repaint();
}

不幸的是,我不完全确定这是你的问题。

The drawMolViewPanel() function seems a little overengineered. For one, the panes list seems to be mostly temporary (you add objects to it, then from that list, add them to the MolTable'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:

public void drawMolViewPanel(String sdf) throws FileNotFoundException, CDKException
{
    ReadSDF(sdf);
    this.removeAll();

    for (int i = 0; i < this.fragments.size(); i++)
    {    
        MolViewer mv = new MolViewer();
        mv.setMolecule((Molecule)this.fragments.get(i));
        this.add(mv);
    }

    this.revalidate();
    this.repaint();
}

I'm not entirely sure that's your issue, unfortunately.

橘香 2024-12-19 06:07:06

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文