Lan*_*fee 4 java swing paintcomponent
如果我有一个JPanel我无法修改的对象,有没有办法可以在paintComponent不使用注入的情况下修改它的方法?
一种方法我想的是获取JPanel的Graphics对象,将它传递给paintComponent(),这个执行操作Graphics的对象,终于画,在我的自定义JPanel.这样做的问题,是我需要能够这样原来的每一次做JPanel的paintComponent()叫.
我不需要替换paintComponent()中的内容,我只需要添加它.
例如:
JFrame frame = null;
for (Frame f : JFrame.getFrames()) {
if (((JFrame) f).getTitle().equals("Title")) {
JPanel panel = null;
// ... Cycle through all components and get the one that's a JPanel
// I want to use ColorConvertOp to make panel greyscale
}
}
Run Code Online (Sandbox Code Playgroud)
一种方法是使用Decorator Pattern来包装现有的类.然后,您的装饰器可以实现paintComponent以首先委托给原始组件,然后在其上面绘制.对于这种方法,您需要实际控制组件的创建,或者在创建组件层次结构后需要替换它们(使用父容器的getComponents()来查找要更改的组件).
| 归档时间: |
|
| 查看次数: |
2537 次 |
| 最近记录: |