응용 SoftWare/JAVA

[예제] Source View

Hyun CHO 2016. 12. 23. 17:36

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.BufferedReader;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.URL;

import java.net.URLConnection;


import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.JTextField;


public class SourceView extends JFrame implements ActionListener{

JPanel p = new JPanel();

JLabel urlLbl = new JLabel("URL : ");

JTextField tf = new JTextField(20);

JButton okBtn = new JButton("확인");

JScrollPane sp;

JTextArea ta = new JTextArea();

public SourceView() {

p.add(urlLbl); p.add(tf); p.add(okBtn);

add(p, "North");

sp = new JScrollPane(ta);

add(sp, "Center");

setSize(500, 500);

setVisible(true);

tf.addActionListener(this);

okBtn.addActionListener(this);

}

public void actionPerformed(ActionEvent ae){

Object obj = ae.getSource();

if(obj==tf || obj==okBtn){

if(tf.getText() != null){

try{

URL url = new URL(tf.getText());

URLConnection conn = url.openConnection();

conn.connect();

InputStream is = conn.getInputStream();

//////

String contentType = conn.getContentType();

int idx = contentType.indexOf("charset=");

String unicode = contentType.substring(idx+8);

InputStreamReader isr = new InputStreamReader(is, unicode);

BufferedReader br = new BufferedReader(isr);

ta.setText("");

while(true){

String str = br.readLine();

if(str==null) break;

ta.append(str+"\n");

}

}catch(Exception e){

System.out.println(e.getMessage());

}

}

}

}

public static void main(String[] args) {

new SourceView();


}


}