當前位置:編程學習大全網 - 編程語言 - 用JAVA設計簡單互聯網瀏覽器

用JAVA設計簡單互聯網瀏覽器

import org.eclipse.swt.SWT;import org.eclipse.swt.browser.Browser;import org.eclipse.swt.browser.LocationEvent;import org.eclipse.swt.browser.LocationListener;import org.eclipse.swt.browser.ProgressEvent;import org.eclipse.swt.browser.ProgressListener;import org.eclipse.swt.browser.StatusTextEvent;import org.eclipse.swt.browser.StatusTextListener;import org.eclipse.swt.browser.TitleEvent;import org.eclipse.swt.browser.TitleListener;import org.eclipse.swt.events.KeyAdapter;import org.eclipse.swt.events.KeyEvent;import org.eclipse.swt.events.MouseAdapter;import org.eclipse.swt.events.MouseEvent;import org.eclipse.swt.events.SelectionAdapter;import org.eclipse.swt.events.SelectionEvent;import org.eclipse.swt.graphics.Point;import org.eclipse.swt.layout.GridData;import org.eclipse.swt.layout.GridLayout;import org.eclipse.swt.widgets.Button;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Label;import org.eclipse.swt.widgets.ProgressBar;import org.eclipse.swt.widgets.Shell;import org.eclipse.swt.widgets.Text;

public class BrowserSample {

//定義瀏覽器的標題 public static final String APP_TITLE = "Simple SWT Browser"; //定義主頁的url public static final String HOME_URL = " /"; //聲明主窗口和其它控件 private Shell shell = null; private Button backButton = null;//後退按鈕 private Button forwardButton = null;//前進按鈕 private Button stopButton = null;//停止按鈕 private Text locationText = null;//顯示url的文本框 private Button goButton = null;//轉向按鈕 private Browser browser = null;//瀏覽器對象 private Button homeButton = null;//主頁按鈕 private Label statusText = null;//顯示瀏覽器狀態的文本框 private ProgressBar progressBar = null;//裝載頁面時的進度條 private Button refreshButton = null;//刷新按鈕

public static void main(String[] args) { Display display = Display.getDefault(); BrowserSample browserSample = new BrowserSample(); browserSample.createShell(); browserSample.shell.open(); while (!browserSample.shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }

/** * 創建窗口和窗口的控件,並註冊事件 */ private void createShell() { shell = new Shell(); GridLayout gridLayout1 = new GridLayout(); GridData gridData2 = new GridData(); GridData gridData4 = new GridData(); GridData gridData5 = new GridData(); GridData gridData6 = new GridData(); GridData gridData7 = new GridData(); GridData gridData8 = new GridData(); backButton = new Button(shell, SWT.ARROW | SWT.LEFT); forwardButton = new Button(shell, SWT.ARROW | SWT.RIGHT); stopButton = new Button(shell, SWT.NONE); refreshButton = new Button(shell, SWT.NONE); homeButton = new Button(shell, SWT.NONE); locationText = new Text(shell, SWT.BORDER); goButton = new Button(shell, SWT.NONE); createBrowser(); progressBar = new ProgressBar(shell, SWT.BORDER); statusText = new Label(shell, SWT.NONE); shell.setText(APP_TITLE); shell.setLayout(gridLayout1); gridLayout1.numColumns = 7; backButton.setEnabled(false); backButton.setToolTipText("後退"); backButton.setLayoutData(gridData6); forwardButton.setEnabled(false); forwardButton.setToolTipText("前進"); forwardButton.setLayoutData(gridData5); stopButton.setText("停止"); stopButton.setEnabled(false); stopButton.setToolTipText("停止載入當前頁面"); goButton.setText("轉到"); goButton.setLayoutData(gridData8); goButton.setToolTipText("前往目標頁面"); gridData2.grabExcessHorizontalSpace = true; gridData2.horizontalAlignment = GridData.FILL; gridData2.verticalAlignment = GridData.CENTER; locationText.setLayoutData(gridData2); locationText.setText(HOME_URL); locationText.setToolTipText("輸入地址"); homeButton.setText("主頁"); homeButton.setToolTipText("回到主頁"); statusText.setText("完成"); statusText.setLayoutData(gridData7); gridData4.horizontalSpan = 5; progressBar.setLayoutData(gridData4); progressBar.setEnabled(false); progressBar.setSelection(0); gridData5.horizontalAlignment = GridData.FILL; gridData5.verticalAlignment = GridData.FILL; gridData6.horizontalAlignment = GridData.FILL; gridData6.verticalAlignment = GridData.FILL; gridData7.horizontalSpan = 1; gridData7.grabExcessHorizontalSpace = true; gridData7.horizontalAlignment = GridData.FILL; gridData7.verticalAlignment = GridData.CENTER; gridData8.horizontalAlignment = GridData.END; gridData8.verticalAlignment = GridData.CENTER; refreshButton.setText("刷新"); refreshButton.setToolTipText("正在刷新..."); shell.setSize(new Point(553, 367)); //註冊顯示地址的文本框事件 locationText.addMouseListener(new MouseAdapter() { public void mouseUp(MouseEvent e) { locationText.selectAll(); } }); locationText.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { // Handle the press of the Enter key in the locationText. // This will browse to the entered text. if (e.character == SWT.LF || e.character == SWT.CR) { e.doit = false; browser.setUrl(locationText.getText()); } } }); refreshButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { browser.refresh();//重新載入 } }); locationText.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { browser.setUrl(locationText.getText());//設置瀏覽器的指向的url } }); stopButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { browser.stop();//停止裝載網頁 } }); backButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { browser.back();//後退 } }); forwardButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { browser.forward();//前進 } }); homeButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { browser.setUrl(HOME_URL);//設置主頁 } }); goButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { browser.setUrl(locationText.getText());//轉向地址的網頁 } }); } /** * 初始化瀏覽器 */ private void createBrowser() { GridData gridData3 = new GridData(); //創建瀏覽器對象 browser = new Browser(shell, SWT.BORDER); gridData3.horizontalSpan = 7; gridData3.horizontalAlignment = GridData.FILL; gridData3.verticalAlignment = GridData.FILL; gridData3.grabExcessVerticalSpace = true; //設置瀏覽器布局 browser.setLayoutData(gridData3); //為瀏覽器註冊標題改變事件 browser.addTitleListener(new TitleListener() { public void changed(TitleEvent e) { shell.setText(APP_TITLE + " - " + e.title); } }); //為瀏覽器註冊地址改變事件 browser.addLocationListener(new LocationListener() { public void changing(LocationEvent e) { locationText.setText(e.location); }

public void changed(LocationEvent e) { } }); //為瀏覽器註冊裝載網頁事件 browser.addProgressListener(new ProgressListener() { //當裝載時,設置裝載的進度,並且設置停止按鈕可用 public void changed(ProgressEvent e) { if (!stopButton.isEnabled() && e.total != e.current) { stopButton.setEnabled(true); } progressBar.setMaximum(e.total); progressBar.setSelection(e.current); } //裝載完成後設置停止按鈕,後退按鈕,前進按鈕和進度條的狀態 public void completed(ProgressEvent e) { stopButton.setEnabled(false); backButton.setEnabled(browser.isBackEnabled()); forwardButton.setEnabled(browser.isForwardEnabled()); progressBar.setSelection(0); } }); //註冊瀏覽器狀態改變事件 browser.addStatusTextListener(new StatusTextListener() { public void changed(StatusTextEvent e) { statusText.setText(e.text); } }); //初始狀態打開主頁的url browser.setUrl(HOME_URL); locationText.setText(HOME_URL); }}拿去用吧

  • 上一篇:我的世界命令方塊為什麽無法召喚實體?
  • 下一篇:廣告行業對學歷要求很高嗎_廣告公司上班需要學歷嗎
  • copyright 2024編程學習大全網