站内搜索: 请输入搜索关键词

当前页面: JAVA 编程资料牛鼻论坛Java & J2SE 技术区→各位师兄师姐好,请帮忙看个程序?小妹不胜感激!

各位师兄师姐好,请帮忙看个程序?小妹不胜感激!

发表新主题   回复此主题

第1楼 2007-06-02 21:40 小妖 写道:

各位师兄师姐好,请帮忙看个程序?小妹不胜感激!

我想点按钮时图片才会连续显示,可是捣鼓了半天也没弄出来......


import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.applet.*;


public class Application1 extends Frame implements ActionListener
{
private Image[] Image;
private int curImage;
boolean bstop;
private MyThread thread;

Font aa=new Font("宋体",Font.BOLD ,20);
Panel p1=new Panel();
Button b1=new Button("退出");
Button b2=new Button("请欣赏!");

public Application1() {
super("生日快乐 !");
setSize(800, 800);
setLayout(null);
p1.setBounds(450,450,90,90);
b1.setForeground(Color.orange);
b2.setForeground(Color.orange);
b1.setBackground(Color.white);
b2.setBackground(Color.white);
b1.setFont(aa);
b2.setFont(aa);
add(p1);
p1.add(b1);
p1.add(b2);
p1.setBackground( Color.white);
b1.addActionListener(this);
b2.addActionListener(this);


final String dir ="D:\\ssyy\\ss\\ss";
String[] files=new File(dir).list();
int num=files.length<=10? files.length:10;
Image = new Image[num];
for (int i = 0; i < num; i++)
Image[i] = Toolkit.getDefaultToolkit().createImage(dir+"\\"+files[i]);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
bstop=true; System.exit(0);
}
});
thread=new MyThread();
thread.start();
show();
}

public void paint(Graphics g) {
g.drawImage(Image[curImage], 0, 20, this);

}
class MyThread extends Thread
{
public void run()
{
while(!bstop)
{
repaint();
try{sleep(1000);}
catch(InterruptedException e){}
curImage++;
if(curImage==Image.length)
curImage=0;
}
}
}
public static void main(String args[])
{
Application1 n = new Application1();
n.show();
}
public void actionPerformed(ActionEvent e)
{

if(e.getActionCommand() =="退出")
System.exit(0);
if(e.getActionCommand() =="请欣赏!")
{


}

}

}

第2楼 2013-08-31 12:44 Robot :

各位师兄师姐好,请帮忙看个程序?小妹不胜感激! 相关


第3楼 2007-06-02 22:06 Kimmy★Piggy 写道:

呵呵,很简单~

把public Application1() {}中的thread.start();这句移动到最后if(e.getActionCommand() =="请欣赏!") {/*这里*/}

就可以了~

发表新主题   回复此主题