gpt4 book ai didi

java - 如何将不同的监听器绑定(bind)到不同的ScrollPane

转载 作者:行者123 更新时间:2023-12-01 23:00:09 25 4
gpt4 key购买 nike

我有一个类,它定义了带有两个 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,就像我这样做的那样?

非常感谢!

最佳答案

它必须是一种方法,但您可以使用 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");
}

当然,监听器必须添加到两个列表实例

关于java - 如何将不同的监听器绑定(bind)到不同的ScrollPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23487884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com