在netbeans框架中创建JList

时间:2011-11-25 09:08:45

标签: java swing jlist layout-manager

我正在Netbeans中创建一个Jlist,我已经编写了一个代码,但代码消失在我的Jlist中

/** Creates new form Toetsenbordd */
    public Toetsenbordd() {
       initComponents();
        initCustomComponents();
    }

    private void initCustomComponents(){
     JList Lijstje;
     JScrollPane Lijst; 

         String filename[]= {"Ard ","Astronauts",
            "Behind the Scene Movie credits","DNA Research -- the Human Genome Project",
            "Extract from The Adventures of Pinocchio","History of Photography",
            "Hubble Space Telescope","Legends of Abraham Lincoln","Netiquette",
            "Observations of the Father of Computing (1791 - 1871)","Rules of Baseball (from 1889)",
            "Speeding up the strategy process","Stinging Insects","The Eight Tools for Creating New Values",
            "The Life of Calamity Jane","The Little Match Girl (adapted)","The Tail of Peter Rabbit",
            "Thoughts of Banjamin Franklin","What is the Cast","Yosemite National Park"
            };
        {

          Lijstje = new JList(filename);
          Lijstje.setFont(new Font("Arial",Font.PLAIN,15));
        Lijst =new JScrollPane(Lijstje);
        Lijst.setSize(410,200);
        Lijst.setLocation(70,75);
        Lijstje.setVisibleRowCount(6);
        Lijstje.setSelectedIndex(0);
        Lijstje.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //seting the border for filescroll
        Lijst.setBorder(BorderFactory.createTitledBorder("CHOOSE TEST"));

        add(Lijst);

    }     



    }

它位于我自制的Jframe中间:

enter image description here

但我想在那里说项目1,2,3

1 个答案:

答案 0 :(得分:2)

首先请不要用大写字母开始变量名称,这是针对Java naming conventions的。

代码中的

setLocation表示您希望Lijst拥有绝对位置。只有在不使用LayoutManager时才能进行此操作,请参阅this tutorial。为此,您需要将布局管理器明确设置为null,因为BorderLayout是默认值:

setLayout(null)

但是不建议使用null布局管理器,你真的应该考虑使用list中的那个