Topic: 看看我的程序怎么了?(re: ActionListener)

  Print this page

1.看看我的程序怎么了?(re: ActionListener) Copy to clipboard
Posted by: billchang
Posted on: 2005-10-07 09:42

各位好!我写过一些程序,总遇到个问题:一个button添加上ActionListener后,让它做些事情,但运行时,第一次点击它时,它没反应,第二次便执行了我所要做的事。但若在actionPerformed(ActionEvent e)中写上一句System.out.println("This is a applet.");则在DOS窗口会正常打印出This is a applet.点一次打印一次。下面是一个在applet中显示一张图片的程序,希望高手们运行一下。附件里有程序的所有内容。
//<applet code="PicApplet.class" height=400 width=400></applet>
import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;
/**
* <p>This is a applet for showing a picture.</p>
* <p>There is a button in the applet.</p>
* <p>It will show a picture when you click this button.</p>
*/
public class PicApplet extends Applet
implements ActionListener{
private Panel buttonPan = new Panel();
private Panel picPan = new Panel();
private Button showButton = new Button("Show");
public void init(){
setLayout(new BorderLayout());
buttonPan.add(showButton);
add(buttonPan,"South");
add(picPan,"Center");
showButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
//System.out.println("This is a applet.");
//You may modify a parameter when you want to change a picture.
Image img = getImage(getCodeBase(),"1.jpg");
picPan.getGraphics().drawImage(img,0,0,null);
}
}

PicApplet.rar (91.86k)


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923