editordemo.java

来自「java 完全探索的随书源码」· Java 代码 · 共 74 行

JAVA
74
字号
// EditorDemo.java

import javax.swing.*;
import javax.swing.event.*;

import java.awt.*;
import java.awt.event.*;

import java.net.*;

class EditorDemo extends JFrame
                 implements ActionListener, HyperlinkListener
{
   JEditorPane view;
   JTextField commandLine;

   EditorDemo (String title)
   {
      super (title);

      addWindowListener (new WindowAdapter ()
                         {
                             public void windowClosing (WindowEvent e)
                             {
                                System.exit (0);
                             }
                         });

      commandLine = new JTextField ();
      commandLine.addActionListener (this);
      getContentPane ().add (commandLine, BorderLayout.NORTH);

      view = new JEditorPane ();
      view.setEditable (false);
      view.setPreferredSize (new Dimension (400, 400));
      view.addHyperlinkListener (this);

      getContentPane ().add (view, BorderLayout.CENTER);

      pack ();
      setVisible (true);
   }

   public void actionPerformed (ActionEvent e)
   {
      try
      {
         URL url = new URL (e.getActionCommand ());
         view.setPage (url);
         commandLine.setText (url.toExternalForm ());
      }
      catch (MalformedURLException e2)
      {
         System.out.println ("Bad URL: " + e.getActionCommand ());
      }
      catch (java.io.IOException e2) {}
   }

   public void hyperlinkUpdate (HyperlinkEvent e)
   {
      try
      {
         view.setPage (e.getURL ());
         commandLine.setText (e.getURL ().toExternalForm ());
      }
      catch (java.io.IOException e2) {}
   }

   public static void main (String [] args)
   {
      new EditorDemo ("Editor Demo");
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?