如何将不同的侦听器绑定到不同的ScrollPane

时间:2014-05-06 06:56:45

标签: java swing user-interface listener jscrollpane

我有一个类,它定义了一个带有两个JList的图形界面。对于每个JList,我想关联一个方法public void valueChanged(ListSelectionEvent s)但我无法做到(显然)。那么我该如何解决呢?

这是我的代码:

public class Inbox implements ListSelectionListener {

    public static void main(String[] args) {
        Inbox inbox = new Inbox();
    }

    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    JPanel titlePanel = new JPanel();
    JPanel centerPanel = new JPanel();
    JPanel listPanel = new JPanel();
    JPanel listRicPanel = new JPanel();
    JPanel listInvPanel = new JPanel();
    JPanel messPanel = new JPanel();
    JPanel buttonPanel = new JPanel();

    GridBagConstraints c;
    JButton indietro = new JButton("Indietro");
    JButton ok = new JButton("Ok");

    String ricevuti[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9"};
    JList ricList = new JList(ricevuti);

    String inviati[] = {"A", "B", "C", "D", "E"};
    JList invList = new JList(inviati);

    Inbox() {
        frame.setTitle("Inbox"); //nome della finestra (frame)
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
       frame.setVisible(true);

        panel.setLayout(new BorderLayout());
        centerPanel.setLayout(new BorderLayout());
        listPanel.setLayout(new GridLayout(2, 0));

        panel.add(titlePanel, BorderLayout.NORTH);
        panel.add(buttonPanel, BorderLayout.SOUTH);
        panel.add(centerPanel, BorderLayout.CENTER);
        buttonPanel.add(indietro);
        buttonPanel.add(ok);

        centerPanel.add(listPanel, BorderLayout.WEST);
        centerPanel.add(messPanel, BorderLayout.CENTER);

        listPanel.add(listRicPanel);
        listPanel.add(listInvPanel);

        /** ScrollPane containing the first list **/
        ricList.setVisibleRowCount(10);
        ricList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        listRicPanel.add(new JScrollPane(ricList));
        ricList.addListSelectionListener(this);

        /** ScrollPane containing the second list **/
        invList.setVisibleRowCount(10);
        invList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        listInvPanel.add(new JScrollPane(invList));
        invList.addListSelectionListener(this);


        frame.pack();

    }


    /**
     * First listener.
     */
    public void valueChanged(ListSelectionEvent e) {
        String str = (String) ricList.getSelectedValue();
        if(str.equals("Alex"))
            System.out.println("Alex");
        else if(str.equals("John"))
            System.out.println("John");
        else
            System.out.println("Other name");
    }

    /**
     * Second listener.
     */
    public void valueChanged(ListSelectionEvent e) {
        String str = (String) invList.getSelectedValue();
        if(str.equals("Alex"))
            System.out.println("Alex");
        else if(str.equals("John"))
            System.out.println("John");
        else
            System.out.println("Other name");
    }

}

我希望该计划能够将不同的听众组合到不同的scroolPane,就像我那样做?

非常感谢!

2 个答案:

答案 0 :(得分:2)

它必须是一个方法,但您可以使用e.getSource()方法获取触发事件的JList实例。然后就可以像这样使用它了

public void valueChanged(ListSelectionEvent e) {
    String str = (String) ((JList)e.getSource()).getSelectedValue();
    if(str.equals("Alex"))
        System.out.println("Alex");
    else if(str.equals("John"))
        System.out.println("John");
    else
        System.out.println("Other name");
}

当然必须将侦听器添加到两个列表实例

答案 1 :(得分:1)

你可以

确定事件的来源并根据结果分支您的逻辑......

public void valueChanged(ListSelectionEvent e) {
    if (e.getSource() == ricList) {
        //...
    else if (e.getSource() == invList) {
        //...
    }
}

这假定您要为每个源执行不同的逻辑。如果您有多个可能的来源,这可能会变得有点麻烦......

而不是"实施" ListSelectionListener在班级......

你可以......

定义一个完全外部的类来完成你想要的特定工作......

public class MyListSelectionListener implements ListSelectionListener {
    public void valueChanged(ListSelectionEvent s) {
        // Custom job here...
    }
}

根据您的需要,这可能是好事和坏事......有一个问题是您可能需要为课程提供额外的背景,以便它能够执行它的操作,因为事件对象可能无法单独提供足够的上下文

你可以

创建执行特定工作的内部类,特别是对于他们已在其中定义的类...

public class Inbox {
    //...
    Inbox() {
        //...
        ricList.addListSelectionListener(new RicListSelectionListener());
        //...
        invList.addListSelectionListener(new InvListSelectionListener());
    }
    //...
    public class RicListSelectionListener implements ListSelectionListener {
        public void valueChanged(ListSelectionEvent s) {
            String str = (String) ricList.getSelectedValue();
            if(str.equals("Alex"))
                System.out.println("Alex");
            else if(str.equals("John"))
                System.out.println("John");
            else
                System.out.println("Other name");            
        } 
    }

    public class InvListSelectionListener implements ListSelectionListener {
        public void valueChanged(ListSelectionEvent s) {
            String str = (String) invList.getSelectedValue();
            if(str.equals("Alex"))
                System.out.println("Alex");
            else if(str.equals("John"))
                System.out.println("John");
            else
                System.out.println("Other name");            
        } 
    }
}

关于这一点的好处是类可以访问父类中的所有实例变量和方法,并且整洁,并且在您需要时很容易找到...

请查看Inner Class Example了解详情

你可以......

使用匿名类。这些有点像内部类,除了它们在行中定义...

public class Inbox {
    //...
    Inbox() {
        //...
        ricList.addListSelectionListener(new ListSelectionListener {
            public void valueChanged(ListSelectionEvent s) {
                String str = (String) ricList.getSelectedValue();
                if(str.equals("Alex"))
                    System.out.println("Alex");
                else if(str.equals("John"))
                    System.out.println("John");
                else
                    System.out.println("Other name");            
            } 
        });
        //...
        invList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent s) {
                String str = (String) invList.getSelectedValue();
                if(str.equals("Alex"))
                    System.out.println("Alex");
                else if(str.equals("John"))
                    System.out.println("John");
                else
                    System.out.println("Other name");            
            } 
        });
    }
}

如果代码太长而且应该谨慎使用这些代码会使代码变得混乱,但这对于将现有功能整合在一起非常有用。

请查看Anonymous Classes了解详情