import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class webpad extends JFrame implements KeyListener { private static webpad frm = null; private static JTextField fld = null; private static JTextArea area = null; private static JScrollPane scr = null; // メインルーチン public static void main(String[] args) { frm = new webpad(); frm.setVisible(true); } public webpad() { super(); // ウインドウの部品設定 getContentPane().setLayout(new BorderLayout()); fld = new JTextField(); fld.setName("text001"); fld.addKeyListener(this); getContentPane().add("North", fld); area = new JTextArea(); area.setEditable(false); scr = new JScrollPane(area); getContentPane().add("Center", scr ); // ウインドウの設定 setTitle("webpad"); setSize(400,300); setBackground(Color.gray); } // WINDOWが閉じた場合の処理 protected void processWindowEvent(WindowEvent e) { if(e.getID()==e.WINDOW_CLOSING) { System.exit(0); } } // オブジェクトfldのキーイベント public void keyReleased(KeyEvent e) { if(e.getComponent().getName().equals(fld.getName())) { if(e.getKeyCode()==e.VK_ENTER) { connectUrl(fld.getText()); } } } public void keyPressed(KeyEvent ev ){} public void keyTyped(KeyEvent ev ){} // サブ関数 public static void connectUrl(String urlname) { try { URL url = new URL(urlname); URLConnection conn = url.openConnection(); // conn.setDoOutput(true); // 必要ないか conn.setDoInput(true); conn.setUseCaches(false); // 出力 // 単なるHttpの場合、ここはない。 // 今回は使用しない。 // OutputStream out = conn.getOutputStream(); // out.close(); // 入力 BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String str; String buf = ""; while(true) { if((str = in.readLine())==null) break; buf = buf + str + "\n"; } area.setText(buf); in.close(); } catch(Exception e) { String err = "error:" + e.toString(); System.out.println(err); area.setText(err); }; return; } }