rnbjacky 該用戶已被刪除 | 如果瀏覽伊莉時速度太慢或無法連接,可以使用其他分流瀏覽伊莉,www01.eyny.com(02,03)。 本帖最後由 19821206 於 2009-4-12 02:10 AM 編輯
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.util.*;
- import java.io.*;
- import java.net.*;
- import java.util.regex.*;
- public class yahooDictionary extends JFrame implements ActionListener {
- //宣告視窗介面的按鈕和輸出框框等
- private JLabel vocabularyLabel;
- private JTextField vocabularyField;
- private JButton button;
- private JTextArea meaningArea;
- private JLabel profileLabel;
- public static void main(String[] args) {
-
- //GUI的視窗設定
- yahooDictionary demo = new yahooDictionary();
- demo.setSize(800,900);
- demo.createGUI();
- demo.setVisible(true);
- }
- private void createGUI() {
- //產生WINDOWS視窗
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- Container window = getContentPane();
- window.setLayout(new FlowLayout());
-
- //在視窗上顯示要求的輸入
- vocabularyLabel = new JLabel("Enter the vocabulary you wanna look up:");
- window.add(vocabularyLabel);
- //產生輸入框
- vocabularyField = new JTextField(10);
- window.add(vocabularyField);
- //產生搜尋鈕
- button = new JButton("SEARCH");
- window.add(button);
- button.addActionListener(this);
- //產生輸出框
- meaningArea = new JTextArea(43,60);
- window.add(meaningArea);
- JScrollPane scrollPane = new JScrollPane(meaningArea);
- window.add(scrollPane);
-
- //在右下角寫出作者資料
- profileLabel = new JLabel("romana分享");
- window.add(profileLabel);
- }
- public void actionPerformed(ActionEvent event) {
- try{
- String word;//宣告想要查的單字
- word = vocabularyField.getText();//取得要查的單字
- URL url = new URL("http://tw.dictionary.yahoo.com/search?&p=" + word);//建立欲連結的網路位址
- URLConnection conn = url.openConnection();//建立一個網路連線到已設定好的網路位址 把http request 傳給server端
- BufferedReader inFromServer = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));//讀取SERVER端傳回來的資料 編碼為utf-8
- String dataFromServer="";//收到的資料由字串方式接收
- meaningArea.setText("");//先將輸出框設定為空白
-
- boolean out = false;
- while ((dataFromServer = inFromServer.readLine()) != null){
- if (dataFromServer.contains("blockquote")){
- out = !out;
- }
- if (out){
- String result = dataFromServer.replaceAll("(?s)<.*?>","").trim();
- if (result.length() > 0){
- meaningArea.append(result + '\n');
- }
- }
- }
- }
- catch (Exception e){
- }
- }
- }
複製代碼
有興趣的人可以參考看看@@... |
|