Java 实现的简易浏览器

Java 实现的简易浏览器(Think浏览器的实现)


[java]  view plain copy
  1. /* 
  2.  * Name:MainUI 
  3.  * Writer:bitsjx 
  4.  * Date:2009-12-07 
  5.  * Time:00:20 
  6.  * Function:the MainUI class which implement the basic GUI function 
  7.  * */  
  8. package ui;  
  9. import java.awt.BorderLayout;  
  10. import java.awt.Container;  
  11. import java.awt.Dimension;  
  12. import java.awt.Toolkit;  
  13. import java.awt.event.ActionEvent;  
  14. import java.awt.event.ActionListener;  
  15. import java.awt.event.InputEvent;  
  16. import java.awt.event.KeyAdapter;  
  17. import java.awt.event.KeyEvent;  
  18. import java.awt.event.WindowAdapter;  
  19. import java.awt.event.WindowEvent;  
  20. import java.io.BufferedReader;  
  21. import java.io.BufferedWriter;  
  22. import java.io.File;  
  23. import java.io.FileInputStream;  
  24. import java.io.FileReader;  
  25. import java.io.FileWriter;  
  26. import java.io.IOException;  
  27. import java.io.InputStream;  
  28. import java.io.InputStreamReader;  
  29. import java.io.Reader;  
  30. import java.net.MalformedURLException;  
  31. import java.net.URI;  
  32. import java.net.URISyntaxException;  
  33. import java.net.URL;  
  34. import java.net.URLConnection;  
  35. import java.util.regex.Matcher;  
  36. import java.util.regex.Pattern;  
  37. import javax.swing.Box;  
  38. import javax.swing.BoxLayout;  
  39. import javax.swing.ImageIcon;  
  40. import javax.swing.JButton;  
  41. import javax.swing.JEditorPane;  
  42. import javax.swing.JFileChooser;  
  43. import javax.swing.JFrame;  
  44. import javax.swing.JLabel;  
  45. import javax.swing.JMenu;  
  46. import javax.swing.JMenuBar;  
  47. import javax.swing.JMenuItem;  
  48. import javax.swing.JOptionPane;  
  49. import javax.swing.JScrollPane;  
  50. import javax.swing.JSplitPane;  
  51. import javax.swing.JTextField;  
  52. import javax.swing.JToolBar;  
  53. import javax.swing.JTree;  
  54. import javax.swing.KeyStroke;  
  55. import javax.swing.SwingConstants;  
  56. import javax.swing.event.HyperlinkEvent;  
  57. import javax.swing.event.HyperlinkListener;  
  58. import javax.swing.event.TreeSelectionEvent;  
  59. import javax.swing.event.TreeSelectionListener;  
  60. import javax.swing.text.BadLocationException;  
  61. import javax.swing.text.EditorKit;  
  62. import javax.swing.text.html.HTMLDocument;  
  63. import javax.swing.text.html.HTMLEditorKit;  
  64. import javax.swing.text.html.HTMLFrameHyperlinkEvent;  
  65. import javax.swing.text.html.HTMLEditorKit.Parser;  
  66. import javax.swing.tree.DefaultMutableTreeNode;  
  67. import ui.JMPWindow;  
  68. import core.PageList;  
  69. import core.PageNode;  
  70. import core.ThinkFrame;  
  71. public class MainUI extends JFrame {  
  72.     // 建立主框架  
  73.     JFrame think = new JFrame("think");   
  74.     // 建立主菜单栏  
  75.     JMenuBar menubar = new JMenuBar();   
  76.     // 文件菜单  
  77.     JMenu filemenu = new JMenu("文件(F)");   
  78.     JMenuItem newwindow = new JMenuItem("新窗口(N)");  
  79.     JMenuItem openfile = new JMenuItem("打开(O)");  
  80.     JMenuItem saveas = new JMenuItem("另存为(S)");  
  81.     JMenuItem quit = new JMenuItem("退出(Q)");  
  82.     // 查看菜单  
  83.     JMenu watchmenu = new JMenu("查看(V)");   
  84.     JMenuItem sourcecode = new JMenuItem("源代码(O)");  
  85.     // 书签菜单  
  86.     static JMenu bookmarkmenu = new JMenu("书签(B)");   
  87.     JMenuItem addbookmark = new JMenuItem("添加书签(B)");  
  88.     // 帮助菜单  
  89.     JMenu helpmenu = new JMenu("帮助(H)");   
  90.     JMenuItem help = new JMenuItem("think 帮助");  
  91.     JMenuItem about = new JMenuItem("关于 think");  
  92.     // 打开保存对话框  
  93.     JFileChooser openfilechooser = new JFileChooser("D://");  
  94.     JFileChooser saveasfilechooser = new JFileChooser("D://");  
  95.     // 定义三种窗口外观  
  96.     public final static String Windows = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";  
  97.     public final static String Metal = "javax.swing.plaf.metal.MetalLookAndFeel";  
  98.     public final static String Motif = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";  
  99.     //定义按钮的图标  
  100.     ImageIcon bookmarkicon=new ImageIcon(getClass().getResource("/icons/back.png"));  
  101.     ImageIcon forwardicon=new ImageIcon(getClass().getResource("/icons/forward.png"));  
  102.     ImageIcon backicon=new ImageIcon(getClass().getResource("/icons/back.png"));  
  103.     ImageIcon refreshicon=new ImageIcon(getClass().getResource("/icons/refresh.png"));   
  104.     ImageIcon homeicon=new ImageIcon(getClass().getResource("/icons/home.png"));  
  105.     ImageIcon goicon=new ImageIcon(getClass().getResource("/icons/go.png"));  
  106.     ImageIcon stopicon=new ImageIcon(getClass().getResource("/icons/stop.png"));  
  107.     //建立工具栏   
  108.     JToolBar toolbar=new JToolBar();   
  109.     JButton bookmark=new JButton("书签",bookmarkicon);  
  110.     JButton back=new JButton("后退",backicon);   
  111.     JButton forward=new JButton("前进",forwardicon);   
  112.     JButton stop=new JButton("停止",stopicon);  
  113.     JButton refresh=new JButton("刷新",refreshicon);   
  114.     JButton home=new JButton("主页",homeicon);  
  115.     //地址栏  
  116.     JLabel addresslabel=new JLabel("地址:");  
  117.     JTextField urlfield = new JTextField(50);  
  118.     JButton go = new JButton("转到",goicon);  
  119.     Box addressbox = new Box(BoxLayout.LINE_AXIS);  
  120.     JToolBar addresstoolbar=new JToolBar();  
  121.     // 建立显示网页的页面  
  122.     JEditorPane webpagepane = new JEditorPane();  
  123.     JScrollPane scrollpane = new JScrollPane(webpagepane);  
  124.     JScrollPane treescollpane=new JScrollPane();  
  125.     // 默认主页URL地址  
  126.     String urladdress = "http://www.baidu.com";  
  127.     //建立分隔栏  
  128.     JSplitPane splitPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);  
  129.     //创建树根节点  
  130.     DefaultMutableTreeNode root=new DefaultMutableTreeNode("收藏夹");  
  131.     //创建一个文件节点  
  132.     DefaultMutableTreeNode homepage=new DefaultMutableTreeNode("主页");  
  133.     JTree jtree=new JTree(root);  
  134.     // 建立一个链表来保存历史记录  
  135.     PageList historylist = new PageList();  
  136.     // 建立一个链表来保存书签记录  
  137.     PageList bookmarklist = new PageList();  
  138.     public MainUI() {  
  139.         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
  140.         screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
  141.         think.setPreferredSize(new Dimension(800,600));             
  142.         int thinkWidth=this.getPreferredSize().width;  
  143.         int thinkHeight=this.getPreferredSize().height;  
  144.         think.setSize(thinkWidth,thinkHeight);  
  145.         think.setLocation((screenSize.width-thinkWidth)/2,(screenSize.height-thinkHeight)/2);  
  146.         // 设置窗口属性  
  147.         //think.setLocationRelativeTo(null);  
  148.         think.setLayout(new BorderLayout());  
  149.         think.setTitle("Think");  
  150.         think.setResizable(true);  
  151.         // 添加菜单栏  
  152.         think.setJMenuBar(menubar);  
  153.         menubar.add(filemenu);  
  154.         filemenu.setMnemonic('F');  
  155.         menubar.add(watchmenu);  
  156.         watchmenu.setMnemonic('V');  
  157.         menubar.add(bookmarkmenu);  
  158.         bookmarkmenu.setMnemonic('B');  
  159.         menubar.add(helpmenu);  
  160.         helpmenu.setMnemonic('H');  
  161.         // 添加菜单项  
  162.         filemenu.add(newwindow);  
  163.         // 添加键盘快捷方式  
  164.         newwindow.setMnemonic('N');  
  165.         newwindow.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,  
  166.                 InputEvent.CTRL_MASK));  
  167.         newwindow.addActionListener(new Action());  
  168.         filemenu.addSeparator();  
  169.         filemenu.add(openfile);  
  170.         openfile.setMnemonic('O');  
  171.         openfile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,  
  172.                 InputEvent.CTRL_MASK));  
  173.         openfile.addActionListener(new Action());  
  174.         filemenu.add(saveas);  
  175.         saveas.setMnemonic('S');  
  176.         saveas.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,  
  177.                 InputEvent.CTRL_MASK));  
  178.         saveas.addActionListener(new Action());  
  179.         filemenu.addSeparator();  
  180.         filemenu.add(quit);  
  181.         quit.setMnemonic('Q');  
  182.         quit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,  
  183.                 InputEvent.CTRL_MASK));  
  184.         quit.addActionListener(new Action());  
  185.         watchmenu.add(sourcecode);  
  186.         sourcecode.setMnemonic('U');  
  187.         sourcecode.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U,  
  188.                 InputEvent.CTRL_MASK));  
  189.         sourcecode.addActionListener(new Action());  
  190.         bookmarkmenu.add(addbookmark);  
  191.         addbookmark.setMnemonic('D');  
  192.         addbookmark.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,  
  193.                 InputEvent.CTRL_MASK));  
  194.         addbookmark.addActionListener(new Action());  
  195.         helpmenu.add(help);  
  196.         help.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1,  
  197.                 InputEvent.BUTTON1_MASK));  
  198.         help.addActionListener(new Action());  
  199.         helpmenu.addSeparator();  
  200.         helpmenu.add(about);  
  201.         about.addActionListener(new Action());  
  202.         // 添加工具按钮  
  203.         toolbar.add(bookmark);  
  204.         bookmark.setFocusable(false);  
  205.         bookmark.setEnabled(true);  
  206.         bookmark.setHorizontalTextPosition(SwingConstants.RIGHT);  
  207.         bookmark.setVerticalTextPosition(SwingConstants.CENTER);  
  208.         bookmark.addActionListener(new Action());  
  209.         toolbar.add(back);  
  210.         back.setFocusable(false);  
  211.         back.setEnabled(false);  
  212.         back.setHorizontalTextPosition(SwingConstants.RIGHT);  
  213.         back.setVerticalTextPosition(SwingConstants.CENTER);  
  214.         back.addActionListener(new Action());  
  215.         toolbar.add(forward);  
  216.         forward.setFocusable(false);  
  217.         forward.setEnabled(false);  
  218.         forward.setHorizontalTextPosition(SwingConstants.RIGHT);  
  219.         forward.setVerticalTextPosition(SwingConstants.CENTER);  
  220.         forward.addActionListener(new Action());  
  221.         toolbar.add(stop);  
  222.         stop.setFocusable(false);  
  223.         stop.setHorizontalTextPosition(SwingConstants.RIGHT);  
  224.         stop.setVerticalTextPosition(SwingConstants.CENTER);  
  225.         stop.addActionListener(new Action());  
  226.         toolbar.add(refresh);  
  227.         refresh.setFocusable(false);  
  228.         refresh.setHorizontalTextPosition(SwingConstants.RIGHT);  
  229.         refresh.setVerticalTextPosition(SwingConstants.CENTER);  
  230.         refresh.addActionListener(new Action());  
  231.         toolbar.add(home);  
  232.         home.setFocusable(false);  
  233.         home.setHorizontalTextPosition(SwingConstants.RIGHT);  
  234.         home.setVerticalTextPosition(SwingConstants.CENTER);  
  235.         home.addActionListener(new Action());  
  236.         toolbar.addSeparator();  
  237.         addressbox.add(addresslabel,BorderLayout.WEST);  
  238.         addressbox.add(urlfield,BorderLayout.CENTER);  
  239.         urlfield.setFocusable(true);  
  240.         urlfield.addActionListener(new Action());  
  241.         urlfield.addKeyListener(new KeyAction());  
  242.         addressbox.add(go,BorderLayout.EAST);  
  243.         go.setFocusable(false);  
  244.         go.setHorizontalTextPosition(SwingConstants.RIGHT);  
  245.         go.setVerticalTextPosition(SwingConstants.CENTER);  
  246.         go.addActionListener(new Action());  
  247.         toolbar.add(addressbox);  
  248.         scrollpane.setPreferredSize(new Dimension(800600));  
  249.         root.add(homepage);  
  250.         jtree.updateUI();  
  251.         jtree.addTreeSelectionListener(new TreeNodeChange());  
  252.         treescollpane.setViewportView(jtree);  
  253.         splitPane.setDividerLocation(0);  
  254.         splitPane.setOneTouchExpandable(true);  
  255.         splitPane.setDividerSize(0);  
  256.         splitPane.add(treescollpane);  
  257.         splitPane.add(scrollpane);  
  258.         // 获得内容面板,将菜单和工具栏添加到内容面板里面  
  259.         Container contentpane = getContentPane();  
  260.         // 将menubar放在框架的顶端  
  261.         setJMenuBar(menubar);  
  262.         contentpane.add(toolbar, BorderLayout.NORTH);  
  263.         contentpane.add(splitPane);  
  264.         //程序加载时载入书签文件   
  265.         try{  
  266.             File loadbookmarklist=new File("BookMarkList.txt");  
  267.             FileReader filereader=new FileReader(loadbookmarklist);  
  268.             BufferedReader bufferedreader=new BufferedReader(filereader);  
  269.             //临时存放网页标题  
  270.             String temp="";  
  271.             //临时存放网页URL  
  272.             String content="";  
  273.             String linesep=System.getProperty("line.separator");  
  274.             while((temp=bufferedreader.readLine())!=null)  
  275.             {  
  276.                 //由于每个标题对应一个网址,所以一次可以读取两行  
  277.                 content=bufferedreader.readLine();  
  278.                 //将书签重新添加到树节点上  
  279.                 DefaultMutableTreeNode treenode=new DefaultMutableTreeNode(temp);  
  280.                 root.add(treenode);  
  281.                 jtree.updateUI();  
  282.                 bookmarklist.addURL(temp, content);  
  283.             }  
  284.             bufferedreader.close();  
  285.             filereader.close();  
  286.         }catch(IOException event){  
  287.             event.printStackTrace();  
  288.             JOptionPane.showMessageDialog(MainUI.this"书签文件打开失败,可能是文件已丢失!","Think 提示您:",JOptionPane.ERROR_MESSAGE);  
  289.         }  
  290.         //设置窗口关闭监听事件  
  291.         this.addWindowListener(new WindowState());  
  292.         this.addWindowStateListener(new WindowState());  
  293.         setNewPage(0, urladdress);  
  294.     }  
  295.     //获得url地址的内容  
  296.     public String getContent(String urladdress)  
  297.     {  
  298.         String linesep;  
  299.         String line="";  
  300.         String tempsource="";  
  301.         linesep=System.getProperty("line.separator");  
  302.         try {  
  303.             //用输入输出流读取URL对象  
  304.             URL source=new URL(urladdress);  
  305.             URLConnection urlconnection=source.openConnection();  
  306.             String encoding=urlconnection.getContentEncoding();  
  307.             System.out.println(encoding);  
  308.             InputStream inputstream=urlconnection.getInputStream();  
  309.             //由于有的网页使用的字符集不同,导致有的网页源代码打开的时候出现乱码  
  310.             //此时utf-8可以使用,但是会导致保存之后的网页  
  311.             //在别的浏览器中打开的时候,依然是乱码  
  312.             InputStreamReader inputstreamreader=new InputStreamReader(inputstream,"utf-8");  
  313.             BufferedReader bufferedreader=new BufferedReader(inputstreamreader);  
  314.             while((line=bufferedreader.readLine())!=null)  
  315.             {  
  316.                 tempsource=tempsource+line+linesep;   
  317.             }  
  318.             //关闭流  
  319.             inputstream.close();  
  320.             bufferedreader.close();  
  321.         } catch (IOException e) {  
  322.             e.printStackTrace();  
  323.         }  
  324.         return tempsource;  
  325.     }  
  326.     //重设页面  
  327.     public void setNewPage(int flag,String urladdress)  
  328.     {  
  329.         String temp=getContent(urladdress);  
  330.         String title=getTitle(temp);  
  331.         try {  
  332.             urlfield.setText(urladdress);  
  333.             webpagepane.setEditable(false);  
  334.             webpagepane.setPage(urladdress);  
  335.             if(historylist.isPrePageNameExist(title))  
  336.             {  
  337.                 back.setEnabled(true);  
  338.             }  
  339.             if(historylist.isNextPageNameExist(title))  
  340.             {  
  341.                 forward.setEnabled(true);  
  342.             }  
  343.             urlfield.repaint();  
  344.             webpagepane.repaint();  
  345.         } catch (IOException e) {  
  346.             e.printStackTrace();  
  347.         }  
  348.         // 添加进历史链表  
  349.         if(flag==0)  
  350.         {  
  351.             historylist.addURL(title,urladdress);  
  352.         }  
  353.         webpagepane.addHyperlinkListener(new HTMLView());  
  354.     }  
  355.     //获取网页标题  
  356.     public String getTitle(String htmlcontent)  
  357.     {  
  358.         String temp="";  
  359.         String regex="<[Tt][Ii][Tt][Ll][Ee]>([^]*)";  
  360.         Pattern pat = Pattern.compile(regex);  
  361.         Matcher match = pat.matcher(htmlcontent);  
  362.         while (match.find()) {  
  363.             int start = match.start();  
  364.             int end = match.end();  
  365.             temp = htmlcontent.substring(start+7, end-1);  
  366.         }  
  367.         return temp;  
  368.     }  
  369.     //退出时处理书签文件的保存  
  370.     public void savebookmarklist()  
  371.     {  
  372.         File savefile=new File("BookMarkList.txt");  
  373.         String linesep=System.getProperty("line.separator");  
  374.         try{  
  375.             FileWriter filewriter=new FileWriter(savefile);  
  376.             BufferedWriter bufferedwriter=new BufferedWriter(filewriter);  
  377.             //将bookmarklist链表里面保存的记录一次写入文件  
  378.             String temppagename="";  
  379.             String tempurladdress="";  
  380.             PageNode head=bookmarklist.getPageNode().getNext();  
  381.             while(head!=null)  
  382.             {  
  383.                 //读取链表里面的内容  
  384.                 temppagename=head.getPagename();  
  385.                 temppagename=temppagename+linesep;  
  386.                 tempurladdress=head.getUrl();  
  387.                 tempurladdress=tempurladdress+linesep;  
  388.                 bufferedwriter.write(temppagename);  
  389.                 bufferedwriter.write(tempurladdress);  
  390.                 head=head.getNext();  
  391.             }  
  392.             bufferedwriter.close();  
  393.             filewriter.close();  
  394.         }catch(IOException event){  
  395.             JOptionPane.showMessageDialog(MainUI.this"书签列表保存失败!","Think 提示您:",JOptionPane.ERROR_MESSAGE);  
  396.         }  
  397.     }  
  398.     // 处理事件的类  
  399.     private class Action implements ActionListener {  
  400.         @Override  
  401.         public void actionPerformed(ActionEvent e) {  
  402.             // 处理菜单事件  
  403.             if (e.getSource() == newwindow) {  
  404.                 // 生成新窗体  
  405.                 ThinkFrame core=new ThinkFrame();  
  406.             }  
  407.             // 打开文件,由于不会解析HTML文件,只能以源代码的形式读出来了  
  408.             if (e.getSource() == openfile) {  
  409.                 openfilechooser.setDialogTitle("打开");  
  410.                 openfilechooser.setDialogType(JFileChooser.OPEN_DIALOG);  
  411.                 int result = openfilechooser.showOpenDialog(MainUI.this);  
  412.                 File fileopen=openfilechooser.getSelectedFile();  
  413.                 String openfilename=fileopen.getPath();  
  414.                 System.out.println(openfilename);  
  415.                 if (result == JFileChooser.APPROVE_OPTION) {  
  416.                     // 从硬盘目录下读取文件  
  417.                     try{  
  418.                         FileReader filereader=new FileReader(openfilename);  
  419.                         BufferedReader bufferedreader=new BufferedReader(filereader);  
  420.                         String temp="";  
  421.                         String content="";  
  422.                         String linesep=System.getProperty("line.separator");  
  423.                         while((temp=bufferedreader.readLine())!=null)  
  424.                         {  
  425.                             content=content+temp+linesep;  
  426.                         }  
  427.                         bufferedreader.close();  
  428.                         filereader.close();  
  429.                         urlfield.setText(openfilename);  
  430.                         urlfield.revalidate();  
  431.                         webpagepane.setText(content);  
  432.                         webpagepane.revalidate();  
  433.                     }catch(IOException event){  
  434.                         event.printStackTrace();  
  435.                         JOptionPane.showMessageDialog(MainUI.this"试图打开源代码文件失败!","Think 提示您:",JOptionPane.ERROR_MESSAGE);  
  436.                     }  
  437.                 }  
  438.             }  
  439.             // 页面另存为  
  440.             if (e.getSource() == saveas) {  
  441.                 urladdress = urlfield.getText().toString().trim();  
  442.                 //设置弹出对话框类型  
  443.                 saveasfilechooser.setDialogTitle("另存为...");  
  444.                 saveasfilechooser.setDialogType(JFileChooser.SAVE_DIALOG);  
  445.                 int result = saveasfilechooser.showSaveDialog(MainUI.this);  
  446.                 //获取文件名  
  447.                 File filesave=saveasfilechooser.getSelectedFile();  
  448.                 String savefilename=filesave.getPath();  
  449.                 savefilename=savefilename+".html";  
  450.                 if (result == JFileChooser.APPROVE_OPTION) {  
  451.                     try{  
  452.                         //将文件存盘  
  453.                         String temp=getContent(urladdress);  
  454.                         FileWriter filewriter=new FileWriter(savefilename);  
  455.                         BufferedWriter bufferedwriter=new BufferedWriter(filewriter);  
  456.                         bufferedwriter.write(temp);  
  457.                         bufferedwriter.close();  
  458.                         filewriter.close();  
  459.                     }catch(IOException event){  
  460.                         JOptionPane.showMessageDialog(MainUI.this"Web页面保存失败!","Think 提示您:",JOptionPane.ERROR_MESSAGE);  
  461.                     }  
  462.                 }  
  463.             }  
  464.             // 退出浏览器  
  465.             if (e.getSource() == quit) {  
  466.                 if (JOptionPane.showConfirmDialog(MainUI.this"确定要退出吗?",  
  467.                         "Think 提示您:", JOptionPane.OK_CANCEL_OPTION) == 0) {  
  468.                     //退出时将书签记录写入文件  
  469.                     savebookmarklist();  
  470.                     System.exit(0);  
  471.                 } else {  
  472.                     // Do Nothing  
  473.                 }  
  474.             }  
  475.             // 查看源代码  
  476.             if (e.getSource() == sourcecode) {  
  477.                 urladdress = urlfield.getText().toString().trim();  
  478.                 if (urladdress.length() > 0  
  479.                         && !urladdress.startsWith("http://")) {  
  480.                     urladdress = "http://"+urladdress;  
  481.                 }  
  482.                 if(!urladdress.equals(""))  
  483.                 {  
  484.                     String temp=getContent(urladdress);  
  485.                     String title=getTitle(temp);  
  486.                     temp=getContent(urladdress);  
  487.                     //建立源代码窗体对象  
  488.                     JMPWindow viewsourcecode=new JMPWindow(title,temp);  
  489.                     viewsourcecode.setVisible(true);  
  490.                 }  
  491.                 else if (urladdress.length() == 0) {  
  492.                     JOptionPane.showMessageDialog(MainUI.this"对不起,你输入了空地址,这是非法操作!","Think 提示您:",JOptionPane.WARNING_MESSAGE);  
  493.                 }  
  494.             }  
  495.             // 添加页面到书签  
  496.             if (e.getSource() == addbookmark)   
  497.             {  
  498.                 urladdress = urlfield.getText().toString().trim();  
  499.                 String temp=getContent(urladdress);  
  500.                 String title=getTitle(temp);  
  501.                 if(title!=""&&urladdress!="")  
  502.                 {  
  503.                     if (bookmarklist.isPageNameExist(title) == false)   
  504.                     {  
  505.                         bookmarklist.addURL(title,urladdress);  
  506.                         System.out.println("书签");  
  507.                         System.out.println(title);  
  508.                         System.out.println(urladdress);  
  509.                         //生成收藏夹窗口对象  
  510.                         AddBookMark addbookmarkdone=new AddBookMark(title,urladdress,false);  
  511.                         addbookmarkdone.setVisible(true);  
  512.                         DefaultMutableTreeNode node=new DefaultMutableTreeNode(title);  
  513.                         root.add(node);  
  514.                         jtree.updateUI();  
  515.                     }   
  516.                     else if(bookmarklist.isPageNameExist(title) == true)  
  517.                     {  
  518.                         AddBookMark addbookmarkdone=new AddBookMark(title,urladdress,true);  
  519.                         addbookmarkdone.setVisible(true);  
  520.                     }  
  521.                 }  
  522.                 else  
  523.                 {  
  524.                     JOptionPane.showMessageDialog(MainUI.this"添加书签标题或者网址不能为空!","Think 提示您:",JOptionPane.WARNING_MESSAGE);  
  525.                 }  
  526.             }  
  527.             // 打开帮助文档  
  528.             if (e.getSource() == help) {  
  529.                 // 待写一个chm格式的文档  
  530.                 JMPWindow viewhelp=new JMPWindow();  
  531.                 viewhelp.setVisible(true);  
  532.             }  
  533.             // 打开关于窗口  
  534.             if (e.getSource() == about) {  
  535.                 JOptionPane.showMessageDialog(MainUI.this"Think version 0.1"  
  536.                         + "/n" + "谢谢您的使用!""关于 Think",  
  537.                         JOptionPane.INFORMATION_MESSAGE);  
  538.             }  
  539.             // 单击书签按钮事件:显示/隐藏  
  540.             if(e.getSource()==bookmark)  
  541.             {  
  542.                 if(splitPane.getDividerLocation()==250)  
  543.                 {  
  544.                     splitPane.setDividerLocation(0);  
  545.                 }  
  546.                 else  
  547.                 {  
  548.                     splitPane.setDividerLocation(250);  
  549.                 }  
  550.             }  
  551.             // 后退事件  
  552.             if (e.getSource() == back) {  
  553.                 urladdress = urlfield.getText().toString().trim();  
  554.                 try {  
  555.                     // 获得历史链表中的前一个URL地址  
  556.                     String pagename="";  
  557.                     if(historylist.isPrePageNameExist(urladdress))  
  558.                     {  
  559.                         pagename= historylist.getPrePageName(pagename);  
  560.                         urladdress=historylist.getURL(pagename);  
  561.                         MainUI.this.setNewPage(1,urladdress);  
  562.                     }  
  563.                 }catch (Exception event) {  
  564.                     event.printStackTrace();  
  565.                     JOptionPane.showMessageDialog(MainUI.this"网络连接失败!","Think 提示您:",JOptionPane.ERROR_MESSAGE);  
  566.                 }  
  567.             }  
  568.             // 前进事件  
  569.             if (e.getSource() == forward) {  
  570.                 urladdress = urlfield.getText().toString().trim();  
  571.                 try {  
  572.                     String pagename="";  
  573.                     if(historylist.isNextPageNameExist(urladdress))  
  574.                     {  
  575.                         // 获得历史链表中的下一个URL地址  
  576.                         pagename= historylist.getNextPageName(pagename);  
  577.                         System.out.println(pagename);  
  578.                         urladdress=historylist.getURL(pagename);  
  579.                         MainUI.this.setNewPage(1,urladdress);  
  580.                     }  
  581.                 } catch (Exception event) {  
  582.                     event.printStackTrace();  
  583.                     JOptionPane.showMessageDialog(MainUI.this"网络连接失败!","Think 提示您:",JOptionPane.ERROR_MESSAGE);  
  584.                 }  
  585.             }  
  586.             //停止事件  
  587.             if (e.getSource() == stop) {  
  588.                 //将页面置空  
  589.                 urladdress = "";  
  590.                 MainUI.this.setNewPage(1,urladdress);  
  591.             }  
  592.             // 刷新事件  
  593.             if (e.getSource() == refresh) {  
  594.                 urladdress = urlfield.getText().toString().trim();  
  595.                 MainUI.this.setNewPage(1,urladdress);  
  596.             }  
  597.             // 回到主页  
  598.             if (e.getSource() == home) {  
  599.                 urladdress =  "http://www.baidu.com";  
  600.                 MainUI.this.setNewPage(0,urladdress);  
  601.             }  
  602.             // 单击go或者回车事件  
  603.             if ((e.getSource() == go) || (e.getSource() == urlfield)) {  
  604.                 urladdress = urlfield.getText().toString().trim();  
  605.                 if (urladdress.length() > 0 && urladdress.startsWith("http://")) {  
  606.                     MainUI.this.setNewPage(0,urladdress);  
  607.                 } else if (urladdress.length() > 0  
  608.                         && !urladdress.startsWith("http://")) {  
  609.                     urladdress = "http://"+urladdress;  
  610.                     MainUI.this.setNewPage(0,urladdress);  
  611.                 } else if (urladdress.length() == 0) {  
  612.                     JOptionPane.showMessageDialog(MainUI.this"对不起,你输入了空地址,这是非法操作!","Think 提示您:",JOptionPane.WARNING_MESSAGE);  
  613.                 }  
  614.             }  
  615.         }  
  616.     }  
  617.     // 实现HyperlinkListener方法,实现页面内的超链接  
  618.     private class HTMLView implements HyperlinkListener {  
  619.         @Override  
  620.         public void hyperlinkUpdate(HyperlinkEvent event) {  
  621.             if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {  
  622.                 JEditorPane pane = (JEditorPane) event.getSource();  
  623.                 if (event instanceof HTMLFrameHyperlinkEvent) {  
  624.                     //处理超链接  
  625.                     HTMLFrameHyperlinkEvent htmlframehyperlinkevent = (HTMLFrameHyperlinkEvent) event;  
  626.                     HTMLDocument htmldocument = (HTMLDocument) pane.getDocument();  
  627.                     htmldocument.processHTMLFrameHyperlinkEvent(htmlframehyperlinkevent);  
  628.                 } else {  
  629.                     try {  
  630.                         String linkurl = event.getURL().toString();  
  631.                         String temp=getContent(linkurl);  
  632.                         String title=getTitle(temp);  
  633.                         pane.setPage(linkurl);  
  634.                         pane.setEditable(false);  
  635.                         urlfield.setText(linkurl);  
  636.                         urlfield.revalidate();  
  637.                         pane.invalidate();  
  638.                         webpagepane.addHyperlinkListener(new HTMLView());  
  639.                         historylist.addURL(title,linkurl);  
  640.                     } catch (IOException t) {  
  641.                         t.printStackTrace();  
  642.                         JOptionPane.showMessageDialog(MainUI.this"网络连接失败!","Think 提示您:",JOptionPane.ERROR_MESSAGE);  
  643.                     }  
  644.                 }  
  645.             }  
  646.         }  
  647.     }  
  648.     //键盘监听事件类  
  649.     private class KeyAction extends KeyAdapter  
  650.     {  
  651.         public void keyPressed(KeyEvent e) {  
  652.             char key=e.getKeyChar();  
  653.             //按下回车键  
  654.             if(key=='/n')  
  655.             {  
  656.                 urladdress = urlfield.getText().toString().trim();  
  657.                 if (urladdress.length() > 0 && urladdress.startsWith("http://")) {  
  658.                     MainUI.this.setNewPage(0,urladdress);  
  659.                 } else if (urladdress.length() > 0  
  660.                         && !urladdress.startsWith("http://")) {  
  661.                     urladdress = "http://"+urladdress;  
  662.                     MainUI.this.setNewPage(0,urladdress);  
  663.                 } else if (urladdress.length() == 0) {  
  664.                     JOptionPane.showMessageDialog(MainUI.this"对不起,你输入了空地址,这是非法操作!","Think 提示您:",JOptionPane.WARNING_MESSAGE);  
  665.                 }  
  666.             }  
  667.         }  
  668.     }  
  669.     //监听树节点改变的事件  
  670.     private class TreeNodeChange implements TreeSelectionListener {  
  671.         @Override  
  672.         public void valueChanged(TreeSelectionEvent e) {  
  673.             JTree tree=(JTree)e.getSource();  
  674.             DefaultMutableTreeNode selectednode=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();  
  675.             String nodename=selectednode.toString();  
  676.             if(selectednode.isLeaf())  
  677.             {  
  678.                 if(bookmarklist.isPageNameExist(nodename))  
  679.                 {     
  680.                     //获取节点名字  
  681.                     String tempurl=bookmarklist.getURL(nodename);  
  682.                     setNewPage(0,tempurl);  
  683.                 }  
  684.                 else if(nodename.equalsIgnoreCase("主页"))  
  685.                 {  
  686.                     String tempurl="http://www.baidu.com";  
  687.                     setNewPage(0,tempurl);  
  688.                 }  
  689.             }  
  690.         }  
  691.     }  
  692.     //鉴定窗口大小改变的类  
  693.     private class WindowState extends WindowAdapter  
  694.     {  
  695.         public void windowClosing(WindowEvent e)  
  696.         {  
  697.             if (JOptionPane.showConfirmDialog(MainUI.this"确定要退出吗?",  
  698.                     "Think 提示您:", JOptionPane.OK_CANCEL_OPTION) == 0) {  
  699.                 //退出时将书签记录写入文件  
  700.                 savebookmarklist();  
  701.                 System.exit(0);  
  702.             } else {  
  703.                 // Do Nothing  
  704.             }  
  705.         }  
  706.         public void windowStateChanged(WindowEvent e)  
  707.         {  
  708.             splitPane.setDividerSize(0);  
  709.         }  
  710.     }  
  711. }  
 

[java]  view plain copy
  1. /* 
  2.  * Name:JMPWindow.java 
  3.  * Writer:bitsjx 
  4.  * Date:2009-12-07 
  5.  * Time:00:20 
  6.  * Function:Use class MainUI to implement a JMPWindow GUI 
  7.  * */  
  8. package ui;  
  9. import java.awt.AWTEvent;  
  10. import java.awt.BorderLayout;  
  11. import java.awt.Dimension;  
  12. import java.awt.FlowLayout;  
  13. import java.awt.event.ActionEvent;  
  14. import java.awt.event.ActionListener;  
  15. import java.io.BufferedReader;  
  16. import java.io.BufferedWriter;  
  17. import java.io.File;  
  18. import java.io.FileNotFoundException;  
  19. import java.io.FileReader;  
  20. import java.io.FileWriter;  
  21. import java.io.IOException;  
  22. import javax.swing.JButton;  
  23. import javax.swing.JFileChooser;  
  24. import javax.swing.JFrame;  
  25. import javax.swing.JOptionPane;  
  26. import javax.swing.JPanel;  
  27. import javax.swing.JScrollPane;  
  28. import javax.swing.JTextArea;  
  29. public class JMPWindow extends JFrame implements ActionListener  
  30. {  
  31.     //面板容器  
  32.     JPanel contentpane;  
  33.     //两个单独的面板  
  34.     JPanel textpanel=new JPanel();  
  35.     JPanel buttonpanel=new JPanel();  
  36.     //声明文本区域用于显示源代码  
  37.     JTextArea textarea=new JTextArea();  
  38.     JScrollPane scrollpane=new JScrollPane();  
  39.     //保存和关闭按钮  
  40.     JButton closebutton=new JButton("关闭");  
  41.     JButton savebutton=new JButton("保存");  
  42.     //源文件  
  43.     String sourcecode="";  
  44.     //页面名称  
  45.     String pageName="";  
  46.     public JMPWindow()  
  47.     {  
  48.         //设置窗口属性  
  49.         this.enableEvents(AWTEvent.WINDOW_EVENT_MASK);  
  50.         this.setSize(new Dimension(800,600));  
  51.         this.setTitle("帮助");  
  52.         this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  
  53.         //添加面板  
  54.         contentpane=(JPanel)getContentPane();  
  55.         contentpane.setLayout(new BorderLayout());  
  56.         scrollpane.getViewport().add(textarea);  
  57.         textpanel.setLayout(new BorderLayout());  
  58.         textpanel.add(scrollpane,BorderLayout.CENTER);  
  59.         contentpane.add(textpanel,BorderLayout.CENTER);  
  60.         //设置按钮区域  
  61.         buttonpanel.setLayout(new FlowLayout());  
  62.         buttonpanel.add(closebutton);  
  63.         closebutton.setFocusable(true);  
  64.         contentpane.add(buttonpanel,BorderLayout.SOUTH);  
  65.         String linesep="";  
  66.         linesep=System.getProperty("line.separator");  
  67.         String readme="";  
  68.         String temp="";  
  69.         try {  
  70.             //读取readme文件  
  71.             File readmefile=new File("readme.txt");  
  72.             FileReader filereader;  
  73.             filereader = new FileReader(readmefile);  
  74.             BufferedReader bufferedreader=new BufferedReader(filereader);  
  75.             while((temp=bufferedreader.readLine())!=null)  
  76.             {  
  77.                 readme=readme+temp+linesep;  
  78.             }  
  79.         } catch (FileNotFoundException e) {  
  80.             e.printStackTrace();  
  81.         } catch (IOException e) {  
  82.             JOptionPane.showMessageDialog(JMPWindow.this,"读取帮助文件出错!","Think 提示您:",JOptionPane.ERROR_MESSAGE);  
  83.         }  
  84.         //readme不可编辑  
  85.         this.textarea.setEditable(false);  
  86.         this.textarea.setText(readme);  
  87.         closebutton.addActionListener(this);  
  88.     }  
  89.     public JMPWindow(String title,String urladdress)  
  90.     {  
  91.         //设置窗口属性  
  92.         this.enableEvents(AWTEvent.WINDOW_EVENT_MASK);  
  93.         this.sourcecode=urladdress;  
  94.         this.setSize(new Dimension(800,600));  
  95.         this.setTitle("源代码");  
  96.         this.pageName=title;  
  97.         this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  
  98.         //添加面板  
  99.         contentpane=(JPanel)getContentPane();  
  100.         contentpane.setLayout(new BorderLayout());  
  101.         scrollpane.getViewport().add(textarea);  
  102.         textpanel.setLayout(new BorderLayout());  
  103.         textpanel.add(scrollpane,BorderLayout.CENTER);  
  104.         contentpane.add(textpanel,BorderLayout.CENTER);  
  105.         //设置按钮区域  
  106.         buttonpanel.setLayout(new FlowLayout());  
  107.         buttonpanel.add(savebutton);  
  108.         savebutton.setFocusable(true);  
  109.         buttonpanel.add(closebutton);  
  110.         closebutton.setFocusable(false);  
  111.         contentpane.add(buttonpanel,BorderLayout.SOUTH);  
  112.         this.textarea.setEditable(false);  
  113.         this.textarea.setText(sourcecode);  
  114.         savebutton.addActionListener(this);  
  115.         closebutton.addActionListener(this);  
  116.     }  
  117.     //响应按钮事件  
  118.     @Override  
  119.     public void actionPerformed(ActionEvent e) {  
  120.         String url="";  
  121.         if(e.getSource()==closebutton)  
  122.         {  
  123.             dispose();  
  124.         }  
  125.         else if(e.getSource()==savebutton)  
  126.         {  
  127.             //若单击保存按钮,则弹出保存对话框  
  128.             JFileChooser filechooser=new JFileChooser();  
  129.             filechooser.setDialogType(JFileChooser.SAVE_DIALOG);  
  130.             int returnvalue=filechooser.showSaveDialog(JMPWindow.this);  
  131.             File savefile=filechooser.getSelectedFile();  
  132.             String savefilename=savefile.getPath();  
  133.             savefilename=savefilename+".html";  
  134.             try{  
  135.                 FileWriter filewriter=new FileWriter(savefilename);  
  136.                 BufferedWriter bufferedwriter=new BufferedWriter(filewriter);  
  137.                 bufferedwriter.write(textarea.getText());  
  138.                 bufferedwriter.close();  
  139.                 filewriter.close();  
  140.             }catch(IOException event){  
  141.                 JOptionPane.showMessageDialog(JMPWindow.this"保存失败!","Think 提示您:",JOptionPane.ERROR_MESSAGE);  
  142.             }  
  143.         }  
  144.     }  
  145. }  
 

[java]  view plain copy
  1. /* 
  2.  * Name:AddBookMark.java 
  3.  * Writer:bitsjx 
  4.  * Date:2009-12-07 
  5.  * Time:00:20 
  6.  * Function:Use class MainUI to implement a AddBookMark GUI 
  7.  * */  
  8. package ui;  
  9. import java.awt.AWTEvent;  
  10. import java.awt.BorderLayout;  
  11. import java.awt.Dimension;  
  12. import java.awt.FlowLayout;  
  13. import java.awt.event.ActionEvent;  
  14. import java.awt.event.ActionListener;  
  15. import javax.swing.JButton;  
  16. import javax.swing.JFrame;  
  17. import javax.swing.JLabel;  
  18. import javax.swing.JOptionPane;  
  19. import javax.swing.JPanel;  
  20. import javax.swing.JTextField;  
  21. public class AddBookMark extends JFrame implements ActionListener {  
  22.     //定义组件  
  23.     JPanel contentpane;  
  24.     //添加文本区域的面板  
  25.     JPanel textpanel=new JPanel();  
  26.     //添加按钮的面板  
  27.     JPanel buttonpanel=new JPanel();  
  28.     //名称、地址  
  29.     JLabel namelabel=new JLabel("名称");  
  30.     JLabel addresslabel=new JLabel("地址");  
  31.     //显示名称和地址的文本框  
  32.     JTextField nametext=new JTextField(20);  
  33.     JTextField addresstext=new JTextField(20);  
  34.     //确定、取消按钮  
  35.     JButton OKbuuton=new JButton("确定");  
  36.     JButton Cancelbuuton=new JButton("取消");  
  37.     //isExist用于判断是否已经存在书签项  
  38.     boolean isExist=false;  
  39.     public AddBookMark(String title,String urladdress,boolean isExist)  
  40.     {  
  41.         //设置窗口属性  
  42.         setLocationRelativeTo(null);  
  43.         setResizable(false);  
  44.         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  
  45.         this.enableEvents(AWTEvent.WINDOW_EVENT_MASK);  
  46.         this.setSize(new Dimension(300,150));  
  47.         this.setTitle(" 收藏夹");  
  48.         this.isExist=isExist;  
  49.         //设置窗体属性  
  50.         contentpane=(JPanel)this.getContentPane();  
  51.         contentpane.setLayout(new BorderLayout());  
  52.         textpanel.setLayout(new FlowLayout());  
  53.         textpanel.add(namelabel);  
  54.         textpanel.add(nametext);  
  55.         nametext.setEnabled(true);  
  56.         nametext.setText(title);  
  57.         nametext.setFocusable(true);  
  58.         textpanel.add(addresslabel);  
  59.         textpanel.add(addresstext);  
  60.         addresstext.setEditable(true);  
  61.         addresstext.setText(urladdress);  
  62.         buttonpanel.setLayout(new FlowLayout());  
  63.         buttonpanel.add(OKbuuton);  
  64.         OKbuuton.setFocusable(false);  
  65.         buttonpanel.add(Cancelbuuton);  
  66.         Cancelbuuton.setFocusable(false);  
  67.         contentpane.add(textpanel,BorderLayout.CENTER);  
  68.         contentpane.add(buttonpanel,BorderLayout.SOUTH);  
  69.         OKbuuton.addActionListener(this);  
  70.         Cancelbuuton.addActionListener(this);  
  71.     }  
  72.     //响应按钮事件  
  73.     @Override  
  74.     public void actionPerformed(ActionEvent e) {  
  75.         if(e.getSource()==Cancelbuuton)  
  76.         {  
  77.             dispose();  
  78.         }  
  79.         if(e.getSource()==OKbuuton)  
  80.         {  
  81.             //如果存在了书签项  
  82.             if(this.isExist==true)  
  83.             {  
  84.                 if (JOptionPane.showConfirmDialog(AddBookMark.this"项目已存在,是否覆盖?",  
  85.                         "Think 提示您:", JOptionPane.OK_CANCEL_OPTION)==0) {  
  86.                 }  
  87.             }  
  88.             dispose();  
  89.         }  
  90.     }  
  91. }  
 

[java]  view plain copy
  1. /* 
  2.  * Name:PageList.java 
  3.  * Writer:bitsjx 
  4.  * Date:2009-12-07 
  5.  * Time:00:26 
  6.  * Function:Construct a LinkList to save History and BookMarks Nodes 
  7.  * */  
  8. package core;  
  9. public class PageList {  
  10.     //头节点  
  11.     private PageNode head;  
  12.     //自由节点  
  13.     private PageNode link;  
  14.     //记录当前的节点所在位置  
  15.     private PageNode linkpointer;  
  16.     public PageList()  
  17.     {  
  18.         head=new PageNode();  
  19.         link=head;  
  20.         linkpointer=head;  
  21.     }  
  22.     //添加新的页面到链表  
  23.     public void addURL(String pagename,String urladdress)  
  24.     {  
  25.         PageNode node=new PageNode();  
  26.         link=head;  
  27.         while(link.getNext()!=null)  
  28.         {  
  29.             link=link.getNext();  
  30.         }  
  31.         node.setPagename(pagename);  
  32.         node.setUrl(urladdress);  
  33.         link.setNext(node);  
  34.         node.setPre(link);  
  35.         node.setNext(null);  
  36.         linkpointer=node;  
  37.     }  
  38.     //获得链表头  
  39.     public PageNode getPageNode()  
  40.     {  
  41.         return this.head;  
  42.     }  
  43.       
  44.     //查找某个pagename对应的URL  
  45.     public String getURL(String pagename)  
  46.     {  
  47.         link=head;  
  48.         String url="";  
  49.         while(!((link.getNext()).getPagename()).equalsIgnoreCase(pagename))  
  50.         {  
  51.             link=link.getNext();  
  52.         }  
  53.         url=(link.getNext()).getUrl();  
  54.         linkpointer=link;  
  55.         return url;  
  56.     }  
  57.       
  58.     //获取某个URL对应的pagename  
  59.     public String getPageName(String urladdress)  
  60.     {  
  61.         link=head;  
  62.         String pagename="";  
  63.         while(!(link.getNext().getUrl()).equalsIgnoreCase(urladdress))  
  64.         {  
  65.             link=link.getNext();  
  66.         }  
  67.         pagename=link.getNext().getPagename();  
  68.         linkpointer=link;  
  69.         return pagename;  
  70.     }  
  71.     //获取当前pagename的前一个pagename  
  72.     public String getPrePageName(String pagename)  
  73.     {  
  74.         link=linkpointer.getPre();  
  75.         linkpointer=link;  
  76.         return link.getPagename();  
  77.     }  
  78.     //获取当前pagename的下一个pagename  
  79.     public String getNextPageName(String pagename)  
  80.     {  
  81.         link=linkpointer.getNext();  
  82.         linkpointer=link;  
  83.         return link.getPagename();  
  84.     }  
  85.     //查找某个pagename地址是否存在  
  86.     public boolean isPageNameExist(String pagename)  
  87.     {  
  88.         link=this.head.getNext();  
  89.         //标记是否找到  
  90.         boolean isfind=false;  
  91.         String pageName="";  
  92.         while(link!=null)  
  93.         {  
  94.             pageName=link.getPagename();  
  95.             if(pageName.equalsIgnoreCase(pagename))  
  96.             {  
  97.                 isfind=true;  
  98.                 break;  
  99.             }  
  100.             link=link.getNext();  
  101.         }  
  102.         System.out.println("查找本页结果:"+isfind);  
  103.         return isfind;  
  104.     }  
  105.     //判断某个pagename的前一个pagename是否存在  
  106.     public boolean isPrePageNameExist(String pagename)  
  107.     {  
  108.         boolean isfind=false;  
  109.         link=head;  
  110.         link=linkpointer.getPre();  
  111.         if(link!=null&&link!=head)  
  112.         {  
  113.             isfind=true;  
  114.         }  
  115.         System.out.println("查找前页结果:"+isfind);  
  116.         return isfind;  
  117.     }  
  118.     //判断某个pagename的后一个pagename是否存在  
  119.     public boolean isNextPageNameExist(String pagename)  
  120.     {  
  121.         boolean isfind=false;  
  122.         link=head;  
  123.         link=linkpointer.getNext();  
  124.         if(link!=null)  
  125.         {  
  126.             isfind=true;  
  127.         }  
  128.         System.out.println("查找后页结果:"+isfind);  
  129.         return isfind;  
  130.     }  
  131. }  
 

[java]  view plain copy
  1. /* 
  2.  * Name:PageNode.java 
  3.  * Writer:bitsjx 
  4.  * Date:2009-12-07 
  5.  * Time:00:20 
  6.  * Function:implement a Node of LinkList 
  7.  * */  
  8. package core;  
  9. public class PageNode {  
  10.     //页面的名称  
  11.     private String pagename="";  
  12.     //url为网址实例   
  13.     private String url="";  
  14.     //next为后一个节点  
  15.     private PageNode next=null;  
  16.     //pre为前一个节点  
  17.     private PageNode pre=null;  
  18.       
  19.     //get、set方法  
  20.     public String getPagename() {  
  21.         return pagename;  
  22.     }  
  23.     public void setPagename(String pagename) {  
  24.         this.pagename = pagename;  
  25.     }  
  26.     public String getUrl() {  
  27.         return url;  
  28.     }  
  29.     public void setUrl(String url) {  
  30.         this.url = url;  
  31.     }  
  32.     public PageNode getNext() {  
  33.         return next;  
  34.     }  
  35.     public void setNext(PageNode next) {  
  36.         this.next = next;  
  37.     }  
  38.     public PageNode getPre() {  
  39.         return pre;  
  40.     }  
  41.     public void setPre(PageNode pre) {  
  42.         this.pre = pre;  
  43.     }  
  44.       
  45.     //重写默认构造函数  
  46.     public PageNode()  
  47.     {  
  48.         this.pagename="";  
  49.         this.url="";  
  50.         this.next=null;  
  51.         this.pre=null;  
  52.     }  
  53.     //自定义构造函数  
  54.     public PageNode(String pagename,String urladdress)  
  55.     {  
  56.         this.pagename=pagename;  
  57.         this.url=urladdress;  
  58.         this.next=null;  
  59.         this.pre=null;  
  60.     }  
  61. }  
 

[java]  view plain copy
  1. /* 
  2.  * Name:ThinkFrame.java 
  3.  * Writer:bitsjx 
  4.  * Date:2009-12-07 
  5.  * Time:00:20 
  6.  * Function:Use class MainUI to implement a GUI 
  7.  * */  
  8. package core;  
  9. import ui.MainUI;  
  10. import javax.swing.JOptionPane;  
  11. import javax.swing.UIManager;  
  12. import javax.swing.UnsupportedLookAndFeelException;  
  13. import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;  
  14. public class ThinkFrame {  
  15.     public ThinkFrame()   
  16.     {  
  17.         //设置窗口外观  
  18.         try {  
  19.             UIManager.setLookAndFeel(new com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel());  
  20.         } catch (UnsupportedLookAndFeelException e) {  
  21.             e.printStackTrace();  
  22.         } catch (Exception e){  
  23.             e.printStackTrace();  
  24.             JOptionPane.showMessageDialog(new MainUI(), "GUI初始化失败!","Think 提示您:",JOptionPane.ERROR_MESSAGE);  
  25.         }  
  26.           
  27.         MainUI ui=new MainUI();  
  28.         ui.pack();  
  29.         ui.setVisible(true);  
  30.     }  
  31.     public static void main(String[] args) {  
  32.         ThinkFrame core=new ThinkFrame();  
  33.     }  
  34. }  
 

时间算是久远了,好多都忘了,大致上就是这样的了,欢迎拍砖哈!

贴图

你可能感兴趣的:(java,se)