Visualizzazione dei risultati da 1 a 4 su 4

Discussione: [Java] Creare browser

  1. #1

    [Java] Creare browser

    Ciao a tutti.
    Mi interessa sapere se esiste una classe oppure una libreria che permette di interpretare codice HTML e generare il relativo output "grafico".
    In dettaglio, dovrei mostrare un'anteprima di una e-mail!

    Grazie

  2. #2
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,318
    Prova a dare un'occhiata alla classe JEditorPane. Segui per bene la documentazione ed eventuali esempi. Non è semplice da utilizzare.


    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

  3. #3
    Grazie.
    Ora guardo la guida...

  4. #4
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    Era più per curiosità che altro, qualche tempo fa ci avevo messo mano anch'io, semplicemente ripulendo un po' il codice che trovi nel tutorial di Swing sul sito della sun.

    codice:
    import javax.swing.*;
    import javax.swing.text.*;
    
    import java.awt.*;              //for layout managers and more
    import java.awt.event.*;        //for action events
    
    import java.net.URL;
    import java.io.IOException;
    
    
    public class TextSamplerDemo extends JPanel implements ActionListener {
    
    
        private JLabel address = new JLabel("Address: ");
        private JTextField addr = new JTextField(100);
        private JButton go = new JButton("Go!");
        private JEditorPane editorPane;
    
        public void actionPerformed (ActionEvent ae) {
          try {
            editorPane.setPage(new URL(addr.getText()));
          }
          catch (Exception e) {
            System.out.println(e.toString());
          }
          validate();
    
        }
    
        public TextSamplerDemo() {
          setLayout(new BorderLayout());
          JPanel navbar = new JPanel();
          navbar.add(address);
          navbar.add(addr);
          navbar.add(go);
          go.addActionListener(this);
          add(navbar, BorderLayout.NORTH);
    
          editorPane = createEditorPane();
          JScrollPane editorScrollPane = new JScrollPane(editorPane);
          editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
          editorScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
          editorScrollPane.setPreferredSize(new Dimension(800, 600));
          editorScrollPane.setMinimumSize(new Dimension(10, 10));
          add(editorScrollPane, BorderLayout.CENTER);
        }
    
       private JEditorPane createEditorPane() {
            JEditorPane editorPane = new JEditorPane();
            editorPane.setEditable(false);
            try {
              java.net.URL helpURL = new URL("http://www.google.it");
              try {
                editorPane.setPage(helpURL);
              }
              catch (IOException e) {
                System.err.println("Attempted to read a bad URL: " + helpURL);
              }
            }
            catch (Exception e) {System.out.println("Azz: "+e.toString());}
            return editorPane;
        }
    
    
        private static void createAndShowGUI() {
            //Make sure we have nice window decorations.
            JFrame.setDefaultLookAndFeelDecorated(true);
    
            //Create and set up the window.
            JFrame frame = new JFrame("TextSamplerDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Create and set up the content pane.
            JComponent newContentPane = new TextSamplerDemo();
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);
    
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    Come potrai vedere, alcune cose sono mostrate decentemente, altre affatto... c'è da gestire javascript da zero ad esempio (prova ad aprire yahoo.com). Se lo devi usare per mostrare documentazione con formattazione semplice però secondo me te la sbrighi con poco.
    Ciao
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

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 © 2025 vBulletin Solutions, Inc. All rights reserved.