[問題] 用java做一個GUI可以使用奇摩字典

看板java作者 (浪漫A大調)時間17年前 (2008/05/02 23:37), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串1/3 (看更多)
之前要寫一個程式是要用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

05/03 01:33, , 1F
建議用pattern跟macher
05/03 01:33, 1F
文章代碼(AID): #186pK-OG (java)
文章代碼(AID): #186pK-OG (java)