简体   繁体   English

调用其他组件的重绘

[英]Call repaint of an other Component

I'm doing some small Java program for college and i'm running into a problem to redraw some Graphics after an update. 我正在为大学做一些小型Java程序,并且在更新后我遇到了重绘某些图形的问题。 Here's the context: 这是上下文:

I have a main Jframe which contains basically two things, a JComponent used to draw/paint some Graphics (Rectangles, Circle, etc) and a JMenu to give sort options to the user. 我有一个主Jframe,它基本上包含两个东西,一个用于绘制/绘制一些图形(矩形,圆形等)的JComponent和一个用于为用户提供排序选项的JMenu。

The JComponent overrides paintComponent so I can draw the Graphics upon request. JComponent会覆盖paintComponent,因此我可以根据请求绘制Graphics。 The Graphics are customs classes with methods to define the type of Graphic and each will call their own drawRect() or drawOval(). 图形是海关类,其中包含定义Graphic类型的方法,每个类都将调用自己的drawRect()或drawOval()。 That is working very well. 这非常有效。 If I call repaint() for the main JFrame it will call paintComponent of the JComponent() as expected. 如果我为主JFrame调用repaint(),它将按预期调用JComponent()的paintComponent。

One of the menus in the JMenu has options to sort the Graphics per different order. JMenu中的一个菜单具有按不同顺序对图形进行排序的选项。 So in my ActionListner of each options I proceed with the sort of the LinkedList containing the Graphics. 所以在我的每个选项的ActionListner中,我继续使用包含Graphics的LinkedList。

The problem is when im done with the sort in the ActionListener, I would call repaint() to draw the Graphics in the JComponent but it will call the repaint() of the JMenu instead... So my question is how to somehow call the repaint of the JComponent from the JMenu? 问题是当我在ActionListener中完成排序时,我会调用repaint()在JComponent中绘制Graphics但它会调用JMenu的repaint()而不是...所以我的问题是如何以某种方式调用从JMenu重绘JComponent?

Thanks. 谢谢。

One of the menus in the JMenu has options to sort the Graphics per different order. JMenu中的一个菜单具有按不同顺序对图形进行排序的选项。 So in my ActionListner of each options I proceed with the sort of the LinkedList containing the Graphics. 所以在我的每个选项的ActionListner中,我继续使用包含Graphics的LinkedList。

Then that code should invoke repaint() on the component. 然后该代码应该调用组件上的repaint()。 That is LinkList should be part of the component that paints the Graphics. 那就是LinkList应该是绘制Graphics的组件的一部分。 So you would have a method like sort(...) that you invoke on the component. 所以你会在组件上调用sort(...)这样的方法。 The method would then do the sort and then repaint() the component. 然后该方法将进行排序,然后重新绘制()组件。

This is the way all Swing methods work, like setText(...) , setBackground(...) . 这是所有Swing方法的工作方式,如setText(...)setBackground(...) You invoke these methods on the component and then the component invokes repaint() on itself. 您在组件上调用这些方法,然后组件在其自身上调用repaint()

The key is to make sure that the calling code has a clean reference to the viewed object of the called code. 关键是要确保调用代码对被调用代码的被查看对象有一个干净的引用。 In other words, the listener code for your menu needs a reference to the drawing JComponent, usually placed into a field of the listener's class. 换句话说,菜单的监听器代码需要引用绘图JComponent,通常放在监听器类的字段中。 How you pass it in will depend on the structure of your program. 如何传递它取决于程序的结构。 This can be done via a constructor parameter, or if you want to be cleaner, via dependency injection. 这可以通过构造函数参数完成,或者如果您想通过依赖注入更清洁。

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

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