2012-01-06 3 views
3

웹 페이지를 표시하는 JPanel을 만들려고합니다. 나는 페이지를 표시 할 때 웹 페이지를 읽을 수있는 무대에 갔는데, 그 페이지는 모두 뒤죽박죽으로 보입니다. 아래를 참조하십시오.JPanel에 내장 된 브라우저

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

public class Browser { 
private JFrame frame; 
private JPanel panelTop; 
private JEditorPane editor; 
private JScrollPane scroll; 
private JTextField field; 
private JButton button; 
private URL url; 

public Browser(String title) { 
    initComponents(); 

    //set the title of the frame 
    frame.setTitle(title); 

    //set the default cloe op of the jframe 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    //set size of frame 
    frame.setSize(800,600); 

    //add jpanel to north of jframe 
    frame.add(BorderLayout.NORTH, panelTop); 

    //add textfield and navigation button to jpanel. 
    panelTop.add(field); 
    panelTop.add(button); 

    //add scroll pane to jframe center 
    frame.add(BorderLayout.CENTER, scroll); 


    //set the frame visible 
    frame.setVisible(true); 
}//end Browser() constructor 

private void initComponents() { 
    //create the JFrame 
    frame = new JFrame(); 

    //create the JPanel used to hold the text field and button. 
    panelTop = new JPanel(); 

    //set the url 
    try { 
     url = new URL("http://www.google.co.uk"); 
    } 
    catch(MalformedURLException mue) { 
     JOptionPane.showMessageDialog(null,mue); 
    } 

    //create the JEditorPane 
    try { 
     editor = new JEditorPane(url); 

     //set the editor pane to false. 
     editor.setEditable(false); 
    } 
    catch(IOException ioe) { 
     JOptionPane.showMessageDialog(null,ioe); 
    } 

    //create the scroll pane and add the JEditorPane to it. 
    scroll = new JScrollPane(editor, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
      JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 

    //create the JTextField 
    field = new JTextField(); 

    //set the JTextField text to the url. 
    //we're not doing this on the event dispatch thread, so we need to use 
    //SwingUtilities. 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      field.setText(url.toString()); 
     } 
    }); 

    //create the button for chanign pages. 
    button = new JButton("Go"); 

    //add action listener to the button 
    button.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      try { 
       editor.setPage(field.getText()); 
      } 
      catch(IOException ioe) { 
       JOptionPane.showMessageDialog(null, ioe); 
      } 
     } 
    }); 
}//end initComponents() 

public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      new Browser("Simple web browser"); 
     } 
    }); 
}//end main method. 
}//end Browser class 

내가 그 읽기 CSS의 ANDE 플래시 등을 놓치고 몇 가지 방법이 있나요 : 여기

enter image description here

는 소스 코드?

+1

내가 JEditorPane로는 가장 현대적인 웹 콘텐츠 형식을 처리 할 수 ​​없다는 것을 거의 확실 해요. – mcfinnigan

답변

6

그냥 JEditorPane이 오래된 HTML (3.2 부분)을 렌더링하고 간단한 CSS가 브라우저가되지 않기 때문입니다. 귀하가 관리하는 콘텐츠에만 적합합니다.

내가 그 읽기 CSS를 실종 몇 가지 방법이 ..

그것은 단순한 스타일이 지원하지 않습니다입니다.

.. 플래시 등?

HTML editor kit에서는 지원되지 않습니다.

새로운 HTML 요소 에 대한 지원을 편집기 키트에 추가 할 수 있지만 플래시, 애플릿 등을 렌더링하는 구성 요소를 제공해야합니다.

+0

내가 사용할 수있는 대안이 있습니까? –

+1

시도 http://djproject.sourceforge.net/ns/index.html –

4

음, 그렇습니다. 일반적으로 HTTP URL에서 응답을 받기 위해 일부 Java HTTP 클라이언트 (이 경우 JEditorPane의 URL에서 HTML 응답을 가져 오는 기능)를 사용하면 자바 스크립트 코드 및 RIA 위젯을 실행하지 않고 텍스트를 얻을 수 있습니다 출력 (예 : Flash, Silverlight 등). 이제 Javascript 문제에 대해 Javascript를 실행하는 HtmlUnit (http://htmlunit.sourceforge.net/)을 사용할 수 있지만 Flash 위젯에 대해서는 여전히 운이 좋지 않습니다.

그러나 여기에 표시하려는 Google 페이지에는 일반적으로 플래시가 없거나 그런 내용이 없습니다. 그냥 HTMLUnit을 사용하여 URL에서 HTML (더하기 실행 된 자바 스크립트)을 가져 와서 표시하십시오. 스윙 GUI를 미세 조정하기 위해 그 HTML을 직접 수동으로 파싱해야 할 수도 있습니다.

로보는 오픈 소스 Java 웹 브라우저입니다. 당신은 소스를 체크 아웃하고 그 사람이 그것을 어떻게 참조 할 수 있습니다 :

http://lobobrowser.org/sourcecode.jsp