题目:
设计一个窗口,在该窗口中添加一个List组件,该组件中有4门课程名称的选项。然后在窗口中添加一个文本区,当选择List组件中的某个选项后,文本区中显示对该课程的介绍,当双击List组件中的某个选项后,文本区显示该课程的开课时间。
这个题目需要您在自己电脑上面建立一些文档,麻烦你们高手同志啦!!!
这个程序我运行没有错误,但是出不了我理想的效果,很是头痛。
问题是这样的:单击还能出课程介绍,双击当鼠标没有弹起来时候也能出来开课时间,但是鼠标弹起来后却又回到课程介绍了,我希望是弹起来后还是开课时间不要消失,麻烦各位帮忙一下!!!我是个新手!!!
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class app34 extends Frame implements ItemListener,ActionListener
{
   static app34 frm=new app34();
   static List lst=new List();
   static TextArea ta=new TextArea(8,20);
  public static void main(String args[])
 {
    frm.setLayout(new FlowLayout(FlowLayout.CENTER,15,10));
    frm.setVisible(true);
    frm.add(lst);
    frm.add(ta);
    lst.add("no1");
    lst.add("no2");
    lst.add("no3");
    lst.add("no4");
    lst.addItemListener(frm);
    lst.addActionListener(frm);
    frm.addWindowListener(new wind());
 }
  public void itemStateChanged(ItemEvent e)
{   String str=((List)e.getSource()).getSelectedItem();
      
   if(str=="no1")
    {
      try 
      {
         FileInputStream fi=new FileInputStream("d:\\Java\\no1.txt");
         byte b[]=new byte[fi.available()];
          fi.read(b);
          ta.setText(new String(b));
          fi.close();
       }
      catch(IOException e1) 
             {};
     }
  else if(str=="no2")
    {
      try 
      {
         FileInputStream fi=new FileInputStream("d:\\Java\\no2.txt");
         byte b[]=new byte[fi.available()];
          fi.read(b);
          ta.setText(new String(b));
          fi.close();
       }
      catch(IOException e1) 
             {};
    }
   else if(str=="no3")
    {
      try 
      {
         FileInputStream fi=new FileInputStream("d:\\Java\\no3.txt");
         byte b[]=new byte[fi.available()];
          fi.read(b);
          ta.setText(new String(b));
          fi.close();
       }
      catch(IOException e1) 
             {};
    }
    else if(str=="no4")
    {
      try 
      {
         FileInputStream fi=new FileInputStream("d:\\Java\\no4.txt");
         byte b[]=new byte[fi.available()];
          fi.read(b);
          ta.setText(new String(b));
          fi.close();
       }
      catch(IOException e1) 
             {};
    }
}
 public void actionPerformed(ActionEvent e)
 {  String str1=((List)e.getSource()).getSelectedItem();
   if(str1=="no1")
    {
      try 
      {
         FileInputStream fi=new FileInputStream("d:\\Java\\no5.txt");
         byte c[]=new byte[fi.available()];
          fi.read(c);
          ta.append(new String(c));
          
       }
      catch(IOException e1) 
             {};
    } else  if(str1=="no2")
    {
      try 
      {
         FileInputStream fi=new FileInputStream("d:\\Java\\no6.txt");
         byte c[]=new byte[fi.available()];
          fi.read(c);
          ta.setText(new String(c));
          
       }
      catch(IOException e1) 
             {};
    }
 else  if(str1=="no3")
    {
      try 
      {
         FileInputStream fi=new FileInputStream("d:\\Java\\no7.txt");
         byte c[]=new byte[fi.available()];
          fi.read(c);
          ta.setText(new String(c));
          
       }
      catch(IOException e1) 
             {};
    }
 else  if(str1=="no4")
    {
      try 
      {
         FileInputStream fi=new FileInputStream("d:\\Java\\no8.txt");
         byte c[]=new byte[fi.available()];
          fi.read(c);
          ta.setText(new String(c));
          
       }
      catch(IOException e1) 
             {};
    }
 }static class wind extends WindowAdapter
{
     public void windowClosing(WindowEvent e)
 {
     frm.dispose();
 }
}
}

解决方案 »

  1.   

    为什么不用MouseListener来处理鼠标单击和双击
      

  2.   

    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    class app34 extends Frame implements ActionListener{
       static app34 frm=new app34();
       static List lst=new List();
       static TextArea ta=new TextArea(8,20);
      public static void main(String args[])
     {
        frm.setLayout(new FlowLayout(FlowLayout.CENTER,15,10));
        frm.setVisible(true);
        frm.add(lst);
        frm.add(ta);
        lst.add("no1");
        lst.add("no2");
        lst.add("no3");
        lst.add("no4");
        lst.addActionListener(this);//修改的地方
        frm.addWindowListener(new wind());
     }
      public void actionPerformed(ActionEvent e)
    { String str;  //修改的地方
      if(e.getClickCount()==2)//修改的地方
         str=lst.getItem(lst.getSelectIndex()).toString();//修改的地方
          
       if(str=="no1")
        {
          try 
          {
             FileInputStream fi=new FileInputStream("d:\\Java\\no1.txt");
             byte b[]=new byte[fi.available()];
              fi.read(b);
              ta.setText(new String(b));
              fi.close();
           }
          catch(IOException e1) 
                 {};
         }
      else if(str=="no2")
        {
          try 
          {
             FileInputStream fi=new FileInputStream("d:\\Java\\no2.txt");
             byte b[]=new byte[fi.available()];
              fi.read(b);
              ta.setText(new String(b));
              fi.close();
           }
          catch(IOException e1) 
                 {};
        }
       else if(str=="no3")
        {
          try 
          {
             FileInputStream fi=new FileInputStream("d:\\Java\\no3.txt");
             byte b[]=new byte[fi.available()];
              fi.read(b);
              ta.setText(new String(b));
              fi.close();
           }
          catch(IOException e1) 
                 {};
        }
        else if(str=="no4")
        {
          try 
          {
             FileInputStream fi=new FileInputStream("d:\\Java\\no4.txt");
             byte b[]=new byte[fi.available()];
              fi.read(b);
              ta.setText(new String(b));
              fi.close();
           }
          catch(IOException e1) 
                 {};
        }
    }
     public void actionPerformed(ActionEvent e)
     {  String str1=((List)e.getSource()).getSelectedItem();
       if(str1=="no1")
        {
          try 
          {
             FileInputStream fi=new FileInputStream("d:\\Java\\no5.txt");
             byte c[]=new byte[fi.available()];
              fi.read(c);
              ta.append(new String(c));
              
           }
          catch(IOException e1) 
                 {};
        } else  if(str1=="no2")
        {
          try 
          {
             FileInputStream fi=new FileInputStream("d:\\Java\\no6.txt");
             byte c[]=new byte[fi.available()];
              fi.read(c);
              ta.setText(new String(c));
              
           }
          catch(IOException e1) 
                 {};
        }
     else  if(str1=="no3")
        {
          try 
          {
             FileInputStream fi=new FileInputStream("d:\\Java\\no7.txt");
             byte c[]=new byte[fi.available()];
              fi.read(c);
              ta.setText(new String(c));
              
           }
          catch(IOException e1) 
                 {};
        }
     else  if(str1=="no4")
        {
          try 
          {
             FileInputStream fi=new FileInputStream("d:\\Java\\no8.txt");
             byte c[]=new byte[fi.available()];
              fi.read(c);
              ta.setText(new String(c));
              
           }
          catch(IOException e1) 
                 {};
        }
     }static class wind extends WindowAdapter
    {
         public void windowClosing(WindowEvent e)
     {
         frm.dispose();
     }
    }
    }
      

  3.   

    上面写错了.该用MouseListener
    lst.addMouseListener(new MouseAdapter(){
         public void mouseClicked(MouseEvent me){
              if(me.getClickCount() == 2){
                  str=lst.getItem(lst.getSelectIndex()).toString();
              }
      

  4.   

    记得List类,单击触发的是选择事件,双击触发的是动作事件,试下