我正在为组件设计课程实施一个程序,目前正在努力解决应该简单使用 Jlist 的问题。程序绘制形状并应将它们显示在框架的 BorderLayout.West 中的 ScrollPane 中的 JList 中。
当前程序显示了 ScrollPane,但不会显示我添加到 shapeListModel 中的对象。我想知道我遗漏了什么,或者我的代码是否有问题。目前,这是参与 JList 创建、分配和更新的所有代码。
//Class Variable Declaration
protected ArrayList<Shape> shapes = new ArrayList<Shape>();
private JScrollPane shapeScrollPane = new JScrollPane();
private JList<String> shapeList;
protected DefaultListModel<String> shapeListModel;
//Creation of JList objects and what not
shapeListModel = new DefaultListModel<String>();
//This element is added aimply to try and get one to show up on my JList,
shapeListModel.addElement("SERIOUSLY FRUSTRATED");
//It does not just so you know
shapeList = new JList<String>(shapeListModel);
shapeList.setModel(shapeListModel);
//Adding JList to ScrollPane and setting size
shapeScrollPane.add(shapeList);
shapeScrollPane.setPreferredSize(new Dimension(250,600));
//Clarifying JList actions
shapeList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
shapeList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
shapeList.setVisibleRowCount(-1);
shapeList.addListSelectionListener(this);**
//This is called everytime a new shape is created and adds it to the Arraylist
//shapes and the DefaultListModel shapeListModel
shapes.add(newShape);
shapeListModel.addElement(newShape.toString());
我为我刚才格式错误的问题道歉。我已经坚持了大约 4 个小时,最后两个小时花在网上寻找答案。我现在要问任何人是否在我的代码中看到问题。