分享

mainframe.java(与urls.java一起完成gui界面)--谭耀武博客 tywo45@163.com

 qzg589 2005-08-10

MainFrame.java(与URLs.java一起完成GUI界面)- -

                                      


package org.tywo.url;


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.logging.Logger;

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

/**
   This program demonstrates how to display HTML documents
   in an editor pane.
*/
public class MainFrame

   public static void main(String[] args)
   { 
      JFrame frame = new EditorPaneFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.show();
   }
}

/**
   This frame contains an editor pane, a text field and button
   to enter a URL and load a document, and a Back button to
   return to a previously loaded document.
*/
class EditorPaneFrame extends JFrame

   public EditorPaneFrame()
   { 
      setTitle("URL伴侣");
      setSize(WIDTH, HEIGHT);

      final Stack urlStack = new Stack();
      final JEditorPane editorPane = new JEditorPane();
      final JTextField url = new JTextField(30);

      // set up hyperlink listener

      editorPane.setEditable(false);
      editorPane.addHyperlinkListener(new
         HyperlinkListener()
         { 
            public void hyperlinkUpdate(HyperlinkEvent event)
            { 
               if (event.getEventType()
                  == HyperlinkEvent.EventType.ACTIVATED)
               { 
                  try
                  { 
                     // remember URL for back button
                     urlStack.push(event.getURL().toString());
                     // show URL in text field
                     url.setText(event.getURL().toString());

                     editorPane.setPage(event.getURL());
                  }
                  catch(IOException e)
                  { 
                     editorPane.setText("Exception: " + e);
                  }
               }
            }
         });

      // set up checkbox for toggling edit mode

      final JCheckBox editable = new JCheckBox();
      editable.addActionListener(new
         ActionListener()
         { 
            public void actionPerformed(ActionEvent event)
            { 
               editorPane.setEditable(editable.isSelected());
            }
         });

      // set up load button for loading URL

      ActionListener listener = new
         ActionListener()
         { 
            public void actionPerformed(ActionEvent event)
            { 
               try
               { 
                  // remember URL for back button
                  urlStack.push(url.getText());

                  editorPane.setPage(url.getText());
               }
               catch(IOException e)
               { 
                  editorPane.setText("Exception: " + e);
               }
            }
         };
  
      JButton loadButton = new JButton("Load");
      loadButton.addActionListener(listener);
      url.addActionListener(listener);

      // set up back button and button action

      JButton backButton = new JButton("Back");
      backButton.addActionListener(new
         ActionListener()
         { 
            public void actionPerformed(ActionEvent event)
            { 
               if (urlStack.size() <= 1) return;
               try
               { 
                  // get URL from back button
                  urlStack.pop();
                  // show URL in text field
                  String urlString = (String)urlStack.peek();
                  url.setText(urlString);

                  editorPane.setPage(urlString);
               }
               catch(IOException e)
               { 
                  editorPane.setText("Exception: " + e);
               }
            }
         });
     
      JButton saveAllButton = new JButton("saveAll");
      saveAllButton.addActionListener(new
         ActionListener()
         { 
            public void actionPerformed(ActionEvent event)
            { 
                URLs.saveAllContentOfLinks(url.getText());
                try
                {
                    URLs.storeContentToFile(url.getText(),new java.net.URL(url.getText()).getHost(),"入口入口入口.html");
                }
                catch(java.net.MalformedURLException me){}
               
                //saveAllContentOfLinks("http://127.0.0.1:8080");
            }
         });
     
      JButton hobbyButton = new JButton("hobby");
      hobbyButton.addActionListener(new
         ActionListener()
         { 
            public void actionPerformed(ActionEvent event)
            { 
               
                    URLs.hobby(null,"links/myLover.links");
               
            }
         });

      Container contentPane = getContentPane();
      contentPane.add(new JScrollPane(editorPane),
         BorderLayout.CENTER);

      // put all control components in a panel

      JPanel panel = new JPanel();
      panel.add(new JLabel("URL"));
      panel.add(url);
      panel.add(loadButton);
      panel.add(backButton);
      panel.add(saveAllButton);
      panel.add(hobbyButton);
      panel.add(new JLabel("Editable"));
      panel.add(editable);

      contentPane.add(panel, BorderLayout.SOUTH);
   }

   private static final int WIDTH =760;
   private static final int HEIGHT = 550;
}

附上程序运行图:

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多