作者honamida (honamida)
看板java
标题[问题] JPanel更新
时间Thu Oct 25 19:07:13 2012
小弟想随着ComboBox的选项不同 去改变列出的Jlist
首先写了一个jlist的 class
public class listFriends extends JPanel {
String[] friendList = new String [5];
private JList friendBox;
public listFriends(){
setLayout (new BorderLayout());
.
. <--------------- friendList 资料
.
friendBox = new JList(friendList);
this.add(friendBox);
}
}
然後在Combox listner的地方
comboList.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
JComboBox cb = (JComboBox)e.getSource();
Object item = e.getItem();
String userName = (String)item;
if(e.getStateChange() == ItemEvent.SELECTED){
Allen --> if(userName.equals("Allen")){
System.out.println("Allen");
User.user = 1;
remove(friendList);
friendList = new listFriends();
add(friendList);
}
Peter ---> if(userName.equals("Peter")){
System.out.println("Peter");
User.user = 2;
remove(friendList);
friendList = new listFriends();
add(friendList);
}
else if (e.getStateChange() == ItemEvent.DESELECTED){
;
}
}
});
姑狗了很久 找不到办法可以解决@@
我不管在combobox里选了"Peter" 或是 "Allen" 他们各字对应的list 就是不会改变
还是印出原本的list
原本的想法是 remove掉原本的list以後 再用原本写好的listfriend 再去new一个
想要更新的list 然後这个list的内容会随着user的值改变 然後再把new出来的新list
add到panel里 不过点了半天完全没反应@@~~
希望有大大能够帮忙解惑为什麽没办法 感谢!
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 123.195.221.161
1F:推 PsMonkey:有印出 Allen 吗? 你有要他重新绘制吗? 10/26 09:19
2F:→ honamida:我发现我忘记repaint() @@~~~ thx 10/26 23:25