Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    [Java] Domanda stupida su class

    Salve a tutti.
    Sono un neofita di java e avrei una domanda banale.
    Sto creando una piccola applicazione con NetBeans e nel codice sorgente ho incollato questa classe:
    codice:
    class Example3  {
    
       //--- Private instances declarations
       private final static int POINTS_PER_INCH = 72;
    
       
       /**
        * Constructor: Example3 
    
    
        *
        */
       public Example3 () {
          
          //--- Create a new PrinterJob object
          PrinterJob printJob = PrinterJob.getPrinterJob ();
    
          //--- Create a new book to add pages to
          Book book = new Book ();
    
          //--- Add the cover page using the default page format for this print job
          book.append (new IntroPage (), printJob.defaultPage ());
    
          //--- Add the document page using a landscape page format
          PageFormat documentPageFormat = new PageFormat ();
          documentPageFormat.setOrientation (PageFormat.LANDSCAPE);
          book.append (new Document (), documentPageFormat);         
    
          //--- Add a third page using the same painter
          book.append (new Document (), documentPageFormat);         
    
          //--- Tell the printJob to use the book as the pageable object
          printJob.setPageable (book); 
    
          //--- Show the print dialog box. If the user click the 
          //--- print button we then proceed to print else we cancel
          //--- the process.
          if (printJob.printDialog()) {
             try {
                printJob.print();  
             } catch (Exception PrintException) {
                PrintException.printStackTrace();
             }
          }
    
       }
    
       /**
        * Class: IntroPage 
    
    
        * 
        * This class defines the painter for the cover page by implementing the
        * Printable interface. 
    
    
        *
        * @author Jean-Pierre Dube <jpdube@videotron.ca>
        * @version 1.0
        * @since 1.0
        * @see Printable
        */
       private class IntroPage implements Printable {
    
    
          /**
           * Method: print 
    
    
           *
           * @param g a value of type Graphics
           * @param pageFormat a value of type PageFormat
           * @param page a value of type int
           * @return a value of type int
           */
          public int print (Graphics g, PageFormat pageFormat, int page) {
    
             //--- Create the Graphics2D object
             Graphics2D g2d = (Graphics2D) g;
    
             //--- Translate the origin to 0,0 for the top left corner
             g2d.translate (pageFormat.getImageableX (), pageFormat.getImageableY ());
    
             //--- Set the default drawing color to black
             g2d.setPaint (Color.black);
    
             //--- Draw a border arround the page
             Rectangle2D.Double border = new Rectangle2D.Double (0, 
                                                                 0, 
                                                                 pageFormat.getImageableWidth (),
                                                                 pageFormat.getImageableHeight ());
             g2d.draw (border);
    
             //--- Print the title
             String titleText = "Printing in Java Part 2";
             Font titleFont = new Font ("helvetica", Font.BOLD, 36);
             g2d.setFont (titleFont);
    
             //--- Compute the horizontal center of the page
             FontMetrics fontMetrics = g2d.getFontMetrics ();
             double titleX = (pageFormat.getImageableWidth () / 2) - (fontMetrics.stringWidth (titleText) / 2);
             double titleY = 3 * POINTS_PER_INCH;
             g2d.drawString (titleText, (int) titleX, (int) titleY);
    
             return (PAGE_EXISTS);
          }
       }
    
    
    
       /**
        * Class: Document 
    
    
        *
        * This class is the painter for the document content.
    
    
        *
        *
        * @author Jean-Pierre Dube <jpdube@videotron.ca>
        * @version 1.0
        * @since 1.0
        * @see Printable
        */
       private class Document implements Printable {
    
    
          /**
           * Method: print 
    
    
           *
           * @param g a value of type Graphics
           * @param pageFormat a value of type PageFormat
           * @param page a value of type int
           * @return a value of type int
           */
          public int print (Graphics g, PageFormat pageFormat, int page) {
    
    
             //--- Create the Graphics2D object
             Graphics2D g2d = (Graphics2D) g;
    
             //--- Translate the origin to 0,0 for the top left corner
             g2d.translate (pageFormat.getImageableX (), pageFormat.getImageableY ());
    
             //--- Set the drawing color to black
             g2d.setPaint (Color.black);
    
             //--- Draw a border arround the page using a 12 point border
             g2d.setStroke (new BasicStroke (12));
             Rectangle2D.Double border = new Rectangle2D.Double (0, 
                                                                 0, 
                                                                 pageFormat.getImageableWidth (),
                                                                 pageFormat.getImageableHeight ());
    
             g2d.draw (border);
    
    
             //--- Print page 1
             if (page == 1) {
                //--- Print the text one inch from the top and laft margins
                g2d.drawString ("This the content page of page: " + page, POINTS_PER_INCH, POINTS_PER_INCH);
                return (PAGE_EXISTS);
             }
    
             //--- Print page 2
             else if (page == 2) {
                //--- Print the text one inch from the top and laft margins
                g2d.drawString ("This the content of the second page: " + page, POINTS_PER_INCH, POINTS_PER_INCH);
                return (PAGE_EXISTS);
             }
             
    
             //--- Validate the page
             return (NO_SUCH_PAGE);
    
          }
       }
       
    } // Example3
    Come faccio a richiamarla di un pulsante JButton?
    grazie in anticipo dell'aiuto
    OnLine il nuovo sito: www.lasfidacontinua.it

  2. #2
    Utente di HTML.it L'avatar di Alex'87
    Registrato dal
    Aug 2001
    residenza
    Verona
    Messaggi
    5,802

    Re: [Java] Domanda stupida su class

    Originariamente inviato da rob_santini
    Come faccio a richiamarla di un pulsante JButton?
    grazie in anticipo dell'aiuto
    Aggiungi un ActionListener al tuo pulsante:

    codice:
    myButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // istanzia qui la tua classe
        }
    });
    SpringSource Certified Spring Professional | Pivotal Certified Enterprise Integration Specialist
    Di questo libro e degli altri (blog personale di recensioni libri) | ​NO M.P. TECNICI

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.