该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
//JList事件响应:-----------------采用内隐类实现事件响应
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class TestJListAction {
JFrame frame;
Container contentPane;
JList list1;
JLabel label1;
String colorname[]={"red","blue","black","yellow"};
Color colors[]={Color.red,Color.blue,Color.black,Color.yellow};
public TestJListAction()
{
JFrame frame=new JFrame("测试列表框");
contentPane=frame.getContentPane();
contentPane.setLayout(new FlowLayout());
list1=new JList(colorname);
list1.setVisibleRowCount(3);
list1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
contentPane.add(new JScrollPane(list1));
label1=new JLabel("颜色");
contentPane.add(label1);
list1.addListSelectionListener
(
new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
label1.setText(list1.getSelectedValue().toString());
label1.setForeground(colors[list1.getSelectedIndex()]);
}
}
);
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
public static void main(String[] args) {
TestJListAction myframe=new TestJListAction();
}
}