简体   繁体   English

值未从JList传递到另一个JList

[英]Values not passing from a JList to another JList

Here's my code. 这是我的代码。 Why the values are not going into the second JList? 为什么值不进入第二个JList? Also, the second Jlist is not visible. 此外,第二个Jlist不可见。 All imports are in place and not visible in the following code: 所有导入均已就绪,在以下代码中不可见:

public class Gui extends JFrame {

    private JList l;
    private JList l2;
    private JButton b1;
    public String [] cd = {"Album a", "Album b", "Album c", "Album d","Album e", "Album f", "Album g", "Album h"}; 

    public Gui(){

    super("Mover");
    l = new JList (cd);
    l.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    b1 = new JButton("Move");

    b1.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    l2.setListData(l.getSelectedValuesList());
                }
            }
);


    setLayout(new FlowLayout());
    add(new JScrollPane(l));
    add(b1);
    add(new JScrollPane(l2));

}
}

Your JList l2 is null, You need to initialise it to before you can use it like you do with l 您的JList l2为null,您需要对其进行初始化,然后才能像使用l一样使用它

Add

l2 = new JList (/*your list2 or empty*/);

just after your initialisation of the JList l 在初始化JList l

Edit As I now see was mentioned in the comments 我现在看到的编辑在评论中被提及

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM