如何使用列表中的选定值?

How Can I Use Selected Values from List?

我目前已经创建了一个多间隔选择 JList,我希望能够对这些选择进行一些操作。目前,我的代码只能创建一个 JList,我可以在其中 "highlight" 选择对象;但是,我想要做的是将选定的对象放入 Array 或 ArrayList 中,以便我可以对这些对象执行一些操作。我在网上搜索并发现了 getSelectedValueList(),但我真的不知道如何使用和访问它以备后用。

我的另一个问题是,我正在考虑创建一个名为 "Submit" 的 JButton,但我想知道我应该如何处理带有 JList 的 window。我应该在按下按钮后让它退出吗?

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    import java.util.List;

    public class ClassSelection extends JFrame {
         private JList classList;
         private String[] classNames = {"Programming Foundations I", 
         "Programming Foundations II",
         "Digital Design", "Computer Organization", "Programming 
         Paradigms", 
         "Honors Programming Paradigms", "Cluster Computing", 
         "Software Engineering"};

     public ClassSelection() {
         super("Classes Taken:");

         Container container = getContentPane();
         container.setLayout(new FlowLayout());

         classList = new JList(classNames);
         classList.setVisibleRowCount(5);
         classList.setFixedCellHeight(45);
         classList.setFixedCellWidth(450);

classList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTIO N);

         container.add(new JScrollPane(classList));
         setSize(600, 600);
         setVisible(true);

    }

    public static void main(String[] args) {
         ClassSelection application = new ClassSelection();
    }
}

请参阅下面的代码。它包含您的要求。

你第二个问题的代码和你说的一样有效。

"Should I make it exit after the button is pressed?"

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

public class ClassSelection extends JFrame {
    private JList classList;
    private String[] classNames = {"Programming Foundations I",
            "Programming Foundations II",
            "Digital Design", "Computer Organization", "Programming Paradigms",
            "Honors Programming Paradigms", "Cluster Computing",
            "Software Engineering"};

    public ClassSelection() {
        super("Classes Taken:");

        Container container = getContentPane();
        container.setLayout(new FlowLayout());

        classList = new JList(classNames);
        classList.setVisibleRowCount(5);
        classList.setFixedCellHeight(45);
        classList.setFixedCellWidth(450);
        classList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        JScrollPane jScrollPane = new JScrollPane(classList);

        // First question
        List<String> myList = new ArrayList<>();

        classList.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                JList jList = (JList) e.getSource();
                String className = (String) jList.getSelectedValue();
                myList.add(className);
                System.out.println("clicked : " + className);

                // you can see the items being appended
                System.out.println("list : " + myList.toString());
            }
        });

        // Second question
        JButton btn = new JButton("Submit");
        btn.addActionListener(event -> {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            dispose();
        });

        container.add(jScrollPane);
        container.add(btn);
        setSize(600, 600);
        setVisible(true);
    }

    public static void main(String[] args) {
        ClassSelection application = new ClassSelection();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

您可以通过以下 2 种方式执行此操作。我个人认为"Approach 1"就够了。但是如果你真的想在用户按下按钮之前收集选定的项目,那么使用 "Approach 2".

方法 1 - 在按钮的动作侦听器中调用 getSelectedValuesList()

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.List;

public class ClassSelection extends JFrame {
  private JList classList;
  private String[] classNames = {"Programming Foundations I",
      "Programming Foundations II",
      "Digital Design", "Computer Organization",
      "Programming Paradigms",
      "Honors Programming Paradigms", "Cluster Computing",
      "Software Engineering"};

  public ClassSelection() {
    super("Classes Taken:");

    Container container = getContentPane();
    container.setLayout(new FlowLayout());

    classList = new JList(classNames);
    classList.setVisibleRowCount(5);
    classList.setFixedCellHeight(45);
    classList.setFixedCellWidth(450);

    classList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

    container.add(new JScrollPane(classList));

    JButton button = new JButton("Print selected items");
    button.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        List selectedItems = classList.getSelectedValuesList();
        System.out.println(selectedItems);
      }
    });
    container.add(button, BorderLayout.SOUTH);

    setSize(600, 600);
    setVisible(true);

  }

  public static void main(String[] args) {
    ClassSelection application = new ClassSelection();
  }
}

方法 2 - 使用 ListSelectionListener

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.util.*;
import java.util.List;

public class ClassSelection2 extends JFrame {
  private JList classList;
  private List<String> selectedItemsList = new ArrayList<>();

  private String[] classNames = {"Programming Foundations I",
      "Programming Foundations II",
      "Digital Design", "Computer Organization",
      "Programming Paradigms",
      "Honors Programming Paradigms", "Cluster Computing",
      "Software Engineering"};

  public ClassSelection2() {
    super("Classes Taken:");

    Container container = getContentPane();
    container.setLayout(new FlowLayout());

    classList = new JList(classNames);
    classList.setVisibleRowCount(5);
    classList.setFixedCellHeight(45);
    classList.setFixedCellWidth(450);

    classList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    classList.addListSelectionListener(new ListSelectionListener() {
      @Override
      public void valueChanged(ListSelectionEvent e) {
        if (!e.getValueIsAdjusting()) {
          selectedItemsList.clear();
          selectedItemsList.addAll(classList.getSelectedValuesList());
        }
      }
    });

    container.add(new JScrollPane(classList));

    JButton button = new JButton("Print selected items");
    button.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        System.out.println(selectedItemsList);
      }
    });
    container.add(button, BorderLayout.SOUTH);

    setSize(600, 600);
    setVisible(true);

  }

  public static void main(String[] args) {
    ClassSelection2 application = new ClassSelection2();
  }
}