For 循环未使用 Java 中的 ListSelectionListener 获取正确的用户选择

For loop not grabbing the correct user selection using ListSelectionListener in Java

我正在尝试在 ListSelectionListener 事件中使用 for 循环来构建要设置为 Jlabel 的字符串。它的工作原理是,它将构建字符串并且 JLabel 将更新但不会更新用户 selected 的项目。例如,我在程序中 select “冰岛”,但程序会 select 美国,因为它是第一个索引,然后如果我继续 select 其他索引,它会到索引 1、2 等而不是我选择的那些。这是代码。

public class guiMain {
    
    private static void constructGUI() {
        JFrame.setDefaultLookAndFeelDecorated(true);
        GUI frame = new GUI();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {            
              public void run() {                
                 constructGUI();           
                  }        
              });
        }
}
class MyActionListener implements ActionListener {
    GUI fr;
    public MyActionListener(GUI frame) {
        fr = frame;
    }
      public void actionPerformed(ActionEvent e) {
          JComboBox cb = (JComboBox) e.getSource();
          String selection = (String) cb.getItemAt(cb.getSelectedIndex());
          System.out.println(selection);
          if(selection.contentEquals("Multiple")) { //user choice of selecting "Single" or "Multiple" in JList
              fr.ctryJList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
          }
          else {
              fr.ctryJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
          }
      }
}

    class GUI extends JFrame {
        
          public JLabel userSelected;
          public JList ctryJList;
        
          public GUI() {
              super("The Country Combo");
              init();
          }
        
          private void init() {
              //array for country choices
              String[] countries = {"United States of America", "Italy", "England", "Iceland", "Netherlands", 
                    "Sweden", "Switzerland", "Canada", "New Zealand"};
              //array for choice of selecting one, or multiple countries
              String[] selection = {"Single", "Multiple"};
              
              this.setLayout(new BorderLayout());
              this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              JComboBox comboBox = new JComboBox(selection); //creation of ComboBox
              comboBox.addActionListener(new MyActionListener(this));
              userSelected = new JLabel("Selected items will appear here");
              ctryJList = new JList(countries); //creation of JList
              ctryJList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
                  @Override
                  public void valueChanged(ListSelectionEvent e){
                        ListSelectionModel lst = (ListSelectionModel) e.getSource();
                        int[] selectedIndices = ctryJList.getSelectedIndices();
                        String output = "Selected Items: ";
                        for(int i = 0; i < selectedIndices.length; i++) {
                            output = output + (String)ctryJList.getModel().getElementAt(i) + " ";
                            userSelected.setText(output);
                        }
                        System.out.println("Your countries are: " + output);
                  }
              });
              this.pack();
              JComponent panel = new JPanel();
              panel.setLayout(new FlowLayout());
              panel.add(new JLabel("Choose Selection Mode:"));
              panel.add(comboBox);
              this.getContentPane().add(panel,BorderLayout.NORTH);
              this.getContentPane().add(ctryJList,BorderLayout.WEST);
              this.getContentPane().add(userSelected, BorderLayout.SOUTH);
              this.pack();
              //TODO: Create JLabel for selection
          }
    }
output = output + (String)ctryJList.getModel().getElementAt(i) ...

您正在获取索引 0、1、2... 处的值,因为“i”始终从 0 开始。

您需要所选项目的索引,因此您需要从数组中获取索引:

output = output + (String)ctryJList.getModel().getElementAt(selectedindices[i]) ...

另外,不要使用字符串连接来构建输出字符串。而是使用 StringBufferStringBuilder 并使用 append(...) 方法。或者也许使用 StringJoiner.