java網(wǎng)頁瀏覽器設(shè)計課程設(shè)計
《java網(wǎng)頁瀏覽器設(shè)計課程設(shè)計》由會員分享,可在線閱讀,更多相關(guān)《java網(wǎng)頁瀏覽器設(shè)計課程設(shè)計(8頁珍藏版)》請?jiān)谘b配圖網(wǎng)上搜索。
1、 java網(wǎng)頁瀏覽器設(shè)計-課程設(shè)計 java網(wǎng)頁瀏覽器設(shè)計 /* **網(wǎng)頁瀏覽器主程序 **WebBrowser.java */ import java.awt.*; import javax.swing.*; import javax.swing.text.*; import java.awt.event.*; import javax.swing.event.*; import javax.swing.border.*; import javax.swing.filechooser.FileFilter; import jav
2、ax.swing.filechooser.FileView; import java.io.*; import .*; import java.util.*; public class WebBrowser extends JFrame implements HyperlinkListener,ActionListener{ ??? //建立工具欄用來顯示地址欄 ??? JToolBar bar=new JToolBar (); ??? ??? //建立網(wǎng)頁顯示界面 ??? JTextField jurl = new JTextField (60);
3、 ??? JEditorPane jEditorPane1 = new JEditorPane (); ??? JScrollPane scrollPane = new JScrollPane (jEditorPane1); ??? ??? JFileChooser chooser=new JFileChooser (); ??? JFileChooser chooser1=new JFileChooser (); ??? String htmlSource; ??? JWindow window = new JWindow (WebBrowser.this); ??
4、? ??? JButton button2=new JButton ("窗口還原"); ??? Toolkit toolkit = Toolkit.getDefaultToolkit(); ??? ??? //建立菜單欄 ??? JMenuBar jMenuBar1 = new JMenuBar(); ??? //建立菜單組 ??? JMenu fileMenu = new JMenu ("文件(F)"); ??? //建立菜單項(xiàng) ??? JMenuItem saveAsItem = new JMenuItem ("另存為(A)..."); ??? JMenuIte
5、m exitItem=new JMenuItem ("退出(I)"); ??? ??? JMenu editMenu=new JMenu ("編輯(E)"); ??? JMenuItem backItem=new JMenuItem ("后退"); ??? JMenuItem forwardItem=new JMenuItem ("前進(jìn)"); ??? ??? JMenu viewMenu=new JMenu ("視圖(V)"); ??? JMenuItem fullscreenItem=new JMenuItem ("全屏(U)"); ??? JMenuItem sour
6、ceItem=new JMenuItem ("查看源碼(C)"); ??? JMenuItem reloadItem=new JMenuItem ("刷新(R)"); ??? //建立工具欄 ??? JToolBar toolBar = new JToolBar(); ??? //建立工具欄中的按鈕組件 ??? JButton picSave = new JButton("另存為"); ??? JButton picBack = new JButton("后退"); ??? JButton picForward = new JButton("前進(jìn)"); ??? JButto
7、n picView = new JButton("查看源代碼"); ??? JButton picExit = new JButton("退出"); ??? JLabel label=new JLabel ("地址"); ??? JButton button=new JButton ("轉(zhuǎn)向"); ??? ??? Box adress=Box.createHorizontalBox ();??? ??? //ArrayList對象,用來存放歷史地址 ??? private ArrayList history=new ArrayList(); ??? //整型變量,
8、表示歷史地址的訪問順序 ??? private int historyIndex; ??? /** ??? **構(gòu)造函數(shù) ??? **初始化圖形用戶界面 ??? */ ??? public WebBrowser(){ ??????? ??????? setTitle ("網(wǎng)頁瀏覽器"); ??????? setResizable(false); ??????? setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ??????????????????? ??????? //為jEditorPane1添加事件偵聽 ?
9、?????? jEditorPane1.addHyperlinkListener (this); ??????? ??????? //為組件fileMenu設(shè)置熱鍵‘F’ ??????? fileMenu.setMnemonic('F'); ??????? ??????? saveAsItem.setMnemonic ('S'); ??????? //為“另存為”組件設(shè)置快捷鍵為ctrl+s ??????? saveAsItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_S,InputEvent.CTRL_MA
10、SK)); ??????? ??????? exitItem.setMnemonic('Q'); ??????? exitItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_E,InputEvent.CTRL_MASK)); ??????? ??????? //將菜單項(xiàng)saveAsItem加入到菜單組fileMenu中 ??????? fileMenu.add (saveAsItem); ??????? //在菜單項(xiàng)中添加隔離 ??????? fileMenu.addSeparator (); ???
11、???? fileMenu.add (exitItem); ??????? ??????? backItem.setMnemonic ('B'); ??????? backItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_Z,InputEvent.CTRL_MASK)); ??????? forwardItem.setMnemonic('D'); ??????? forwardItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_P,InputEvent
12、.CTRL_MASK)); ??????? ??????? editMenu.setMnemonic('E'); ??????? editMenu.add (backItem); ??????? editMenu.add (forwardItem);? ??????? ??????? viewMenu.setMnemonic('V'); ??????? ??????? fullscreenItem.setMnemonic('U'); ??????? fullscreenItem.setAccelerator (KeyStroke.getKeyStroke (Ke
13、yEvent.VK_U,InputEvent.CTRL_MASK)); ??????? sourceItem.setMnemonic('C'); ??????? sourceItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_C,InputEvent.CTRL_MASK)); ??????? reloadItem.setMnemonic('R'); ??????? reloadItem.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_R,InputEvent.CTR
14、L_MASK)); ??????? ??????? Container contentPane=getContentPane (); ??????????? ??????? //設(shè)置大小 ??????? scrollPane.setPreferredSize(new Dimension(100,500)); ??????? contentPane.add (scrollPane, BorderLayout.SOUTH); ??????? ??????? //在工具欄中添加按鈕組件 ??????? toolBar.add(picSave); ??????? tool
15、Bar.addSeparator(); ??????? toolBar.add(picBack); ??????? toolBar.add(picForward); ??????? toolBar.addSeparator(); ??????? toolBar.add(picView); ??????? toolBar.addSeparator(); ??????? toolBar.add(picExit); ??????? ??????? contentPane.add (bar,BorderLayout.CENTER); ??????? contentPane.add
16、(toolBar,BorderLayout.NORTH); ??????? ??????? viewMenu.add (fullscreenItem); ??????? viewMenu.add (sourceItem); ??????? viewMenu.addSeparator (); ??????? viewMenu.add (reloadItem); ??????? ??????? jMenuBar1.add (fileMenu); ??????? jMenuBar1.add (editMenu); ??????? jMenuBar1.add (vie
17、wMenu); ??????? setJMenuBar (jMenuBar1); ??? ??????? adress.add (label); ??????? adress.add (jurl); ??????? adress.add (button); ??????? bar.add (adress); ??????? ??????? //為組件添加事件監(jiān)X聽 ??????? saveAsItem.addActionListener(this); ??????? picSave.addActionListener(this); ??????? ex
18、itItem.addActionListener(this); ??????? picExit.addActionListener(this); ??????? backItem.addActionListener(this); ??????? picBack.addActionListener(this); ??????? forwardItem.addActionListener(this); ??????? picForward.addActionListener(this); ??????? fullscreenItem.addActionListener(this);
19、??????? sourceItem.addActionListener(this); ??????? picView.addActionListener(this); ??????? reloadItem.addActionListener(this); ??????? button.addActionListener(this); ??????? jurl.addActionListener(this); ??? } ??? ??? /** ??? **實(shí)現(xiàn)監(jiān)X聽*X器接口的actionPerformed函數(shù) ??? */ ??? public void action
20、Performed(ActionEvent e) { ??????? String url = ""; ??????? //點(diǎn)擊轉(zhuǎn)向按鈕 ??????? if (e.getSource() == button){ ??????????? //獲得地址欄的內(nèi)容 ??????????? url=jurl.getText (); ??????????? //url不為“”,并且以“http://”開頭 ??????????? if(url.length ()>0&&url.startsWith ("http://")){ ??????????????? try { ?????
21、?????????????? //JEditorPane組件顯示url的內(nèi)容鏈接 ??????????????????? jEditorPane1.setPage (url); ??????????????????? //將url的內(nèi)容添加到ArrayList對象history中 ??????????????????? history.add(url); ??????????????????? //historyIndex的數(shù)值設(shè)為history對象的長度-1 ??????????????????? historyIndex=history.size()-1; ????????
22、??????????? //重新布局 ????? jEditorPane1.setEditable(false); //add by copy editor :) ??????????????????? jEditorPane1.revalidate (); ??????????????? } ??????????????? catch(Exception ex) { ??????????????????? //如果鏈接顯示失敗,則彈出選擇對話框“無法打開該搜索頁” ??????????????????? JOptionPane.showMessageDialog (WebB
23、rowser.this,"無法打開該搜索頁","網(wǎng)頁瀏覽器",JOptionPane.ERROR_MESSAGE); ??????????????? } ??????????? } ??????????? //url不為“”,并且不以“http://”開頭 ??????????? else if(url.length ()>0&&!url.startsWith ("http://")) { ??????????????? //在url前面添加“http://” ??????????????? url="http://"+url; ??????????????? try {
24、 ??????????????????? jEditorPane1.setPage (url ); ??????????????????? history.add(url); ??????????????????? historyIndex=history.size()-1; ????? jEditorPane1.setEditable(false); //add by copy editor :) ??????????????????? jEditorPane1.revalidate (); ??????????????? } ??????????????? catc
25、h(Exception ex) { ??????????????????? JOptionPane.showMessageDialog (WebBrowser.this,"無法打開該搜索頁","網(wǎng)頁瀏覽器",JOptionPane.ERROR_MESSAGE); ??????????????? } ??????????? } ??????????? //沒有輸入url,即url為空 ??????????? else if(url.length ()==0){ ??????????????? JOptionPane.showMessageDialog (WebBrowser.thi
26、s,"請輸入鏈接地址","網(wǎng)頁瀏覽器",JOptionPane.ERROR_MESSAGE); ??????????? } ??????? ??????? } ??????? //輸入地址后點(diǎn)擊回車 ??????? else if (e.getSource() == jurl){ ??????????? url=jurl.getText (); ??????????? if(url.length ()>0&&url.startsWith ("http://")) { ??????????????? try { ??????????????????? jEditorPa
27、ne1.setPage (url); ??????????????????? history.add(url); ??????????????????? historyIndex=history.size()-1; ????? jEditorPane1.setEditable(false); //add by copy editor :) ??????????????????? jEditorPane1.revalidate (); ??????????????????? jurl.setMaximumSize (jurl.getPreferredSize ()); ???
28、???????????? } ??????????????? catch(Exception ex) { ??????????????????? JOptionPane.showMessageDialog (WebBrowser.this,"無法打開該搜索頁","網(wǎng)頁瀏覽器",JOptionPane.ERROR_MESSAGE); ??????????????? } ??????????? } ??????????? else if(url.length ()>0&&!url.startsWith ("http://")) { ??????????????? url="h
29、ttp://"+url; ??????????????? try { ??????????????????? jEditorPane1.setPage (url ); ??????????????????? history.add(url); ??????????????????? historyIndex=history.size()-1; ????? jEditorPane1.setEditable(false); //add by copy editor :) ??????????????????? jEditorPane1.revalidate (); ?????
30、?????????? } ??????????????? catch(Exception ex) { ??????????????????? JOptionPane.showMessageDialog (WebBrowser.this,"無法打開該搜索頁","網(wǎng)頁瀏覽器",JOptionPane.ERROR_MESSAGE); ??????????????? } ??????????? } ??????????? else if(url.length ()==0){ ??????????????? JOptionPane.showMessageDialog (WebBrows
31、er.this,"請輸入鏈接地址","網(wǎng)頁瀏覽器",JOptionPane.ERROR_MESSAGE); ??????????? } ??????? } ??????? //另存為... ??????? else if (e.getSource() == picSave||e.getSource() == saveAsItem){ ??????????? url = jurl.getText ().toString ().trim(); ??????????? if(url.length ()>0&&!url.startsWith ("http://")) { ??????
32、????????? url=""+url; ??????????? } ??????????? if(!url.equals ("")) { ??????????????? //保存文件 ??????????????? saveFile(url); ??????????? } ??????????? else { ??????????????? JOptionPane.showMessageDialog (WebBrowser.this,"請輸入鏈接地址","網(wǎng)頁瀏覽器",JOptionPane.ERROR_MESSAGE); ??????????? } ??????? } ??????? //退出 ??????? else if (e.getSource() == exitItem ||e.getSource() == picExit){ ??????????? System.exit(0); ??????? }140 [1]?[2]?下一頁 無恥悲鄙下流的網(wǎng)學(xué)網(wǎng)總是抄六維論文網(wǎng)的論文,現(xiàn)六維論文網(wǎng)啟用新的域名http://www./
- 溫馨提示:
1: 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
3.本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
5. 裝配圖網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 外匯的概念
- 六級上《能量與太陽》-教科版課件
- 六年級語文下冊第五單元16表里的生物作業(yè)課件新人教版
- 尺有所短寸有所長課件
- 手衛(wèi)生知識培訓(xùn)課件
- 攝像頭成像質(zhì)量評價及方案課件
- 六年級科學(xué)上冊-晝夜交替-(1)ppt課件-鄂教版
- 人體解剖學(xué)與組織胚胎學(xué)
- 任務(wù)型外語教學(xué)原則在教材編寫中的體現(xiàn)培訓(xùn)ppt課件
- 人教部編版歷史八年級下冊第8課經(jīng)濟(jì)體制改革課件
- 人教版八年級道德與法治第一單元1.2在社會中成長ppt課件
- 關(guān)鍵績效指標(biāo)KPI和平衡計分卡資料課件
- 頑固性氣胸的治療課件
- 化合價與化學(xué)式課件
- 議論文寫作思維訓(xùn)練之——辯證思維課件