我正在进行自学AP Java课程,我只需要一些快速帮助。我有paintComponent的这个问题。我总是收到消息来覆盖它并为它创建其他东西。这是我的代码:
package pkghello;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;
/**
*
* @author ap
*/
public class RectangleComponent extends JComponent {
public void paintComponent (Graphics g) //It only highlights "paintComponent"
{
Graphics2D g2 = (Graphics2D) g;
//
Rectangle box = new Rectangle (5,10,20,30);
g2.draw(box);
box.translate(90, 90);
box.setSize(150,150);
g2.draw(box);
}
}
答案 0 :(得分:2)
两件事。它可能希望您添加@Override
注释,除非您真的需要它,否则paintComponent
应该保留protected
例如。
@Override
protected void paintComponent (Graphics g)