java網(wǎng)頁(yè)瀏覽器設(shè)計(jì)課程設(shè)計(jì)
《java網(wǎng)頁(yè)瀏覽器設(shè)計(jì)課程設(shè)計(jì)》由會(huì)員分享,可在線閱讀,更多相關(guān)《java網(wǎng)頁(yè)瀏覽器設(shè)計(jì)課程設(shè)計(jì)(8頁(yè)珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。
1、 java網(wǎng)頁(yè)瀏覽器設(shè)計(jì)-課程設(shè)計(jì) java網(wǎng)頁(yè)瀏覽器設(shè)計(jì) /* **網(wǎng)頁(yè)瀏覽器主程序 **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{ ??? //建立工具欄用來(lái)顯示地址欄 ??? JToolBar bar=new JToolBar (); ??? ??? //建立網(wǎng)頁(yè)顯示界面 ??? 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對(duì)象,用來(lái)存放歷史地址 ??? private ArrayList history=new ArrayList(); ??? //整型變量,
8、表示歷史地址的訪問(wèn)順序 ??? private int historyIndex; ??? /** ??? **構(gòu)造函數(shù) ??? **初始化圖形用戶界面 ??? */ ??? public WebBrowser(){ ??????? ??????? setTitle ("網(wǎng)頁(yè)瀏覽器"); ??????? setResizable(false); ??????? setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ??????????????????? ??????? //為jEditorPane1添加事件偵聽(tīng) ?
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聽(tīng) ??????? 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聽(tīng)*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://”開(kāi)頭 ??????????? if(url.length ()>0&&url.startsWith ("http://")){ ??????????????? try { ?????
21、?????????????? //JEditorPane組件顯示url的內(nèi)容鏈接 ??????????????????? jEditorPane1.setPage (url); ??????????????????? //將url的內(nèi)容添加到ArrayList對(duì)象history中 ??????????????????? history.add(url); ??????????????????? //historyIndex的數(shù)值設(shè)為history對(duì)象的長(zhǎng)度-1 ??????????????????? historyIndex=history.size()-1; ????????
22、??????????? //重新布局 ????? jEditorPane1.setEditable(false); //add by copy editor :) ??????????????????? jEditorPane1.revalidate (); ??????????????? } ??????????????? catch(Exception ex) { ??????????????????? //如果鏈接顯示失敗,則彈出選擇對(duì)話框“無(wú)法打開(kāi)該搜索頁(yè)” ??????????????????? JOptionPane.showMessageDialog (WebB
23、rowser.this,"無(wú)法打開(kāi)該搜索頁(yè)","網(wǎng)頁(yè)瀏覽器",JOptionPane.ERROR_MESSAGE); ??????????????? } ??????????? } ??????????? //url不為“”,并且不以“http://”開(kāi)頭 ??????????? 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ú)法打開(kāi)該搜索頁(yè)","網(wǎng)頁(yè)瀏覽器",JOptionPane.ERROR_MESSAGE); ??????????????? } ??????????? } ??????????? //沒(méi)有輸入url,即url為空 ??????????? else if(url.length ()==0){ ??????????????? JOptionPane.showMessageDialog (WebBrowser.thi
26、s,"請(qǐng)輸入鏈接地址","網(wǎng)頁(yè)瀏覽器",JOptionPane.ERROR_MESSAGE); ??????????? } ??????? ??????? } ??????? //輸入地址后點(diǎn)擊回車(chē) ??????? 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ú)法打開(kāi)該搜索頁(yè)","網(wǎng)頁(yè)瀏覽器",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ú)法打開(kāi)該搜索頁(yè)","網(wǎng)頁(yè)瀏覽器",JOptionPane.ERROR_MESSAGE); ??????????????? } ??????????? } ??????????? else if(url.length ()==0){ ??????????????? JOptionPane.showMessageDialog (WebBrows
31、er.this,"請(qǐng)輸入鏈接地址","網(wǎng)頁(yè)瀏覽器",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,"請(qǐng)輸入鏈接地址","網(wǎng)頁(yè)瀏覽器",JOptionPane.ERROR_MESSAGE); ??????????? } ??????? } ??????? //退出 ??????? else if (e.getSource() == exitItem ||e.getSource() == picExit){ ??????????? System.exit(0); ??????? }140 [1]?[2]?下一頁(yè) 無(wú)恥悲鄙下流的網(wǎng)學(xué)網(wǎng)總是抄六維論文網(wǎng)的論文,現(xiàn)六維論文網(wǎng)啟用新的域名http://www./
- 溫馨提示:
1: 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
3.本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
5. 裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 隱蔽工程驗(yàn)收要點(diǎn)
- 給排水中水泵揚(yáng)程與壓力的關(guān)系
- 水泥廠燒成回轉(zhuǎn)窯中控操作問(wèn)題解答
- 地暖安裝注意事項(xiàng)
- 水泥廠中控操作自動(dòng)化專(zhuān)業(yè)試題(附答案)
- 防水套管的應(yīng)用與分類(lèi)
- 施工現(xiàn)場(chǎng)職業(yè)健康管理制度
- 常見(jiàn)基坑工程的支護(hù)方式
- 水泥包裝車(chē)間各崗位職責(zé)
- 打樁機(jī)的種類(lèi)
- 水泥磨操作員試題(附答案)
- 鋼結(jié)構(gòu)工程量計(jì)算注意事項(xiàng)
- 水泥控制工試題(附答案)
- 水泥生產(chǎn)工藝類(lèi)知識(shí)考試題(附答案)-
- 鋼結(jié)構(gòu)安裝施工安全技術(shù)交底范文