作者rnbjacky (浪漫A大调)
看板java
标题[问题] 用java做一个GUI可以使用奇摩字典
时间Fri May 2 23:37:33 2008
之前要写一个程式是要用java连上yahoo 字典 的server 然後回传想要搜寻的单字
但是遇到了几个问题
下面是我目前的原始码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.net.*;
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) {
yahooDictionary demo = new yahooDictionary();
demo.setSize(800,900);
demo.createGUI();
demo.setVisible(true);
}
private void createGUI() {
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);
}
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();
BufferedReader inFromServer = new BufferedReader(new
InputStreamReader(conn.getInputStream(),"utf-8"));
String dataFromServer="";
meaningArea.setText("");
while( (dataFromServer = inFromServer.readLine()) != null ){
String result = dataFromServer.replaceAll("(?s)<.*?>", "");
meaningArea.append(result + '\n');
}
}
catch (Exception e){
}
}
}
目前可以显示那页的原始码
但是只想留下有用的部分(翻译以後的部分)
後来我补上了String result = dataFromServer.replaceAll("(?s)<.*?>", "");
变成其他原始码的部分都被取代 都不会在textArea看到(部分还是会显示)
这行的replaceAll是从网路上找到的 其实我不懂为什麽"(?s)<.*?>"这样写
然後目标是只撷取原始码中<blockquote>和<\blockquote>中间的部分(就是翻译)
不知道如何可以让bufferReader读到的部分 在输出的时候只仅於中间??
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.129.162.209
1F:推 slalala:建议用pattern跟macher 05/03 01:33