急,求帮助
Jlist中如何实现像QQ那样,有新消息来了,让图片跳动,图中间是Jlist,
MyCellRenderer代码如下:
package com.view.v;
import java.awt.Component;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
public class MyCellRenderer extends JLabel implements ListCellRenderer {
/**
*
*/
private static final long serialVersionUID = 5774183038120753788L;
Item item;
private Icon icon;
public MyCellRenderer() {
};
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
this.item =(Item)value;
final String s = item.getText();
icon = item.getIcon();
setText(s);
setBorder(BorderFactory.createEmptyBorder(5, 5, 1, 1));// 加入宽度为5的空白边框
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setIcon(icon);//设置图片
setEnabled(list.isEnabled());
setFont(list.getFont());
setOpaque(true);
return this;
}
}
Item类代码
package com.view.v;
import javax.swing.Icon;
public class Item {
private String text;
private Icon icon;
public Icon getIcon() {
return icon;
}
public void setIcon(Icon icon) {
this.icon = icon;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
@Override
public String toString() {
return text;
}
}
然后list中这样使用:
DefaultListModel model = new DefaultListModel();;
userList.setModel(model);
Item item = new Item();
item.setText("admin");
item.setIcon(icon);//设置显示的图片
model.addElement(item);
userList.setCellRenderer(new MyCellRenderer());
现在要的效果就是像QQ那样,有新消息来的时候,对应的用户头像跳动
我不懂怎么怎弄,网上有人说设置一个计时器,不停切换头像实现跳动效果,我在MyCellderer里面设置过,但是没起作用。代码有什么不明的可以提出来
跪求帮助 Java QQ 图片 --------------------编程问答-------------------- 就是用计时器或者自己写线程不停的切换头像啊。 --------------------编程问答--------------------
在哪里添加计时器好,反正Mycellderer里面添加了计时器不起作用 --------------------编程问答-------------------- Mycellderer应该可以,如果不行,在他的上层laylower放,例如:panle或者lable试一试! --------------------编程问答--------------------
Mycellderer试过了,不行 在Mycellderer里面
设置计时器,不断切换图片
setIcon(icon);//设置图片 --------------------编程问答--------------------
package com.view.v;
import java.awt.Component;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
public class MyCellRenderer extends JLabel implements ListCellRenderer {
/**
*
*/
private static final long serialVersionUID = 5774183038120753788L;
Item item;
private Icon icon;
public MyCellRenderer() {
};
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
this.item =(Item)value;
final String s = item.getText();
//icon = item.getIcon();
setText(s);
setBorder(BorderFactory.createEmptyBorder(5, 5, 1, 1));// 加入宽度为5的空白边框
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
Timer timer = new Timer();
URL img = MyCellRenderer.class.getClassLoader().getResource("1.png");
URL img2 = MyCellRenderer.class.getClassLoader().getResource("2.png");
final Icon icon = new ImageIcon(img);
final Icon icon2 = new ImageIcon(img2);
timer.schedule(new TimerTask() {//添加了计时器,0.5秒切换一次图片
boolean tag=true;
@Override
public void run() {
if (tag) {
setIcon(icon);
System.out.println("ok----->1");
tag=false;
}else {
setIcon(icon2);
System.out.println("ok----->2");
tag=true;
}
}
}, 0, 500);
setEnabled(list.isEnabled());
setFont(list.getFont());
setOpaque(true);
return this;
}
}
各位帮忙分析下,打印结果为何是这样:
ok----->1
ok----->1
ok----->1
ok----->2
ok----->2
ok----->2
我想象中应该是
ok----->1
ok----->2
ok----->1
ok----->2
ok----->1
ok----->2
关键是图片没动,好像切换没效果
--------------------编程问答-------------------- --------------------编程问答-------------------- swing 被我直接跳过 了 --------------------编程问答-------------------- 两个线程是两条平行的线程,执行哪个完全是CPU随机的,建议LZ可以将时间设短点 往图片里值时先取比较下 不一样再塞 --------------------编程问答-------------------- 为什么我按你的是可以的
@Test
public void test() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {//添加了计时器,0.5秒切换一次图片
boolean tag = true;
@Override
public void run() {
if (tag) {
System.out.println("ok----->1");
tag = false;
} else {
System.out.println("ok----->2");
tag = true;
}
}
}, 0, 500);
while (true) {
}
}
ok----->1
ok----->2
ok----->1
ok----->2
ok----->1
ok----->2
ok----->1
ok----->2
ok----->1
ok----->2
ok----->1
ok----->2
ok----->1
ok----->2
ok----->1
ok----->2
ok----->1
ok----->2
ok----->1
ok----->2
ok----->1
ok----->2
ok----->1
ok----->2
ok----->1
ok----->2
ok----->1
ok----->2
ok----->1
ok----->2
ok----->1
ok----->2
ok----->1
ok----->2
ok----->1
ok----->2
ok----->1
ok----->2
ok----->1
ok----->2
ok----->1
ok----->2
ok----->1
ok----->2
补充:Java , Java SE