Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java GUI 设计  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 JList的一个问题
probing





发贴: 9
积分: 0
于 2006-03-04 19:25 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
程序如下
期待的运行结果是JList有三行显示

但是多运行几次此程序会发现有的时候只显示2行
而且出来的界面里这两行根本无法选中
这种情况的几率大概是10%左右
请问这是bug还是程序有问题呢?

import java.awt.BorderLayout;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JList;

public class ListTest extends JFrame {

  Vector data;
  private JList list;
  /**
   * Launch the application
   * @param args
   */
  public static void main(String args[]) {
    Vector myData = new Vector();
    try {
      ListTest frame = new ListTest( myData );
      frame.setVisible(true);
    } catch (Exception e) {
      e.printStackTrace();
    }
    myData.add("line3");
  }

  /**
   * Create the frame
  */
  public ListTest( Vector data ) {
    super();
    this.data = data;
    setBounds(100, 100, 500, 375);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    data.add( "line1" );
    data.add( "line2" );
    list = new JList( data );
    getContentPane().add(list, BorderLayout.CENTER);
    //
    
  }

}



作者 Re:JList的一个问题 [Re:probing]
xiaosilent





发贴: 46
积分: 1
于 2006-03-04 22:36 user profilesend a private message to usersend email to xiaosilentsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
以下仅代表个人观点

myData.add("line3"); 的位置有问题.

//====第一种建议
public static void main(String args[]) {
Vector myData = new Vector();
try {
ListTest frame = new ListTest( myData );
frame.setVisible(true);
myData.add("line3");
frame.validate();
} catch (Exception e) {
e.printStackTrace();
}

}

//==第二种
public static void main(String args[]) {
Vector myData = new Vector();
try {
ListTest frame = new ListTest( myData );
myData.add("line3");
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}

}

总之,在显示以后,如果改变了list的内容,应该重新验证.



作者 Re:JList的一个问题 [Re:probing]
yzdbj





发贴: 9
积分: 0
于 2006-03-05 10:10 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
这要有MVC(Model-View-Controller)模型的概念!(可以与VB三层的概念相结合)
作用:Modell管理自己的状态且处理对状态的操作,当它改变时要负责通知View;View负责显示数据;Controller:管理Model与View之间的交互!
这是因为List控件是一个重量级控件!它只负责把数据给显示出来。
在这里M(Model)是你程序里的Vector ,List控件相当于你程序里的V(View)!List里面显示的数据只是初始化时的数据,当要进行添加、删除操作时Model要 通知List控件,关键在这里还要定义一个类:DefaultListModel,它负责通知View.
例如:
DefaultListModel idlm;
idlm= new DefaultListModel();
JList ijl ;
ijl= new JList(idlm);
idlm.addElement("要添加的项目");//在相关事件里添加
idlm.remove(“要删除的项目”);//在相关事件里添加
不知这样解释可以吗?
我是一新手,,大家可以常交互!



作者 Re:JList的一个问题 [Re:probing]
probing





发贴: 9
积分: 0
于 2006-03-08 16:34 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
验证了一下二楼的第二个方法是可以的,第一个验证了一下也不行。
好像比较正规的做法就是楼上的这种了 或者自己继承AbstractListModel自己写一个model
看了一下swing的源码其实 DefaultListModel实现也就是里面包了一个Vector,只不过add的时候会调用父类里面的fireIntervalAdded方法。api里对这个的解释是AbstractListModel subclasses must call this method after one or more elements are added to the model.
看来直接传Vector给JList构造函数的方法一般是在以后不会对list内容改变的情况下用的,如果要动态改变就要继承AbstractListModel并使用fireIntervalAdded

我是这么理解的,不过觉得这个设计对开发者好像不够方便...


probing edited on 2006-03-08 16:36


flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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