//单击可以在文本中显示,双击就只能显示一下,不能一直显示,请问是什么问题?
import java.awt.*;
import java.awt.event.*;
public class test10{
public static void main(String args[])
{
MyFrame f = new MyFrame();
f.setBounds(100,100,200,300);
f.setVisible(true);
f.validate();
f.addWindowListener(new WindowAdapter()
{ public void WindowClosing(WindowEvent e)
  {
System.exit(0);
  }
}
);
}
}
 class MyFrame extends Frame implements ItemListener,ActionListener
{
List lst1;//列表框
TextArea area;
int count=1;
public MyFrame()
{
area=new TextArea(6,25);
lst1=new List(2,false);

lst1.add("苹果");
lst1.add("香蕉");
lst1.add("荔枝");
lst1.add("雪梨");

add(lst1,BorderLayout.NORTH); 
add(area,BorderLayout.CENTER);


lst1.addItemListener(this);
lst1.addActionListener(this);
}
public void itemStateChanged(ItemEvent e)
{
if(lst1.getSelectedIndex()==0)
{
area.setText("苹果");
}
else if(lst1.getSelectedIndex()==1)
{
area.setText("香蕉");
}
else if(lst1.getSelectedIndex()==2)
{
area.setText("荔枝");
}
else if(lst1.getSelectedIndex()==3)
{
area.setText("雪梨");
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==lst1)
if(lst1.getSelectedIndex()==0)
{
area.setText("\n"+lst1.getSelectedItem()+":红富士来至山东");
}
else if(lst1.getSelectedIndex()==1)
{
area.setText("\n"+lst1.getSelectedItem()+":海南香蕉来至海南");
}
else if(lst1.getSelectedIndex()==2)
{
area.setText("\n"+lst1.getSelectedItem()+":水晶荔枝来至广西");
}
else if(lst1.getSelectedIndex()==3)
{
area.setText("\n"+lst1.getSelectedItem()+":香甜雪梨来至四川");
}

}

}

解决方案 »

  1.   

    原因是双击后,又执行了一遍itemStateChanged
      

  2.   

    原因是双击后,又执行了一遍itemStateChanged
      

  3.   

    解决方法找到了,要添加一个计数来判断选哪个,count
    import java.awt.*;
    import java.awt.event.*;
    public class test10{
    public static void main(String args[])
    {
    MyFrame f = new MyFrame();
    f.setBounds(100,100,200,300);
    f.setVisible(true);
    f.validate();
    f.addWindowListener(new WindowAdapter()
    { public void WindowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    }
    );
    }
    }
     class MyFrame extends Frame implements ItemListener,ActionListener
    {
    List lst1;//列表框
    TextArea area;
    int count=1;
    public MyFrame()
    {
    area=new TextArea(6,25);
    lst1=new List(2,false);lst1.add("苹果");
    lst1.add("香蕉");
    lst1.add("荔枝");
    lst1.add("雪梨");add(lst1,BorderLayout.NORTH);  
    add(area,BorderLayout.CENTER);
    lst1.addItemListener(this);
    lst1.addActionListener(this);
    }
    public void itemStateChanged(ItemEvent e)
    {
    if(count==1)//添加
    {
    if(lst1.getSelectedIndex()==0)
    {
    area.setText("苹果"); 
    }
    else if(lst1.getSelectedIndex()==1)
    {
    area.setText("香蕉");
    }
    else if(lst1.getSelectedIndex()==2)
    {
    area.setText("荔枝");
    }
    else if(lst1.getSelectedIndex()==3)
    {
    area.setText("雪梨");
    }
    }
    else
    count=1;
    }
    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==lst1)
    if(lst1.getSelectedIndex()==0)
    {
    area.setText("\n"+lst1.getSelectedItem()+":红富士来至山东");
    }
    else if(lst1.getSelectedIndex()==1)
    {
    area.setText("\n"+lst1.getSelectedItem()+":海南香蕉来至海南");
    }
    else if(lst1.getSelectedIndex()==2)
    {
    area.setText("\n"+lst1.getSelectedItem()+":水晶荔枝来至广西");
    }
    else if(lst1.getSelectedIndex()==3)
    {
    area.setText("\n"+lst1.getSelectedItem()+":香甜雪梨来至四川");
    }}
    count++;
    }