8

你能帮我吗,如何改变JList中显示的项目的颜色?

我正在制作一个用户 JList,我可以在其中看到在线和离线用户,并且我需要离线用户具有与在线用户不同的颜色。

我创建用户的代码

final String [] strings=database.getUsers(myLogin);  

jList1.setModel(new javax.swing.AbstractListModel() {
     @Override
     public int getSize() { return strings.length; }
     @Override
     public Object getElementAt(int i) { return strings[i]; }
});
4

2 回答 2

8

我希望这段代码能完全帮助你

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.EventQueue;
    import java.util.Vector;

   import javax.swing.DefaultListCellRenderer;
   import javax.swing.JFrame;
   import javax.swing.JList;
   import javax.swing.JScrollPane;

   public class UserList {

 public static void main(String[] args) {
      EventQueue.invokeLater(new Runnable() {

           @Override
           public void run() {
                JFrame f = new JFrame("Users");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setSize(300, 300);

                JList list = new JList(new Vector<User>() {
                     {
                          add(new User("A", false));
                          add(new User("B", true));
                          add(new User("C", true));
                          add(new User("D", false));
                     }
                });

                list.setCellRenderer(new DefaultListCellRenderer() {

                     @Override
                     public Component getListCellRendererComponent(JList list, Object value, int index,
                               boolean isSelected, boolean cellHasFocus) {
                          Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                          if (value instanceof User) {
                               User nextUser = (User) value;
                               setText(nextUser.name);
                               if (nextUser.loggedIn) {
                                    setBackground(Color.GREEN);
                               } else {
                                    setBackground(Color.RED);
                               }
                               if (isSelected) {
                                    setBackground(getBackground().darker());
                               }
                          } else {
                               setText("whodat?");
                          }
                          return c;
                     }

                });
                f.add(new JScrollPane(list), BorderLayout.CENTER);
                f.setVisible(true);
           }
      });
 }

 static class User {
      String name = "NN";
      boolean loggedIn = false;

      public User(String name, boolean loggedIn) {
           this.name = name;
           this.loggedIn = loggedIn;
      }
 }

}

于 2015-10-11T11:49:51.120 回答
7
于 2012-04-20T18:03:48.957 回答