이번에는 GUI로 만들어 보았다.
특별히 달라진 기능은 존재하지 않는다.
실행파일로 제작해 보는 연습삼아 몇타 두드려 보았다.
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class cyworld extends JFrame{
JTabbedPane pane= new JTabbedPane();
public cyworld(){
this.setSize(getPreferredSize());
this.setResizable(false);
JPanel panel;
this.setLayout(new BorderLayout());
panel= new PanelIndex(this);
pane.addTab("Index", panel);
panel= new PanelUsage();
pane.addTab("Usage", panel);
panel= new PanelAbout();
pane.addTab("About", panel);
pane.setSelectedIndex(0);
this.getContentPane().add(pane,"Center");
this.pack();
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
}
public Dimension getPreferredSize(){
return new Dimension(200, 200);
}
public static void main(String args[]){
cyworld cyworld= new cyworld();
cyworld.pack();
cyworld.setVisible(true);
}
};
//가장 중심이되는 패널이다. 실제 업카운팅 구문이 이 패널안에 있다.
class PanelIndex extends JPanel implements ActionListener, Runnable{
JLabel label1= new JLabel("Input Yout tid plz: ");
JLabel label2= new JLabel("Print Message");
JTextField inTid= new JTextField();
JButton btnStart= new JButton("Start");
JButton btnStop= new JButton("Stop");
TextArea taPrint= new TextArea(5, 10);
GridBagLayout gbl= new GridBagLayout();
GridBagConstraints gbc= new GridBagConstraints();
childWindow chWindow;
Thread cThread;
public PanelIndex(JFrame f){
chWindow= new childWindow(f);
gbc.weightx=1.0;
gbc.weighty=1.0;
gbc.fill= GridBagConstraints.BOTH;
btnStart.addActionListener(this);
btnStop.addActionListener(this);
this.setLayout(gbl);
this.add(0, 0, 1, 1, label1);
this.add(0, 1, 1, 1, inTid);
this.add(1, 1, 1, 1, btnStart);
this.add(1, 1, 1, 1, btnStop);
this.add(0, 2, 2 ,1, label2);
this.add(0, 3, 2, 1, taPrint);
cThread= new Thread(this);
}
public void add(int x, int y, int w, int h, Component c){
gbc.gridx= x;
gbc.gridy= y;
gbc.gridwidth= w;
gbc.gridheight= h;
add(c, gbc);
}
public void actionPerformed(ActionEvent ae){ //버튼에 이벤트 발생시 호출됨.
JButton jb= (JButton)ae.getSource();
if(inTid.getText().length()==8){
try{
if(jb.equals(btnStart)){//실행 버튼을 눌렀을때
if(cThread==null){
cThread= new Thread(this);
}
cThread.start();
taPrint.append("카운터 시작됨.\n");
btnStop.setVisible(true);
btnStart.setVisible(false);
}else{//실행 버튼을 눌렀을때가 아는 즉 stop버튼 눌렀을때.
cThread.stop();
cThread=null;
taPrint.append("카운터 정지됨.\n");
btnStart.setVisible(true);
btnStop.setVisible(false);
}
}catch(Exception e){
chWindow.show("치명적 에러 발생.");
}
}else{
System.out.println(inTid.getText());
chWindow.show("tid 값이 올바르지 않음");
}
}
String urlStr= "http://minihp.cyworld.nate.com/pims/main/pims_main4.asp?tid=";
String myUrl= "http://minihp.cyworld.nate.com/pims/main/pims_main4.asp?tid=24631766";
int count=0;
public void run(){
try{
while(true){
URL url= new URL(urlStr);
URLConnection con= url.openConnection();
InputStream is= con.getInputStream();
if(con.toString().endsWith("limit=0") || con.toString().endsWith("limit=1")){
taPrint.append("매크로 에러에 걸림!\n");
break;
}
count++;
taPrint.append("Count: "+Integer.toString(count)+"\n");
Thread.sleep(7000);
//제작자 방문.
URL url2= new URL(myUrl);
URLConnection con2= url2.openConnection();
InputStream is2= con2.getInputStream();
Thread.sleep(7000);
}
}catch(Exception e){}
}
};
class PanelUsage extends JPanel{
public PanelUsage(){
JButton btnUsage= new JButton("사용법 보기");
btnUsage.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
try{
Runtime rt= Runtime.getRuntime(); //사용법을 보기위한 버튼을 눌렀을때 발생하며, 로컬의 인터넷익스플로러를 실행시킨다. 문제점은 아래의 경로상에 iexplore가 없다면 익셉션을 일으킨다. ㅡ,.ㅡ;;
try{
Process p1= rt.exec("C:\\Program Files\\Internet Explorer\\IEXPLORE.exe http://dogbull.cafe24.com/count/usage.html");
}catch(Exception e){
System.out.println("Cannot Load Internet Explorer");
}
}catch(Exception e){}
}
});
this.add(btnUsage);
}
};
class PanelAbout extends JPanel{
public PanelAbout(){
JTextArea txt= new JTextArea("", 10, 10);
txt.append("Cyworld Up Counter.\n\n");
txt.append("CopyLeft by DogBull(ParkJuHyun)\n\n");
txt.append("There is a legal responsibility\n");
txt.append("all to the user, which it\n");
txt.append("happens as use this program.\n");
txt.setEditable(false);
this.add(txt);
}
};
//주로 에러를 출력하기 위한 팝업 다이얼로그 창이다.
class childWindow extends JDialog implements ActionListener{
JLabel msg= new JLabel("", JLabel.CENTER);
JButton btnConfirm= new JButton("Confirm");
public childWindow(JFrame f){
super(f, "Error", true);
btnConfirm.addActionListener(this);
this.add("North", msg);
this.add("South", btnConfirm);
}
public void show(String str){
this.setResizable(false);
msg.setText(str);
this.setSize(150, 80);
super.show();
this.pack();
}
public void actionPerformed(ActionEvent ae){
dispose();
}
};
첨부된 파일은 컴파일 및 실행파일까지 제작 완료된 것이다.
시작은 미미하나, 디버깅은 창대하다.
'Computer > Java' 카테고리의 다른 글
[Data Structure2] Binary Tree (Linked List) (0) | 2006.10.09 |
---|---|
[Data Structure2] Max Heap (for int array) (0) | 2006.10.09 |
[tip] 화면 크기 구하기. (0) | 2006.08.04 |
Omok Server For Flash Client Ver1.0 (0) | 2006.07.23 |
Cyworld Today Counter Hack Ver1.0(Consol Version) by DogBull. (0) | 2006.07.23 |