零时给你写了个简单的: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();
}
}

解决方案 »

  1.   

    repaint() 会调用 paint()方法
      

  2.   

    大哥!
    我这儿就是不能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;
    }
      

  3.   

    你在paint中加入
    System.out.println("为什么不调用我?");试试
      

  4.   

    我在paint的第一行加了,结果输出好多那句话!怎么是不断去调用啊??
      

  5.   

    测试出来了!是这些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();
    }
      

  6.   

    if ((MenuItem)e.target instanceof MenuItem)
      

  7.   

    而且我每次点击frame的边框都会自动去调用好多次!但就是不paint真正的内容!javafaq2004(农村干部瞎忙活) 您有没有QQ啊我想找你帮忙..
      

  8.   

    程序会不会自动去转入action方法呢?
      

  9.   

    我另外一个frame,也是点击菜单项repaint,就运行正常!
    怎么这
    谁能帮帮我看一下啊我把程序传给他
    谢谢!!!!!!!!!!!
      

  10.   

    我现在发现就是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;
    }
      

  11.   

    试出来!!
    我把那些 if写在handleEvent里就对了
    !!!
    怎么会action里不行的啊!!!
      

  12.   

    你每次是从handleEvent调用,而不是action