实现类似QQ好友列表功能,int i表示有i个好友,生成i个JPanel 
我的代码如下,存在问题,没法正常显示。请高手指点 
int i=6;
final JPanel panel[i];
final JLabel face[i];
final JLabel ID[i];
for(j=0;j<=i;j++)
{
panel[j] = new JPanel();
panel[j].setLayout(null);
panel[j].setBounds(25, 21*j+21, 94, 29);
frame.getContentPane().add(panel[j]); face[j] = DefaultComponentFactory.getInstance().createLabel("face");
face[j].setBounds(0, 0, 29, 29);
panel[j].add(face[j]); ID[j] = DefaultComponentFactory.getInstance().createLabel("ID");
ID[j].setBounds(31, 6, 63, 17);
panel[j].add(ID[j]);
}

解决方案 »

  1.   

    没法正常显示是怎么个不正常啊?
    是不是只能显示一个?
    我认为你的frame和panel都应该设置布局而且你这个列表的设计并不是太好,有现成的jlist你为什么不用呢
      

  2.   

    panel[j] = new JPanel();
    panel[j].setLayout(null); // 注意, 这里设置的是panel[j]的Layout为null, 而不是设置frame.getContentPane()取得的content pane的Layout为null, 而你是把panel[j]加到frame的content pane中去, content pane是容器, 所以应该设置 content pane的Layout为null.
    panel[j].setBounds(25, 21*j+21, 94, 29);
    frame.getContentPane().add(panel[j]); 
      

  3.   

    content pane的Layout已经设置为null了,还是不顶用
    二楼的能不能给个jlist的教程,我刚刚开始学swt
      

  4.   

    我发给你一个eclipse官方网站提供的swt上面的list的例子代码,自己研究
    /*******************************************************************************
     * Copyright (c) 2000, 2005 IBM Corporation and others.
     * All rights reserved. This program and the accompanying materials
     * are made available under the terms of the Eclipse Public License v1.0
     * which accompanies this distribution, and is available at
     * http://www.eclipse.org/legal/epl-v10.html
     *
     * Contributors:
     *     IBM Corporation - initial API and implementation
     *******************************************************************************/
    package org.eclipse.swt.examples.controlexample;
    import org.eclipse.swt.*;
    import org.eclipse.swt.widgets.*;
    import org.eclipse.swt.layout.*;class ListTab extends ScrollableTab { /* Example widgets and groups that contain them */
    List list1;
    Group listGroup;

    static String [] ListData1 = {ControlExample.getResourceString("ListData1_0"),
      ControlExample.getResourceString("ListData1_1"),
      ControlExample.getResourceString("ListData1_2"),
      ControlExample.getResourceString("ListData1_3"),
      ControlExample.getResourceString("ListData1_4"),
      ControlExample.getResourceString("ListData1_5"),
      ControlExample.getResourceString("ListData1_6"),
      ControlExample.getResourceString("ListData1_7"),
      ControlExample.getResourceString("ListData1_8")}; /**
     * Creates the Tab within a given instance of ControlExample.
     */
    ListTab(ControlExample instance) {
    super(instance);
    }

    /**
     * Creates the "Example" group.
     */
    void createExampleGroup () {
    super.createExampleGroup ();

    /* Create a group for the list */
    listGroup = new Group (exampleGroup, SWT.NONE);
    listGroup.setLayout (new GridLayout ());
    listGroup.setLayoutData (new GridData (SWT.FILL, SWT.FILL, true, true));
    listGroup.setText ("List");
    }

    /**
     * Creates the "Example" widgets.
     */
    void createExampleWidgets () {

    /* Compute the widget style */
    int style = getDefaultStyle();
    if (singleButton.getSelection ()) style |= SWT.SINGLE;
    if (multiButton.getSelection ()) style |= SWT.MULTI;
    if (horizontalButton.getSelection ()) style |= SWT.H_SCROLL;
    if (verticalButton.getSelection ()) style |= SWT.V_SCROLL;
    if (borderButton.getSelection ()) style |= SWT.BORDER;

    /* Create the example widgets */
    list1 = new List (listGroup, style);
    list1.setItems (ListData1);
    }

    /**
     * Gets the "Example" widget children.
     */
    Widget [] getExampleWidgets () {
    return new Widget [] {list1};
    }

    /**
     * Returns a list of set/get API method names (without the set/get prefix)
     * that can be used to set/get values in the example control(s).
     */
    String[] getMethodNames() {
    return new String[] {"Items", "Selection", "ToolTipText", "TopIndex"};
    } /**
     * Gets the text for the tab folder item.
     */
    String getTabText () {
    return "List";
    }
    }
      

  5.   

    在eclipse的help--->welcome-->samples里面可以下载到所有swt的实例教程代码