Swingexamples-using-jeditorpane-to-show-html

提供:Dev Guides
移動先:案内検索

Swingの例-JEditorPaneを使用してHTMLページを表示する

次の例は、SwingベースのアプリケーションでHTMLファイルのHTMLコンテンツを表示する方法を示しています。

次のAPIを使用しています。

  • JEditorPane -HTMLコンテンツを表示するためのエディターボックスを作成します。
  • * jEditorPane.setPage(URL url)*-渡されたURLからHTMLを取得して表示します。

SwingTester.javaが存在する現在のディレクトリにhtmlファイル test を作成してみましょう。

test

<html>
   <head>
      <title>Swing Tester</title>
   </head>
   <body>
      <p>Swing API is a set of extensible GUI Components
         to ease the developer's life to create JAVA based Front End/GUI
         Applications. It is build on top of AWT API and acts as a replacement of
         AWT API, since it has almost every control corresponding to AWT controls.
         Swing component follows a Model-View-Controller architecture to
         fulfill the following criterias.</p>
      <ul>
         <li><p>A single API is to be sufficient to support multiple look and feel.</p></li>
         <li><p>API is to be model driven so that the highest level API is not required to have data.</p></li>
         <li><p>API is to use the Java Bean model so that Builder Tools and IDE can provide better services to the developers for use.</p></li>
      </ul>
      <h2>MVC Architecture</h2>
      <p>Swing API architecture follows loosely based MVC architecture in the following manner.</p>
      <ul>
         <li><p>Model represents component's data.</p></li>
         <li><p>View represents visual representation of the component's data.</p></li>
         <li><p>Controller takes the input from the user on the view and reflects the changes in Component's data.</p></li>
         <li><p>Swing component has Model as a seperate element, while the View and Controller part are clubbed in the User Interface elements. Because of which, Swing has a pluggable look-and-feel architecture.</p></li>
      </ul>
      <h2>Swing Features</h2>
      <ul>
         <li><p><b>Light Weight</b> − Swing components are independent of native Operating System's API as Swing API controls are rendered mostly using pure JAVA code instead of underlying operating system calls.</p></li>
         <li><p><b>Rich Controls</b> − Swing provides a rich set of advanced controls like Tree, TabbedPane, slider, colorpicker, and table controls.</p></li>
         <li><p><b>Highly Customizable</b> − Swing controls can be customized in a very easy way as visual apperance is independent of internal representation.</p></li>
         <li><p><b>Pluggable look-and-feel</b> − SWING based GUI Application look and feel can be changed at run-time, based on available values.</p></li>
      </ul>
   </body>
</html>

SwingTester

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.io.IOException;
import java.net.URL;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class SwingTester {
   public static void main(String[] args) {
      createWindow();
   }

   private static void createWindow() {
      JFrame frame = new JFrame("Swing Tester");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      createUI(frame);
      frame.setSize(560, 450);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   private static void createUI(final JFrame frame){
      JPanel panel = new JPanel();
      LayoutManager layout = new FlowLayout();
      panel.setLayout(layout);

      JEditorPane jEditorPane = new JEditorPane();
      jEditorPane.setEditable(false);
      URL url= SwingTester.class.getResource("test");

      try {
         jEditorPane.setPage(url);
      } catch (IOException e) {
         jEditorPane.setContentType("text/html");
         jEditorPane.setText("<html>Page not found.</html>");
      }

      JScrollPane jScrollPane = new JScrollPane(jEditorPane);
      jScrollPane.setPreferredSize(new Dimension(540,400));

      panel.add(jScrollPane);
      frame.getContentPane().add(panel, BorderLayout.CENTER);
   }
}

出力

htmlファイルを使用してHTMLを表示 link:/cgi-bin/printpage.cgi [__印刷]