【问题标题】:Printing in one JOption pane across multiple classes在多个类的一个 JOption 窗格中打印
【发布时间】:2016-07-31 21:10:20
【问题描述】:

我试图将所有输出放到一个跨 3 个不同类的 JOption 窗格中。目前,每当我打印电子书时,它们都会打印在各自的对话框中,必须一次按一个确定,才能循环浏览所有书籍。

我需要将它们与“编程者”和电子书计数/成本部分一起包含在一个对话框中。

我很好奇是否必须跨每个类进行导入。谢谢。

import javax.swing.JOptionPane; // dialog box

public class Ebook
{
    private String author = "";
    private String title = "";
    private double price = 0.0;
    private String isbn = "";


        public Ebook(String author, String title, double price, String isbn) // ebook constructor 
        {
            this.author = author;
            this.title = title;

            if (price > 0) // validate non-negative price
                this.price = price;

            else 
            {   
                this.price = 0.0; 
                    System.out.println("Invalid price");
            }

            if (isbn.length() == 10 || isbn.length() ==  13) // isbn length must be exactly 10 or 13
                this.isbn = isbn;

            else
                this.isbn = "None";
        }   

        public void setPrice(double price)
        {
            if (price < 0) // validate
            {
                System.out.println("Invalid price");
            }

            else
                this.price = price;
        }

        public double getPrice()
        {
            return price;
        }

        public void setAuthor(String theAuthor)
        {
            this.author = theAuthor;
        }

        public String getAuthor()
        {
            return author;
        }

        public void setIsbn(String isbn)
        {
            if (isbn.length() == 10 || isbn.length() ==  13) // validate
            {
                this.isbn = isbn;
            }
            else 
                isbn = "None";
        }

        public String getIsbn()
        {
            return isbn;
        }

        public void setTitle(String title)
        {
            this.title = title;
        }

        public String getTitle()
        {
            return title;
        }

        public String toString()
        {
            return String.format("Author: %s%nTitle: %s%nPrice: $%.1f%nISBN: %s%n",
                author,title,price,isbn);




        } 
} // This was made by --



import javax.swing.JOptionPane; // dialog box

public class EbookLibrary
{
    private int count = 0;
    private double total_cost = 0.0;


    Ebook[] ebooks = new Ebook[25]; // array of ebook objects

    public EbookLibrary() // no argument constructor for ebooklibrary object in library test
    {

    }
    public int getCount() // total number of ebooks
    {
        return count;
    }
    public double getCost() // sum of all ebooks
    {
        return total_cost;
    }
    public String toString() // formatted string with the number and cost of all ebooks
    {
        String message2 = String.format("Ebook count: %d%nTotal Cost: $%.1f", count, total_cost); // for dialog box



        return message2;
    }
    public void addEbook(String author, String title, double price, String isbn) // adds ebooks to the array
    {

     if (count < 25) // dont walk off array
     {


        Ebook anEbook = new Ebook(author,title,price,isbn); // ebook object to be created when addEbook is called 

        ebooks[count] = anEbook;

        total_cost += price; // total of all the books 

        String message = String.format("%s%n", ebooks[count]);
            JOptionPane.showMessageDialog(null,message); // display books in dialog boxes

        count++; // increment count each time 


     }



    } 



} // This was made by ------

import javax.swing.JOptionPane; // dialog box

public class EbookLibraryTest
{
    public static void main(String[] args)
    {

        EbookLibrary aLibrary = new EbookLibrary(); // EbookLibrary object for calling addEbook

        // ebook objects
        aLibrary.addEbook("Blah", "What", 88.8, "1234567891")
        aLibrary.addEbook("Thing Do", "What What", 45.0, "1234567891111");
        aLibrary.addEbook("Stephen King","The Thing",1.1, "1234567891");
        aLibrary.addEbook("Robert","A Title", -1.0, "1234567891"); // test invalid price, should return 0.0 and "invalid price"
        aLibrary.addEbook("Tom","Bad Title", 33.1, "1234567891111");
        aLibrary.addEbook("Bob", "FML and Other Acronyms", 25.0, "1"); // test ISBN value, should return "None"


        aLibrary.getCount();
        aLibrary.getCost();

        String message = "Programmed by ---- ";
            String message2 = String.format("%s%n", aLibrary.toString());
                JOptionPane.showMessageDialog(null,message + "\n " + message2);




    }
}

【问题讨论】:

    标签: java dialog joptionpane


    【解决方案1】:

    首先,您需要删除添加Ebook 时添加对话框的部分。相反,您希望有一种方法可以一次完成所有操作。

    public void printOrder(String signature){
        String message = "";
        for (int q = 0; q < count; q++){
            message += String.format("%s%n", ebooks[q]);
        }
        message += signature;
        JOptionPane.showMessageDialog(null,message);
    }
    

    该代码将放在EBookLibrary 中。我还想指出,您可能需要查看 List 以存储 Ebook,因为它可以根据您需要的大小而增长和缩小。


    现在,在您打印签名的最后,您可以将其更改为 printOrder() 方法并解析您将打印出的文本。

    String message = "Programmed by ---- ";
    String message2 = String.format("%s%n", aLibrary.toString());
    
    aLibrary.printOrder(message + "\n " + message2);
    

    【讨论】:

      【解决方案2】:

      您正在显示 7 个消息对话框而不是 1 个。

      只需创建一个方法来返回有关您的EBookLibrary 类中每个addedEbook 的信息,然后主要使用一个循环并一次添加一本书。每次添加新书时,该书返回的信息 (string) 都会自行添加到您的main 中的message2

      或者,您可以像这样编辑addEbook 方法,而不是创建新方法:

      String message = String.format("%s%n", ebooks[count]);
      count++;
      return message; //instead of JOptionPane
      

      main 方法中:

          EbookLibrary aLibrary = new EbookLibrary(); // EbookLibrary object for calling addEbook
          String message = "Programmed by ----  ";
      
          while(count < 25) {
          // ebook objects
          message += aLibrary.addEbook("Blah", "What", 88.8, "1234567891") + '/n':
          message += aLibrary.addEbook("Thing Do", "What What", 45.0, "1234567891111") + '/n';
          message += aLibrary.addEbook("Stephen King","The Thing",1.1, "1234567891") + '/n';
          message += aLibrary.addEbook("Robert","A Title", -1.0, "1234567891") + '/n'; // test invalid price, should return 0.0 and "invalid price"
          message += aLibrary.addEbook("Tom","Bad Title", 33.1, "1234567891111") + '/n';
          message += aLibrary.addEbook("Bob", "FML and Other Acronyms", 25.0, "1") + '/n'; // test ISBN value, should return "None"
      
      
          aLibrary.getCount();
          aLibrary.getCost();
      
      
            //String message2 = String.format("%s%n", aLibrary.toString());
                  JOptionPane.showMessageDialog(null,message);
      

      试试这个,或者尝试从库中取出 JOptionPanes,只在 main 中显示单个窗格。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-27
        • 2014-04-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多