java 关于repaint
importjava.awt.Graphics;importjavax.swing.JPanel;publicclassGamePanelextendsJPanel{pr...
import java.awt.Graphics;
import javax.swing.JPanel;
public class GamePanel extends JPanel {
private Ground ground;
private Shape shape;
public void display(Ground ground,Shape shape){
System.out.println("GamePanel'display");
this.ground = ground;
this.shape = shape;
this.repaint();
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
if(shape != null && ground != null){
shape.drawMe();
ground.drwaMe();
}
}
public GamePanel(){
this.setSize(300, 300);
}
}
我调用display方法后他后输出为:
GamePanel's display
shape.drawMe()的结果
ground.drwaMe()的结果
shape.drawMe()的结果
ground.drwaMe()的结果
为什么paintComponent会调用2变,我只想要一变的输出结果该怎么办? 展开
import javax.swing.JPanel;
public class GamePanel extends JPanel {
private Ground ground;
private Shape shape;
public void display(Ground ground,Shape shape){
System.out.println("GamePanel'display");
this.ground = ground;
this.shape = shape;
this.repaint();
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
if(shape != null && ground != null){
shape.drawMe();
ground.drwaMe();
}
}
public GamePanel(){
this.setSize(300, 300);
}
}
我调用display方法后他后输出为:
GamePanel's display
shape.drawMe()的结果
ground.drwaMe()的结果
shape.drawMe()的结果
ground.drwaMe()的结果
为什么paintComponent会调用2变,我只想要一变的输出结果该怎么办? 展开
展开全部
protected void paintComponent(Graphics g) {
应该覆盖protected void paint(Graphics g) {这个方法
应该覆盖protected void paint(Graphics g) {这个方法
追问
不行啊
会不会是别的地方出错了么
但是别的地方没有调用啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
if(shape != null && ground != null){
shape.drawMe();
ground.drwaMe();
}
放入display方法内,去掉paintComponent方法。
shape.drawMe();
ground.drwaMe();
}
放入display方法内,去掉paintComponent方法。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询