如何在另一个类中使用调用此方法并在Java中显示它

时间:2018-12-04 09:54:49

标签: java arrays user-interface methods override

大家好,我最近正在从事这项工作,但是我一直在努力寻找一种方法来调用此方法,以便在我获得书名并将其显示在列表上之后

因此调用该方法的类是:

     class BookInfo
{

  private String strBookTitle;
  private char chBookType;
  private boolean boolBookFormatAvail[] = new  boolean[3];
  private double doubBookPrice[] = new double[3];
  private String strBookCover;
  private String strBookComment;  


  //set and get

  public void setBook( String l)
  {
      strBookTitle = l;

  }
  public void setchBookType( char l)
  {
      chBookType = l;

  }
  public void setboolBookFormatAvail( boolean[] l)
  {
      boolBookFormatAvail = l;

  }
  public void setbooldoubBookPrice( double [] l)
  {
      doubBookPrice = l;

  }
  public void setstrBookCover( String l)
  {
     strBookCover = l;

  }
  public void setstrBookComment( String l)
  {
      strBookComment = l;

  }


  public String getBook ()
  {
      return strBookTitle;
  }
  public char getBookType ()
  {
      return chBookType;
  }
  public boolean [] getAvailability ()
  {
      return  boolBookFormatAvail;
  }
  public double [] getdoubBookPrice ()
  {
      return doubBookPrice;
  }
  public String getstrBookCover ()
  {
      return strBookCover;
  }
  public String getComment ()
  {
      return strBookComment;
  }



  BookInfo(String title, char cType, boolean boolFormatE, boolean boolFormatM, boolean boolFormatH, double doubEPrice, double doubMPrice, double doubHPrice, String cover,String strBookCmt)
  {

     strBookTitle = title;
      chBookType = cType;
     boolBookFormatAvail[0] = boolFormatE;
      boolBookFormatAvail[1] = boolFormatM;
     boolBookFormatAvail[2] = boolFormatH;

     doubBookPrice[0] = doubEPrice;
     doubBookPrice[1] = doubMPrice;
      doubBookPrice[2] = doubHPrice;
      strBookCover = cover; 
      strBookComment = strBookCmt;


  }
    ////////////////////////////////////////////////
  //Finish this class by writing the various public set/get methods
  // for the above instance variables
  ////////////////////////////////////////////////  
}

///////////////////////////////

class BookList
{

  BookInfo[] bkList;

  public void createList()
  {

     bkList = new BookInfo[29];
        bkList[0] = new BookInfo("20,000 Leagues Under the Sea",'a', true,true,true,34.86,23.99,9.99,"20000 leagues under the sea.jpg","<html>Undersea adventure <br />with captain nemo.</html>");
        bkList[1] = new BookInfo("A Knight in Shining Armour",'r', false,true,true,30.86,12.99,7.99,"A Knight in Shining Armour.jpg","A knight in shining armour");

        bkList[2] = new BookInfo("Around the World in Eighty Days",'a', true,true,false,34.86,23.99,5.99,"Around the World in Eighty Days.jpg","Travelling before travel agents");

        bkList[3] = new BookInfo("Do Androids dream of Electric Sheep",'s', true,false,true,25.45,10.92,8.99,"Do Androids dream of Electric Sheep.jpg","<html>Rick Decard must retire 5 replicants<br /> and wants to be called BladeRunner.</html>");

        bkList[4] = new BookInfo("Dracula",'h', false,false,false,24.86,23.99,44.99,"Dracula.jpg","<html>Yes, it's the guy<br />with the fangs and a liquid diet.</html>");

        bkList[5] = new BookInfo("Dune",'s', true,false,true,24.86,23.99,29.99,"dune.jpg","Desert worms with spice, delicious.");
        bkList[6] = new BookInfo("Ender's Game",'s', false,true,true,30.86,12.99,7.99,"Enders Game.jpg","It's the End game");

        bkList[7] = new BookInfo("Interview with the Vampire",'h', true,true,false,34.86,23.99,5.99,"Interview with the Vampire.jpg","Interview with the Vampire.");

        bkList[8] = new BookInfo("It",'h', true,false,true,25.45,10.92,8.99,"it.jpg","Clown with balloon.");

        bkList[9] = new BookInfo("Jurassic Park",'s', false,false,false,24.86,23.99,44.99,"Jurassic Park.jpg","<html>Yes, it's the dinosaurs<br />with the fangs and a meat diet.</html>");



        bkList[10] = new BookInfo("Life of Pi",'a', true,true,true,34.86,23.99,9.99,"life of pi.jpg","Cooking adventure with pies.");
        bkList[11] = new BookInfo("Misery",'h', false,true,true,30.86,12.99,7.99,"misery.jpg","Author meets crazy fan.");

        bkList[12] = new BookInfo("Neuromancer",'s', true,true,false,34.86,23.99,5.99,"Neuromancer.jpg","Magic with Science");

        bkList[13] = new BookInfo("Ready Player One",'s', true,false,true,25.45,10.92,8.99,"Ready Player One.jpg","Nothing, just not ready.");

        bkList[14] = new BookInfo("Shane",'a', false,false,false,24.86,23.99,44.99,"shane.jpg","Yes, it's the guy on a horse.");

        bkList[15] = new BookInfo("Something Wicked this way comes",'h', true,true,true,34.86,23.99,9.99,"Something Wicked this way comes.jpg","It's called an exam.");
        bkList[16] = new BookInfo("Star Trek - Death in Winter",'s', false,true,true,30.86,12.99,7.99,"Star Trek - Death in Winter.jpg","Better than summer.");

        bkList[17] = new BookInfo("Star Trek - Mission to Horatius",'s', true,true,false,34.86,23.99,5.99,"Star Trek - Mission to Horatius.jpg","Classic action with Kirk and Spock.");

        bkList[18] = new BookInfo("Starship Troopers",'s', true,false,true,25.45,10.92,8.99,"Starship Troopers.jpg","Kill those bugs.");

        bkList[19] = new BookInfo("Stranger in a strange land",'s', false,false,false,24.86,23.99,44.99,"Stranger in a strange land.jpg","Yes, it's strange.");



      bkList[20] = new BookInfo("The Black Lyon",'r', true,true,true,34.86,23.99,9.99,"The Black Lyon.jpg","Less said the better.");
        bkList[21] = new BookInfo("The Count of Monte Cristo",'a', false,true,true,30.86,12.99,7.99,"The count of Monte Cristo.jpg","This is the other count.");

        bkList[22] = new BookInfo("The Day of the Duchess",'r', true,true,false,34.86,23.99,5.99,"The Day of the Duchess.jpg","Yes, it's that day");

        bkList[23] = new BookInfo("The Handmaid's Tale",'s', true,false,true,25.45,10.92,8.99,"The Hand maids Tale.jpg","And what a tale.");

        bkList[24] = new BookInfo("The Notebook",'r', false,false,false,24.86,23.99,44.99,"The Notebook.jpg","Yes, it's the guy with a notebook.");

        bkList[25] = new BookInfo("The Thirty-nine steps",'a', true,true,true,34.86,23.99,9.99,"The Thirty-nine steps.jpg","One step too many.");
        bkList[26] = new BookInfo("The Time Traveler's Wife",'r', false,true,true,30.86,12.99,7.99,"The Time travelers Wife.jpg","Doctor Who.");

        bkList[27] = new BookInfo("The Woman in Black",'h', true,true,false,34.86,23.99,5.99,"The Woman in Black.jpg","There was no other colour.");

        bkList[28] = new BookInfo("True Grit",'a', true,false,true,25.45,10.92,8.99,"true grit.jpg","Yes, it's true.");


  }

 public static void main(String[] args)
{
   BookList x = new BookList();

   System.out.println(x.createList().toString());
}

} 

这个方法我想在这里显示:

 public void actionPerformed(ActionEvent e)
   {
      Object source = e.getSource();
      String cmd = e.getActionCommand();

      BookInfo caca;
      BookList pedro = new BookList();
      JFrame frame = new JFrame();
      if(source == bookType )
      {

         int b = bookType.getSelectedIndex();
         if (b == 0)
         {
            JOptionPane.showMessageDialog(frame,"No Genre selected");

         }else if(b == 1)
         {

            // here is where the code is supoose to go      

         }

      }
}

我已经尝试过callind类BookList()的对象,但是不起作用,而且我也尝试使用mu get和set方法只是为了检索也不起作用的信息bu

0 个答案:

没有答案