我知道这是措辞不佳,但我不知道如何更好地说。基本上我有我自己的JComponent MyComponent
,它在它的图形上绘制了一些东西。我希望它绘制其的东西,然后调用一个方法来完成油漆,这里有一个例子:我怎样才能让一个班级加入我的绘画功能?
public class MyComponent extends JComponent{
// etc
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g2 = (Graphics2D)g;
g2.drawSomething(); // etc
// Once it is done, check if that function is exists, and call it.
if(secondaryPaint != null){
secondaryPaint(g2);
}
}
}
然后,在不同的类:
// etc
MyComponent mc = new MyComponent()
mc.setSecondaryDrawFunction(paint);
// etc
private void paint(Graphics2D g2){
g2.drawSomething();
}
我不知道如何lambda表达式工作还是适用于这种情况,但也许呢?
你的意思是抽象类?你可以创建一个从JComponent继承的抽象类,并重写paintComponent方法,并在最后一次调用时调用抽象类定义的抽象方法 – Pali