- 论坛徽章:
- 0
|
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.event.*;//由于ListSelectionEvent是swing的事件,不是awt的事件,因此我们必须import
- //javax.swing.event.*。
- public class JList7 extends JFrame implements ListSelectionListener
- {
- JList list = null;
- JLabel label = null;
- String[] s = {"美国","日本","大陆","英国","法国","意大利","澳洲","韩国"};
-
- public JList7()
- {
- JFrame f = new JFrame("JList");
- Container contentPane = f.getContentPane();
- contentPane.setLayout(new BorderLayout());
- label = new JLabel();
-
- list = new JList(s);
- list.setVisibleRowCount(5);
- list.setBorder(BorderFactory.createTitledBorder("您最喜欢到哪个国家玩呢?"));
- list.addListSelectionListener(this);
-
- contentPane.add(label,BorderLayout.NORTH);
- contentPane.add(new JScrollPane(list),BorderLayout.CENTER);
- f.pack();
- f.show();
- f.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- }
-
- public static void main(String args[])
- {
- new JList7();
- }
-
- public void valueChanged(ListSelectionEvent e)
- {
- System.out.println("eeee");
- int tmp = 0;
- String stmp = "您目前选取:";
- int[] index = list.getSelectedIndices();//利用JList类所提供的getSelectedIndices()方法可得到用户所选取的所有
- for(int i=0; i < index.length ; i++)//index值,这些index值由一个int array返回.
- {
- tmp = index[i];
- stmp = stmp+s[tmp]+" ";
- }
- label.setText(stmp);
- }
- }
复制代码
请问为什么会输出两次"eeee",是说在JList上的监听要执行两次么?用什么方法可以只输出一次呢? |
|