Java:在从本地JList向JList添加项时遇到麻烦



我在这里遇到了一个问题,我创建了一个actionListener,旨在创建一个随机的人,并将其添加到JList中,以显示在JScrollPane上。到目前为止,一切都很顺利,只是每当我单击JButton添加新人员时,JList不会添加到当前列表中,而是每次都替换它,因此JList上只显示一个项目。我知道问题发生在哪里,您将立即在动作事件行中看到它。无论如何,感谢任何帮助我的朋友!

private static final JTextArea PlayerList = new JTextArea(30, 100);
private JList newbie;
private List<Human> members = new ArrayList<Human>();
private JTextArea Area;
private String[] listString;
private String[] newString;
private ArrayList<String> list = new ArrayList<String>();
private ArrayList<String> zz = new ArrayList<String>();

public JTabbedPaneFrame() throws FileNotFoundException 
{
    super("JTabbedPane Demo");

    JTabbedPane tabbedPane = new JTabbedPane(); 
    JPanel Gladiator = new JPanel();
    getContentPane().add(Gladiator); 
    /////////////Tabbed Pane Gladiator///////////////////
    tabbedPane.addTab("Gladiator", null, Gladiator, "");


    Box ListOfPlayers = Box.createVerticalBox();
    ListOfPlayers.add(Box.createRigidArea(new Dimension(100,100)));
    ListOfPlayers.setBorder(new TitledBorder("List of Players"));
    Area = new JTextArea(20, 15);
    Area.setLineWrap(true);
    Area.setWrapStyleWord(false);
    final JList newbie = new JList();
    JScrollPane PlayerViewer = new JScrollPane(newbie);
    PlayerViewer.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    ListOfPlayers.add(PlayerViewer);

    Gladiator.add(ListOfPlayers);
    /////////////Vertical Box between Text and Tabbed Profile//////
    Box Randomer = Box.createVerticalBox();
    Randomer.setBorder(new TitledBorder("Randomize or Add Gladiator"));
    JButton AddIndividual = new JButton("Add a Player");
    Randomer.add(AddIndividual); 
    Gladiator.add(Randomer);

    AddIndividual.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent event)
        {

                String x = "";
                String y = "";
                String z = "";
                String ee = "";
                ArrayList<String> listx = new ArrayList<String>();
                ArrayList<String> zzx = new ArrayList<String>();
                JList newbiex;
                Human temp;
                try {

                    temp = new Human();
                    x = temp.toString();
                    y = temp.getSurname();
                    z = temp.getFirstName();
                    listx.add(x);
                    ee = String.format(y + ", " + z );
                    zzx.add(ee);
                    listString = new String[zzx.size()];
                    listString = zzx.toArray(listString);
                    newbiex = new JList(zzx.toArray());
                    newbie.setModel(newbiex.getModel());
                    members.add(temp);

                    for(String W: listString) /////testing diff method here////
                    {
                          Area.append(W);
                    }
                    } 
                catch (FileNotFoundException e) 
                    {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    }


        }

    });


    add(tabbedPane); 

    /////////////Action Buttons////////////////////

}
}       

类HumanRenderer扩展DefaultListCellRenderer

    {
       public Component getListCellRendererComponent(JList list, Object value,
          int index, boolean isSelected, boolean cellHasFocus) {
          JLabel label = new JLabel(); 
          if (value != null) {
             Human human = (Human) value;
             label.setText(human.getSurname() + ", " + human.getFirstName());
          }
          return label;
       }
    }

您正在ActionListener中创建一个新的JList newbiex,您从未将其添加到JFrame中。相反,重用原来的JList newbie,并将新的Human添加到其模型中。使用可变的DefaultListModel

DefaultListModel<Human> model = new DefaultListModel<>();
JList<Human> newbie = new JList<>(model);

您将需要一个自定义单元渲染器来在JList中显示Human对象。参见编写自定义单元格渲染器


class HumanRenderer extends DefaultListRenderer {
   @Override
   public Component getListCellRendererComponent(JList list, Object value,
      int index, boolean isSelected, boolean cellHasFocus) {
      JLabel label = new JLabel(); 
      if (value != null) {
         Human human = (Human) value;
         label.setText(human.getSurName() + ", " + human.getFirstName());
      }
      return label;
   }
}

你在这里做了很多奇怪的事情。

似乎每次单击都创建一个JList,在其中添加一个Human,并将初始模型的列表设置为新创建的列表的模型。

为了简化,您可以生成一个包含最后添加的Human的新模型,并将其用作列表的新模型。

唯一要做的就是将您的新Human添加到现有模型中,因此您必须保留对该模型的引用。

您可以在Java教程中找到更多细节:http://docs.oracle.com/javase/tutorial/uiswing/components/list.html

最新更新