gpt4 book ai didi

Java:从本地 JList 添加项目到 JList 时遇到问题

转载 作者:行者123 更新时间:2023-12-01 14:32:50 25 4
gpt4 key购买 nike

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

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 对象。请参阅Writing a Custom Cell Renderer

<小时/>
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;
}
}

关于Java:从本地 JList 添加项目到 JList 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16720355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com