好奇怪!!frame里repaint不调用paint方法吗????

wangweixun 2004-11-21 08:56:22
我在action方法里调用repaint() 为什么不重打印啊???
单独让paint打印三种情况就是对的!

高手帮帮我啊~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public void paint(Graphics g)
{
if (firstTime)
{
return;
}
gameMap=new GameMap(m,n,lev);
inIcons=new ImageIcon[m][n];
icons=new Image[m][n];
for (int i=1;i<=m;i++)
{
for (int j=1;j<=n;j++)
{
int k=gameMap.map[i][j];
inIcons[i-1][j-1]=new ImageIcon
("D:/My Documents/HowToConnect/image/icon"+k+".jpg");
icons[i-1][j-1]=inIcons[i-1][j-1].getImage();
}
}
int posX=60;
int posY=80;
for (int j=0;j<n;j++)
{
for (int i=0;i<m;i++)
{
g.drawImage(icons[i][j],posX,posY,this);
posX+=60;
}
posX=60;
posY+=60;
}
}

public boolean action (Event e,Object o)
{
if (e.target instanceof MenuItem)
{
if (e.arg.equals("About"))
about=new About(this);
else if (e.arg.equals("Easy"))
{
level=1; m=12; n=7; lev=21;
firstTime=false;
repaint();
}
else if (e.arg.equals("Normal"))
{
level=2; m=14; n=8; lev=28;
firstTime=false;
repaint();
}
else if (e.arg.equals("Hard"))
{
level=3; m=16; n=9; lev=36;
firstTime=false;
repaint();
}
}
return true;
}
...全文
177 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
snow_oracle 2004-11-21
  • 打赏
  • 举报
回复
这么快就运行出了?
我还在看呢.具体还是不大清楚,刚学.只是曾经遇到过相似的问题.
虽然解决了,原因还是不是太清楚,带着问题进一步学习
longjing_g 2004-11-21
  • 打赏
  • 举报
回复
你每次是从handleEvent调用,而不是action
wangweixun 2004-11-21
  • 打赏
  • 举报
回复
试出来!!
我把那些 if写在handleEvent里就对了
!!!
怎么会action里不行的啊!!!
wangweixun 2004-11-21
  • 打赏
  • 举报
回复
我现在发现就是action方法进不进!!
但是我有另外一个一样结构的小画板程序,就运行正常!!会自动转入 action方法!
import java.awt.*;

public class MyDrawPad extends Frame
。。。
public boolean action (Event e,Object o)
{
if (e.target instanceof MenuItem)
{
if (e.arg.equals("White background"))
{

System.out.println("!!!");
setBackground(Color.white);
}
else if (e.arg.equals("Black background"))
setBackground(Color.black);
else if (e.arg.equals("Yellow background"))
setBackground(Color.yellow);
else if (e.arg.equals("Black"))
color=1;
else if (e.arg.equals("Red"))
color=2;
else if (e.arg.equals("Blue"))
color=3;
else if (e.arg.equals("Green"))
color=4;
else if (e.arg.equals("Circle-1"))
shape=1;
else if (e.arg.equals("Circle-2"))
shape=2;
else if (e.arg.equals("Circle-3"))
shape=3;
else if (e.arg.equals("Square"))
shape=4;
}
return true;
}
snow_oracle 2004-11-21
  • 打赏
  • 举报
回复
apljp@163.com
互相学习
wangweixun 2004-11-21
  • 打赏
  • 举报
回复
我另外一个frame,也是点击菜单项repaint,就运行正常!
怎么这。。。
谁能帮帮我看一下啊。。。我把程序传给他。。。
谢谢!!!!!!!!!!!
snow_oracle 2004-11-21
  • 打赏
  • 举报
回复
程序会不会自动去转入action方法呢?
wangweixun 2004-11-21
  • 打赏
  • 举报
回复
而且我每次点击frame的边框都会自动去调用好多次!但就是不paint真正的内容!

javafaq2004(农村干部瞎忙活) 您有没有QQ啊。。。我想找你帮忙..
jFresH_MaN 2004-11-21
  • 打赏
  • 举报
回复
if ((MenuItem)e.target instanceof MenuItem)
wangweixun 2004-11-21
  • 打赏
  • 举报
回复
测试出来了!是这些if 进不进!!

怎么回事啊!
if (e.target instanceof MenuItem)
{
if (e.arg.equals("About"))
about=new About(this);
else if (e.arg.equals("Easy"))
{
//这里三处给m,n,lev赋值,然后调用repaint()方法啊!
m,n,lev是这个类的私有成员函数
level=1; m=12; n=7; lev=21;
firstTime=false;
repaint();
}
catblue 2004-11-21
  • 打赏
  • 举报
回复
关注ing
wangweixun 2004-11-21
  • 打赏
  • 举报
回复
我在paint的第一行加了,结果输出好多那句话!怎么是不断去调用啊??
javafaq2004 2004-11-21
  • 打赏
  • 举报
回复
你在paint中加入
System.out.println("为什么不调用我?");试试
wangweixun 2004-11-21
  • 打赏
  • 举报
回复
大哥!
我这儿就是不能repaint()!好奇怪啊!

public void paint(Graphics g)
{
if (firstTime)
{
return;
}
//注:下面的m,n,lev就是action里由点击菜单项事件里赋值的

gameMap=new GameMap(m,n,lev);
inIcons=new ImageIcon[m][n];
icons=new Image[m][n];
for (int i=1;i<=m;i++)
{
for (int j=1;j<=n;j++)
{
int k=gameMap.map[i][j];
inIcons[i-1][j-1]=new ImageIcon
("D:/My Documents/HowToConnect/image/icon"+k+".jpg");
icons[i-1][j-1]=inIcons[i-1][j-1].getImage();
}
}
int posX=getWidth()/2-(m/2)*60;
int posY=(getHeight()-50)/2-(n/2)*60+30;
for (int j=0;j<n;j++)
{
for (int i=0;i<m;i++)
{
g.drawImage(icons[i][j],posX,posY,this);
posX+=60;
}
posX=getWidth()/2-(m/2)*60;
posY+=60;
}
}

public boolean action (Event e,Object o)
{
if (e.target instanceof MenuItem)
{
if (e.arg.equals("About"))
about=new About(this);
else if (e.arg.equals("Easy"))
{
//这里三处给m,n,lev赋值,然后调用repaint()方法啊!
m,n,lev是这个类的私有成员函数
level=1; m=12; n=7; lev=21;
firstTime=false;
repaint();
}
else if (e.arg.equals("Normal"))
{
level=2; m=14; n=8; lev=28;
firstTime=false;
repaint();
}
else if (e.arg.equals("Hard"))
{
level=3; m=16; n=9; lev=36;
firstTime=false;
repaint();
}
}
return true;
}
zhuyupu2004 2004-11-21
  • 打赏
  • 举报
回复
repaint() 会调用 paint()方法
javafaq2004 2004-11-21
  • 打赏
  • 举报
回复
零时给你写了个简单的:

import java.awt.*;
import java.awt.event.*;

public class MyTest extends Frame implements ActionListener{
Button b1 = new Button("Button1");
String str = "";
public void actionPerformed(ActionEvent e){
str = "red";
repaint();
}
public MyTest(){
setLayout(new FlowLayout());
add(b1);
b1.addActionListener(this);
setSize(400,300);
setVisible(true);
}
public void paint(Graphics g){
if(str.equals("red")){
g.setColor(Color.red);
g.drawString("MyTest",100,100);
}
}
public static void main(String[] args){
new MyTest();
}
}
javafaq2004 2004-11-21
  • 打赏
  • 举报
回复
代码贴得不多不少了点。多贴点好调试,少贴点看起来明白。

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧