구성
- View 객체 : 화면을 구성하고 컴포넌트에 이벤트를 연결하는 객체
- Model 객체 : 해당 이벤트에 로직을 수행하여 결과를 View 객체에게 전달하는 객체
View 객체 : ShowURLinfo
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
public class ShowURLinfo extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private int x = 50;
private int y = 50;
private int width = 900;
private int height = 600;
private JTextField inputField;
private JTextArea displayArea;
private JPanel pInput;
private JPanel pButton;
private JButton showDocument, showInfo, reset;
private SearchUrlInfo urlInfo;
public ShowURLinfo() {
super("Display URL Infomation");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(x, y, width, height);
Container c = getContentPane();
c.setLayout(new BorderLayout());
setVisible(true);
showDocument = new JButton("Show Document");
showDocument.addActionListener(this);
showInfo = new JButton("Document Info");
showInfo.addActionListener(this);
reset = new JButton("Reset");
reset.addActionListener(this);
pButton = new JPanel();
pButton.setLayout(new FlowLayout(FlowLayout.CENTER));
pButton.add(showDocument);
pButton.add(showInfo);
pButton.add(reset);
inputField = new JTextField("http://");
inputField.setBorder(new TitledBorder("Enter URL here"));
inputField.addActionListener(this);
pInput = new JPanel();
pInput.setLayout(new GridLayout(1,2));
pInput.add(inputField);
pInput.add(pButton);
displayArea = new JTextArea();
c.add(pInput, BorderLayout.NORTH);
c.add(new JScrollPane(displayArea), BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent evt) {
String command = evt.getActionCommand();
String urlAddress = inputField.getText();
displayArea.setText("");
inputField.setText("http://");
try {
urlInfo = new SearchUrlInfo(displayArea, urlAddress);
} catch(MalformedURLException e) {
displayArea.append("This url is not correct!");
return ;
}
if ( command.equals("Show Document")) {
urlInfo.readURLData();
}else if ( command.equals("Document Info") ){
urlInfo.useURLConnection();
}else if ( command.equals("Reset") ){
displayArea.setText("");
inputField.setText("http://");
}
}
public static void main(String[] args) {
new ShowURLinfo();
}
}
Model 객체 : SearchUrlInfo
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import javax.swing.JTextArea;
public class SearchUrlInfo {
private JTextArea area;
private URL url;
private URLConnection connector;
public SearchUrlInfo(JTextArea area, String url) throws MalformedURLException {
this.area = area;
this.url = new URL(url);
}
public void readURLData() {
InputStream istream = null;
BufferedReader reader = null;
try{
istream = url.openStream();
reader = new BufferedReader( new InputStreamReader(istream) );
area.append("----------------- URL Data -----------------\n");
String input;
while( ( input = reader.readLine() ) != null ){
area.append(" " + input + "\n");
}
}catch(Exception e){
area.append("Stream didn't open!\n");
}finally {
if ( istream != null ) try { istream.close(); } catch(IOException e) {}
}
}
public void useURLConnection(){
try {
connector = url.openConnection();
} catch (IOException e) {
area.append("This url didn't connect!");
}
try {
area.append("----------------- URL Document Data -----------------\n");
area.append("Document Type : "+ connector.getContentType() + "\n");
area.append("Document length : "+ connector.getContentLength() + "\n");
area.append("Last Modified : "+ new Date(connector.getLastModified()) + "\n");
area.append("Server's clock : "+ new Date(connector.getDate())+ "\n");
} catch (IllegalArgumentException e) {
area.append("This url is not have host address!");
}
}
}
[위 코드의 실행 파일]
'자바(Java) > JAVA 2SE' 카테고리의 다른 글
자바 어플리케이션의 구성 (0) | 2010.11.28 |
---|---|
TableModel 를 사용하여 구구단 출력하는 프로그램 (0) | 2010.09.03 |
사용자 예외 클래스 만들기 (0) | 2010.08.29 |
throws 문 사용법 (0) | 2010.08.29 |
이벤트 클래스 계층도 (0) | 2010.08.29 |