?? chap12-10.txt
字號:
// 程序12-10
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
public class browseWeb extends subJFrame {
private JTextField enterField;
private JEditorPane contentsArea;
public browseWeb( ) { // 構造函數
super( "Web browser" );
enterField = new JTextField( "請在此輸入URL" ); // 生成一個文本框對象
enterField.addActionListener( // 設置監聽者
new ActionListener( ) {
public void actionPerformed( ActionEvent event ){
getThePage( event.getActionCommand( ) ); // 讀取網頁
}
}
);
Container container = getContentPane( );
container.add( enterField, BorderLayout.NORTH );
contentsArea = new JEditorPane( ); // 創建帶滾動條的文本域
contentsArea.setEditable( false );
contentsArea.addHyperlinkListener( // 設置超級鏈接監聽
new HyperlinkListener( ) {
// 若用戶單擊了超級鏈接,則轉到指定頁面
public void hyperlinkUpdate( HyperlinkEvent event ){
if ( event.getEventType( ) == HyperlinkEvent.EventType.ACTIVATED )
getThePage( event.getURL( ).toString( ) );
}
}
);
container.add( new JScrollPane( contentsArea ), BorderLayout.CENTER );
setSize( 500, 250 );
show( );
}
private void getThePage( String location ){
// 當讀取文件時,設置鼠標的光標為運行態
setCursor( Cursor.getPredefinedCursor( Cursor.WAIT_CURSOR ) );
try { // 讀取文件,并在文本區顯示
contentsArea.setPage( location );
enterField.setText( location );
}catch ( IOException ioException ) { // 顯示一個出錯對話窗
JOptionPane.showMessageDialog( this,"URL 錯誤!", "URL出錯",JOptionPane.ERROR_MESSAGE );
}
// 運行結束,設置鼠標光標為正常態
setCursor( Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR ) );
}
public static void main( String args[ ] ) {
browseWeb application = new browseWeb( );
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -