各位大哥好 小弟才开始学习java 对一些东西不懂 请大家帮忙我想写一个很简单的程序 就是
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class cat extends Applet implements ActionListener
{
public void init()
{
Label prompt=new Label("猫");
Button btn=new Button("叫");
add(prompt);
add(btn);
btn.addActionListener(this);
}
public void paint(Graphics g)
{
g.drawString("喵", 100, 200);

    }


public void actionPerformed(ActionEvent e)
{
repaint();
}


}
我想让点击按钮的时候才打出喵  我应该怎样做
一般我看书上 
在问问大家applet程序是不是按顺序执行的
如果这样的话我把paint写在actionPerformed的后面
然后在actionPerformed里面调用
为什么还是不行
一运行就有“喵”
谢谢大家指导

解决方案 »

  1.   

    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;
    public class cat extends Applet implements ActionListener
    {
    boolean mao = false;
    public void init()
    {
    Label prompt=new Label("猫");
    Button btn=new Button("叫");
    add(prompt);
    add(btn);
    btn.addActionListener(this);
    }
    public void paint(Graphics g)
    {
    if(mao)
    g.drawString("喵", 100, 200);
      } public void actionPerformed(ActionEvent e)
    {
    mao = true;
    repaint();
    }
    }
      

  2.   

    为什么偶觉得LZ这个用SWT做要好点。。
      

  3.   

    谢谢#1楼 你的程序没问题 
    就是你能告诉我mao那个变量是怎样与点击按钮联系起来的
      

  4.   

    mao是一个Flag.只有当mao 是 true 的时候 喵 字菜会显示。你按"叫",变量mao就会是变成true。
      

  5.   


    btn.addActionListener(this);
     
    这一句
      

  6.   

    import java.awt.*;
    import java.awt.event.*;public class Test extends Frame implements ActionListener {
    TextField tf=null;
    public void init() {
    setLayout(new FlowLayout());
    setBounds(300,300,200,100);
    Button a=new Button("猫");
    Label prompt = new Label("+");
    Button btn = new Button("叫");
    Button l=new Button("=");
    tf=new TextField("    ");
    add(a);
    add(prompt);
    add(btn);
    add(l);
    add(tf);
    addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    });
    l.addActionListener(this);
    setVisible(true);
    } /*public void paint(Graphics g) {
    g.drawString("喵", 100, 200); }*/ public void actionPerformed(ActionEvent e) {
    tf.setText("喵喵喵");
    }
    public static void main(String[] args){
    new Test().init();
    }}