Java 将jlist与自定义对象一起使用时遇到问题

Java 将jlist与自定义对象一起使用时遇到问题,java,swing,jlist,listcellrenderer,Java,Swing,Jlist,Listcellrenderer,我设置了一个JList来使用SamplePerson对象的ArrayList的内容。每个SamplePerson在创建时自动创建一个随机名称。我试图让JList在JList中显示所选人员的姓名,但是列表显示为空。Oracle教程有一些帮助,但我仍然不知道如何让它显示出来 package main; import java.awt.Component; class SamplePerson{ private String firstName, lastname; private static

我设置了一个JList来使用SamplePerson对象的ArrayList的内容。每个SamplePerson在创建时自动创建一个随机名称。我试图让JList在JList中显示所选人员的姓名,但是列表显示为空。Oracle教程有一些帮助,但我仍然不知道如何让它显示出来

package main;

import java.awt.Component;

class SamplePerson{
private String firstName, lastname;
private static final String[] firstNameChoices = {"Mark", "Eric", "Henry", "Carl", "Watson"};
private static final String[] lastNameChoices = {"Smith", "Castle", "Baldwin", "Anderson"};

// A bunch of other fields that would go here are omitted for clarity.

String personDescription;
Random rand = new Random();

public SamplePerson() {
    initalizeName();
    personDescription = "This is a placeholder";

    // Other fields are initialized here.
}

private void initalizeName(){
    firstName = firstNameChoices[rand.nextInt(firstNameChoices.length)];
    lastname = lastNameChoices[rand.nextInt(lastNameChoices.length)];
}

public String getFullName() {
    return firstName +" " + lastname;
}

public String getFullNameLastNameFirst() {
    return lastname + ", " + firstName;
}

public String getPersonDescription() {
    return personDescription;
}
}

public class ListAppTest implements ListSelectionListener{

private JFrame frame;
private JTextPane textPane;
private JList<SamplePerson> list;

private ArrayList<SamplePerson> arrayListOfPeople;
private ListModel<SamplePerson> listMod;
private ListCellRenderer<SamplePerson> listRender;
private JLabel lblListOfPeople;
private JLabel lblDescription;

private String description;
/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                ListAppTest window = new ListAppTest();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public ListAppTest() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {

    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    listRender = new ListCellRenderer<SamplePerson>() {

        @Override
        public Component getListCellRendererComponent(
                JList<? extends SamplePerson> list, SamplePerson value, int index,
                boolean isSelected, boolean cellHasFocus) {

            JLabel thisLabel = new JLabel(value.getFullName());
            thisLabel.setBounds(0, 0, 61, 16);
            return thisLabel;
        }
    };

    arrayListOfPeople = new ArrayList<SamplePerson>();

    addToArrayListOfPeople(6);

    listMod = new DefaultListModel<SamplePerson>();

    list = new JList<SamplePerson>(listMod);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setSelectedIndex(1);
    list.setCellRenderer(listRender);

    list.setBounds(30, 30, 120, 220);
    list.addListSelectionListener(this);

    frame.getContentPane().add(list);
    if(list.getSelectedIndex() != -1){
        description = list.getSelectedValue().getPersonDescription();
    }else{
        description = "";
    }

    textPane = new JTextPane();
    textPane.setText(description);
    textPane.setEditable(false);
    textPane.setBounds(230, 50, 160, 170);

    frame.getContentPane().add(textPane);

    lblListOfPeople = new JLabel("List of People");
    lblListOfPeople.setBounds(30, 22, 90, 16);
    frame.getContentPane().add(lblListOfPeople);

    lblDescription = new JLabel("Description");
    lblDescription.setBounds(230, 22, 80, 16);
    frame.getContentPane().add(lblDescription);

}

private void addToArrayListOfPeople(int peopleToAdd){
    for (int i = 0; i < peopleToAdd; i++) {
        arrayListOfPeople.add(new SamplePerson());
    }
}

@Override
public void valueChanged(ListSelectionEvent e) {
    description = list.getSelectedValue().getPersonDescription();
    textPane.setText(description);
}
}
packagemain;
导入java.awt.Component;
类抽样人员{
私有字符串firstName,lastname;
私有静态最终字符串[]firstnameSchoices={“Mark”、“Eric”、“Henry”、“Carl”、“Watson”};
私有静态最终字符串[]lastNameChoices={“Smith”、“Castle”、“Baldwin”、“Anderson”};
//为了清晰起见,这里省略了一组其他字段。
字符串描述;
Random rand=新的Random();
公众抽样人员(){
initalizeName();
personDescription=“这是一个占位符”;
//其他字段在此处初始化。
}
私有void initializeName(){
firstName=firstNameChoices[rand.nextInt(firstNameChoices.length)];
lastname=lastNameChoices[rand.nextInt(lastNameChoices.length)];
}
公共字符串getFullName(){
返回firstName+“”+lastname;
}
公共字符串getFullNameLastNameFirst(){
返回lastname+“,”+firstName;
}
公共字符串getPersonDescription(){
返回人员描述;
}
}
公共类ListAppTest实现ListSelectionListener{
私有JFrame;
私有JTextPane textPane;
私人名单;
私人ArrayList arrayListOfPeople;
私有ListModel listMod;
私有ListCellRender;
私人JLabel-lbl-people;
私人JLabel LBL描述;
私有字符串描述;
/**
*启动应用程序。
*/
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
试一试{
ListAppTest窗口=新建ListAppTest();
window.frame.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
/**
*创建应用程序。
*/
公共ListAppTest(){
初始化();
}
/**
*初始化框架的内容。
*/
私有void初始化(){
frame=新的JFrame();
机架立根(100450300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
listRender=新ListCellRenderer(){
@凌驾
公共组件GetListCellRenderComponent(

JList您从不向
列表模型添加任何内容

addToArrayListOfPeople(6);

listMod = new DefaultListModel<SamplePerson>();
您应该避免使用
null
版面,因为您无法控制字体大小、DPI、屏幕分辨率、呈现管道或其他可能影响字体呈现方式的因素的差异。
null
版面将使您的工作量增加10倍,并降低应用程序的可移植性。您的工作量也将减少Swing的“古怪”设计用于合适的布局管理器

您还应该将
JList
添加到
JScrollPane

看看:


    • 您从未向
      列表模型添加任何内容

      addToArrayListOfPeople(6);
      
      listMod = new DefaultListModel<SamplePerson>();
      
      您应该避免使用
      null
      布局,因为您无法控制字体大小、DPI、屏幕分辨率、呈现管道或其他可能影响字体呈现方式的因素的差异。
      null
      布局将使您的工作量增加10倍,并降低应用程序的可移植性。您的“古怪”作为Swing设计用于合适的布局管理器

      您还应该将
      JList
      添加到
      JScrollPane

      看看:


        • 您从未向
          列表模型添加任何内容

          addToArrayListOfPeople(6);
          
          listMod = new DefaultListModel<SamplePerson>();
          
          您应该避免使用
          null
          布局,因为您无法控制字体大小、DPI、屏幕分辨率、呈现管道或其他可能影响字体呈现方式的因素的差异。
          null
          布局将使您的工作量增加10倍,并降低应用程序的可移植性。您的“古怪”作为Swing设计用于合适的布局管理器

          您还应该将
          JList
          添加到
          JScrollPane

          看看:


            • 您从未向
              列表模型添加任何内容

              addToArrayListOfPeople(6);
              
              listMod = new DefaultListModel<SamplePerson>();
              
              您应该避免使用
              null
              布局,因为您无法控制字体大小、DPI、屏幕分辨率、呈现管道或其他可能影响字体呈现方式的因素的差异。
              null
              布局将使您的工作量增加10倍,并降低应用程序的可移植性。您的“古怪”作为Swing设计用于合适的布局管理器

              您还应该将
              JList
              添加到
              JScrollPane

              看看:


              您不应该在
              GetListCellRenderComponent
              方法内创建渲染器组件。声明是否在方法外,并在方法内更改其状态。您不应该在
              GetListCellRenderComponent
              方法内创建渲染器组件。如果在方法外,则声明并仅更改在方法内更改它的状态。不应在
              GetListCellRenderComponent
              方法内创建渲染器组件。声明是否在方法外,只需在方法内更改它的状态。不应在
              GetListCellRenderComponent
              方法内创建渲染器组件。声明是否为outsid方法的一部分,只需在方法内部更改它的状态。感谢您的帮助。空布局仅适用于我测试某个东西或尝试使其工作时,我个人更喜欢使用springLayout(或cardLayout,当