这个程序是一个用swing显示文件的列表.button2是一个按钮它连接的是一个日期控件.会返回一个字符串.如20091130.如果把20091130传给getFile方法就会返会一个list..我在button2里把他封装在了一个JList里面.当我选取一个日期时为什么不能把list放到3333这里啊
public class MyFirstFrame extends JFrame {
33333  static JList list; static String date; public static void main(String[] args) throws IOException {
MyFirstFrame mf = new MyFirstFrame();
mf.setVisible(true);
}
public MyFirstFrame(JList list,String Date){
super();
try {
jbInit();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

} public MyFirstFrame() throws HeadlessException {
super();
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
} public MyFirstFrame(GraphicsConfiguration gc) {
super(gc);
} public MyFirstFrame(String title) throws HeadlessException {
super(title);
} public MyFirstFrame(String title, GraphicsConfiguration gc) {
super(title, gc);
} BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton(); private void jbInit() throws Exception {
this.getContentPane().setLayout(borderLayout1);
this.getContentPane().setBackground(SystemColor.inactiveCaption);
this
.addHierarchyBoundsListener(new MyFirstFrame_this_hierarchyBoundsAdapter(
this));
jPanel1.setBackground(Color.lightGray);
jPanel1.setMinimumSize(new Dimension(52, 20));
jPanel1.setPreferredSize(new Dimension(10, 30));
jButton1.setAlignmentX((float) 1.0);
jButton1.setAlignmentY((float) 1.0);
jButton1.setBorder(BorderFactory.createLoweredBevelBorder());
jButton1.setPreferredSize(new Dimension(22, 23));
jButton1.setToolTipText("");
jButton1.setHorizontalTextPosition(SwingConstants.CENTER);
jButton1.setMnemonic('0');
jButton1.setText("下载");
jButton1
.addActionListener(new MyFirstFrame_jButton1_actionAdapter(this));
11111 System.out.println(list+"++++++++++"); setSize(600, 400);
jButton2.setText("请选择日期");
jButton2
.addActionListener(new MyFirstFrame_jButton2_actionAdapter(this));
this.getContentPane().add(jButton1, java.awt.BorderLayout.SOUTH);
this.getContentPane().add(new JScrollPane(list),
java.awt.BorderLayout.CENTER);
this.getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);
jPanel1.add(jButton2);
} } public void this_ancestorMoved(HierarchyEvent e) { } public void jButton2_actionPerformed(ActionEvent e) throws IOException {
MyCalendar ca = new MyCalendar();
ca.setVisible(true);
date = ca.getValue();
UrlCon co=new UrlCon();
List l = co.getFile(date);
System.out.println(l.size());
for (int i = 0; i < l.size(); i++) {
if (l.get(i).equals("WEB-INF")) {
l.remove(i);
}
}
Object[] str = new Object[l.size()];
str = l.toArray();
final DefaultListModel model = new DefaultListModel();
for (int i = 0, n = str.length; i < n; i++) {
model.addElement(str[i]);
}
22222222   list = new JList(model);


}}class MyFirstFrame_jButton2_actionAdapter implements ActionListener {
private MyFirstFrame adaptee; MyFirstFrame_jButton2_actionAdapter(MyFirstFrame adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
try {
adaptee.jButton2_actionPerformed(e);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}class MyFirstFrame_this_hierarchyBoundsAdapter extends HierarchyBoundsAdapter {
private MyFirstFrame adaptee; MyFirstFrame_this_hierarchyBoundsAdapter(MyFirstFrame adaptee) {
this.adaptee = adaptee;
} public void ancestorMoved(HierarchyEvent e) {
adaptee.this_ancestorMoved(e);
}
}
class UrlCon {
public ArrayList getFile(String date) throws IOException {
ArrayList l = new ArrayList();
try {
// URL url = new URL("http://localhost:8080/ftpdowns/MyServlet");
// URL url=new
// URL("http://136.5.68.7:8080/creport/files/20091130/xlsfiles/");
// System.out.println(value+"==+++++"); URL url = new URL("http://136.5.68.7:8080/creport/filelist?dir="
+ date);
// URL url=new
// URL("http://136.5.68.7:8080/creport/filelist?dir=20091130");
HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
// urlcon.setDoOutput(true);
// urlcon.setDoInput(true);
urlcon.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(urlcon
.getInputStream()));
String inputLine;
/*
 * while ((inputLine = in.readLine()) != null) {
 * if(inputLine.substring(0, 2).equals("<a")){ String
 * regex="(CDMA.*?xls)"; Pattern p= Pattern.compile(regex); Matcher
 * m= p.matcher(inputLine); while (m.find()) {
 * if(decode(m.group()).substring(0, 5).equals("CDMA网"))
 * l.add(decode(m.group()));
 *  } } }
 */
while ((inputLine = in.readLine()) != null) {
l.add(inputLine);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return l;
}}

解决方案 »

  1.   

    这个类本来是一个swing,它里面会显示getFile方法返回的文件列表.
    我就是为什么当我选取了一个日期的时候,在橙色的那里根本得不到值.
      

  2.   

    代码有点长,而且就那些代码不好调试,缺少其他用到的class
      

  3.   

    public void jButton2_actionPerformed(ActionEvent e) throws IOException {
    MyCalendar ca = new MyCalendar();
    ca.setVisible(true);
    date = ca.getValue();
    UrlCon co=new UrlCon();
    List l = co.getFile(date);
    //list取到值了吗?
    System.out.println(l.size());
    for (int i = 0; i < l.size(); i++) {
    if (l.get(i).equals("WEB-INF")) {
    l.remove(i);
    }
    }
    Object[] str = new Object[l.size()];
    str = l.toArray();
    //str的值是什么?
    System.out.println(str);
    final DefaultListModel model = new DefaultListModel();
    for (int i = 0, n = str.length; i < n; i++) {
    model.addElement(str[i]);
    }
    //model的值是什么?
    22222222  list = new JList(model);
    }

    final DefaultListModel model = new DefaultListModel();
    list = new JList(model);
    放到jbInit()方法中,jButton2事件只负责addElement(str[i]);