日期:2014-05-20  浏览次数:20648 次

如何向Jlist里面的元素注册ActionListener
以下是我的代码,我想在JList中的每个元素加入ActionListener,但是不知道如何表示每个元素,谢谢
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.text.Position;

public class AddressBook extends JApplet implements ActionListener {
String[] ButtonName = { "Search", "Add", "Amend", "Delete" };
String Reply;
String Name;
String Address;
String Mobile;
String Home;
JButton[] FunctionButtons = new JButton[4];
static String[] Data = new String[250];
JList List = new JList(Data);
JScrollPane ScrollPane = new JScrollPane(List);
static String[] Detail = new String[1000];
JList ListPersonData = new JList();
JScrollPane ScrollPane2 = new JScrollPane(ListPersonData);
JButton ButtonListener;
String StringListener;
WriteData Wd=new WriteData();
ReadData Rd=new ReadData();

public AddressBook() {
setLayout(null);

Font Font1=new Font("monospaced",Font.PLAIN,30);
setFont(Font1);

for (int i : range(0, 3, 1)) {
FunctionButtons[i] = new JButton(ButtonName[i]);
add(FunctionButtons[i]);
FunctionButtons[i].setSize(100, 40);
FunctionButtons[i].setLocation(160, 170 + 40 * i);
FunctionButtons[i].setBackground(Color.pink);
FunctionButtons[i].addActionListener(this);
}

add(ScrollPane2);
ScrollPane2.setSize(250, 150);
ScrollPane2.setLocation(10, 10);

add(ScrollPane);
ScrollPane.setSize(150, 160);
ScrollPane.setLocation(10, 170);
ScrollPane.setBackground(Color.red);
List.setFixedCellHeight(20);
}
public void init() {
Rd.readData();
resize(270, 350);

}

public int[] range(int first, int last, int step) {
int[] src = new int[(last - first) / step + 1];
for (int i = 0; i < src.length; i++) {
src[i] = first + step * i;
}
return src;

}

@Override
public void actionPerformed(ActionEvent e) {
ButtonListener = (JButton) e.getSource();
add();

}

public void search() {
if (ButtonListener == FunctionButtons[0]) {
Reply = JOptionPane
.showInputDialog("Please enter the name you want to search");
for (int n = 0; n < Data.length; n++) {
if (Reply.length() <= Data[n].length()) {
if (Reply == Data[n].substring(0, Reply.length())) {
}
}
}
}
}
public void add(){
if (ButtonListener == FunctionButtons[1]) {
Name= JOptionPane
.showInputDialog("Please enter the name you want to add");
Address=JOptionPane
.showInputDialog("Please enter the address you want to add");
Mobile=JOptionPane
.showInputDialog("Please enter the mobile number you want to add");
Home=JOptionPane
.showInputDialog("Please enter the home number you want to add");
Wd.writeDataToFiles(Name, Address, Mobile, Home, "Address.txt");
setVisible(false);
Rd.readData();
setVisible(true);
}

}
public void amend(){

}
public void delete(){

}
}

------解决方案--------------------
http://www.blogjava.net/zeyuphoenix/archive/2010/05/03/319976.html
楼主自己参考一下
------解决方案--------------------
Java code

不是ActionListener 

下面的片段。。你自己加入到你的程序中。。调试一下逻辑。。。

        final JList list = 这里我省略赋值..;
        list.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                // TODO Auto-generated method stub
                String ss = list.getSelectedValue().toString(); // ss是获取出的每次选中的list中的值,,根据此值查询出详细数据就能取得LZ想要的。。 
            }
        });