图片的连续播放问题,求解决~~

liuhuabai100 2011-04-28 11:04:19
jButton1为显示模式(点击一下,出现一张图片),目前的代码已经实现了功能;
jButton2为播放模式(点击一下,图片持续播放),google了很久,都没有找到解决办法,请高人解决啊~~
其中lqq为List<String>,lours为List<Object>,
mp1和mp2均为Map<Integer,String>,String中存放的是图片的绝对路径。
尝试过robot,没有成功。部分代码如下:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//设置jTextField1的内容和jButton1的图标
int jcb=jComboBox1.getSelectedIndex();
flag=playFlag(flag,jcb);
oursf=playOursf(oursf,jcb);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
//设置jButton1和jComboBox1不启动
jButton1.setEnabled(false);
jComboBox1.setEnabled(false);
int jtf=2;
try{
jtf=Integer.parseInt(jTextField2.getText());
} catch(NumberFormatException e){
JOptionPane.showMessageDialog(null, "不支持的数据格式", "出错啦!", JOptionPane.ERROR_MESSAGE);
}
/*Point p1=jButton1.getLocation();
try {
Robot robot=new Robot();
robot.mouseMove(p1.x+40, p1.y+40);
robot.mousePress(InputEvent.BUTTON1_MASK);
Thread.sleep(300);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.mouseMove(p1.x+60, p1.y+60);
Thread.sleep(jtf*1000);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
int jcb=jComboBox1.getSelectedIndex();
flag=playFlag(flag,jcb);
oursf=playOursf(oursf,jcb);
}
//playFlag方法,用于设置jButton1的图标和jTextField1的文字内容
public int playFlag(int flag,int playMode){
int val=flag;
//Mode0顺序播放,Mode1为随机播放
switch(playMode){
case 0:
if(val%mp1.size()==0){
val=0;
}
jTextField1.setText(lqq.get(val));
icn=new javax.swing.ImageIcon(mp1.get(val++).toString());
jButton1.setIcon(icn);
break;
case 1:
val=(int) (Math.random()*mp1.size());
jTextField1.setText(lqq.get(val));
icn=new javax.swing.ImageIcon(mp1.get(val).toString());
jButton1.setIcon(icn);
break;
default:
jTextField1.setText(null);
jButton1.setIcon(null);
break;
}
return val;
}
//playOursf的方法,用于设置jLabel1中的图片和jTextArea1中的文字内容
public int playOursf(int oursf,int playMode){
int val=oursf;
//Mode0顺序播放,Mode1为随机播放
switch(playMode){
case 0:
if(val%mp2.size()==0){
val=0;
}
jTextArea1.setText(lours.get(val).toString());
icn=new ImageIcon(mp2.get(val++).toString());
jLabel1.setIcon(icn);
break;
case 1:
val=(int) (Math.random()*mp2.size());
jTextArea1.setText(lours.get(val).toString());
icn=new javax.swing.ImageIcon(mp2.get(val).toString());
jLabel1.setIcon(icn);
break;
default:
jTextArea1.setText(null);
jLabel1.setIcon(null);
break;
}
return val;
}
...全文
142 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yy727742460 2011-05-26
  • 打赏
  • 举报
回复
我在弄这个,看看
huntor 2011-04-29
  • 打赏
  • 举报
回复
点击jbutton2 使用 javax.swing.Timer 定时更新图片。
liuhuabai100 2011-04-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 huntor 的回复:]
点击jbutton2 使用 javax.swing.Timer 定时更新图片。
[/Quote]
非常谢谢,使用Timer实现了功能。ActionListener taskPerformer这个地方却没太弄明白,O(∩_∩)O~

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
//设置jButton1和jComboBox1不启动
jButton1.setEnabled(false);
jComboBox1.setEnabled(false);
int jtf=2000;
try{
jtf=Integer.parseInt(jTextField2.getText());
} catch(NumberFormatException e){
JOptionPane.showMessageDialog(null, "不支持的数据格式", "出错啦!", JOptionPane.ERROR_MESSAGE);
return;
}
ActionListener taskPerformer=new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int jcb=jComboBox1.getSelectedIndex();
flag=playFlag(flag,jcb);
oursf=playOursf(oursf,jcb);
}
};
timer=new Timer(jtf,taskPerformer);
timer.start();
}
//jButton3实现暂停功能
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt){
timer.stop();
}
//jButton4实现停止功能
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt){
timer.stop();
//重新使jButton1和jComboBox1启动
jButton1.setEnabled(true);
jComboBox1.setEnabled(true);
//取消图片显示,并且使两个变量归零
flag=playFlag(flag,3);
oursf=playOursf(oursf,3);
}

62,614

社区成员

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

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