2011-08-06 3 views
0

Je suis nouveau sur java. Je veux intégrer un navigateur dans un JFrame. J'ai utilisé un JEditorPane pour afficher le HTML, mais il n'affiche toujours pas la page correctement. Il montre la page, mais les choses ne sont pas à leur place. Ma plus grande préoccupation est que les applets Java qui sont incorporés dans les pages Web n'apparaissent pas dans mon navigateur personnalisé. Je n'utilise probablement pas JEditorPane correctement. Voici mon code:Applet intégré au navigateur intégré dans JFrame

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.net.*; 
import java.io.*; 

public class Frame1 extends JFrame implements ActionListener 
{ 
    private JEditorPane editorPane;  
    private JTextField addressField; 
    private JButton goButton; 
    private JLabel label; 

    public Frame1() {   
     super("Frame1"); 
     try { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } catch(Exception e) { 
      System.out.println("Unablet to set look and feel."); 
     } 

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     label = new JLabel("Enter URL. Must include http://"); 

     // setup editor pane 
     editorPane = new JEditorPane(); 
     editorPane.setEditable(false); 
     URL url = null; 
     try { 
      url = new URL("http://www.cut-the-knot.org/SimpleGames/FrogsAndToads2d.shtml"); 
     } catch (MalformedURLException e) { 

     } 

     if (url != null) { 
      try { 
       editorPane.setPage(url); 
      } catch (IOException e) { 
       label.setText("Attempted to read a bad URL: " + url); 
      } 
     } else { 
      label.setText("String specifies an unknown protocol."); 
     } 

     // put editor pane in a scroll pane 
     JScrollPane editorScrollPane = new JScrollPane(editorPane); 
     editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
     editorScrollPane.setPreferredSize(new Dimension(250, 145)); 
     editorScrollPane.setMinimumSize(new Dimension(10, 10)); 

     // setup everything in southPanel. 
     // southPanel > addressBar + label> addressField + goButton 
     addressField = new JTextField(30); 
     goButton = new JButton("Go");  
     goButton.addActionListener(this); 
     JPanel addressBar = new JPanel();   
     JPanel southPanel = new JPanel(new GridLayout(2, 1)); 
     addressBar.add(addressField); 
     addressBar.add(goButton); 
     southPanel.add(addressBar); 
     southPanel.add(label); 

     // add everything to JFrame 
     setLayout(new BorderLayout());   
     add(southPanel, BorderLayout.SOUTH); 
     add(editorScrollPane, BorderLayout.CENTER); 

     setSize(500, 500); 
     setVisible(true); 
    } 

    public void actionPerformed(ActionEvent e) { 
     if (e.getSource() == goButton) { 
      URL url = null; 
      try { 
       url = new URL(addressField.getText()); 
      } catch (MalformedURLException murle) { 

      } 
      if (url != null) { 
       try { 
        editorPane.setPage(url); 
       } catch (IOException ioe) { 
        label.setText("Attempted to read a bad URL: " + url); 
       } 
      } else { 
       label.setText("String specifies an unknown protocol."); 
      } 
     } 
    } 

    public static void main(String[] args) { 
     new Frame1(); 
    } 
} 
+1

'JEditorPane' n'est pas un navigateur; c'est un composant de texte. Voir aussi cette [réponse] (http://stackoverflow.com/questions/6784577/vertically-centering-text-in-html-table-cell-in-java-jlabel/6785121#6785121). – trashgod

+1

Oui, le support HTML de Swing est uniquement pour le formatage, pas pour l'incorporation d'applets ou similaires. Vous pourriez être en mesure d'intégrer une partie du 'appletviewer', mais alors vous n'auriez pas de sortie HTML, seulement l'applet (s). –

+0

En aparté, il est incroyablement mauvais d'essayer/attraper sans rien dans le bloc catch. Ne faites pas que des exceptions. –

Répondre

Questions connexes