티스토리 뷰

일상 이야기

자바 프로그램을 짜다가..

섬뜩파워 2013. 5. 25. 20:58

얼마전에 이런일이 있었다.

여자친구네 회사에서 신상품을 입고해야 하니 창고에 와서 재고들 좀 알박기 해달라고;;

마침 돈도 없었고, 간만에 힘쓰는 일이 땡겨서 해주기로 했다.

여자친구는 그때 11번가나 G마켓 등등에 회사 물건을 업로드하는 웹디일을 하고 있었는데

G마켓 특유의 짱돌 시스템 때문인지 단순한 HTML업무도 매우 귀찮게 되어 있었다.

그래서 고생하는 여자친구를 위해 A태그를 간단히 작성하는 프로그램을 짜보기로 했다^^


실행한 화면은 다음과 같다.




코드는 다음과 같다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import javax.swing.*;
import java.util.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.io.*;
class TagGenerator implements ActionListener
{
    //public TagGenerator(){} 만약을 위해 만든 생성자
    JFrame frame;  
    JTextField urlField;
    JTextField ImagePathField;
    JTextArea area;
    JButton button;
 
    /*****************************************************************************************************
    이벤트 처리 : 두개의 텍스트필드에서 값을 입력받아 HTML태그로 만든뒤 텍스트 에리어에 뿌려줌.
    ******************************************************************************************************/
    public void actionPerformed(ActionEvent e){
        String url = urlField.getText();
        String localPath = ImagePathField.getText();
        String result = "<a href="\""" +="" url="" "\"=""><img src="\"" "="" +="" localpath="" \"=""></a>\n";
        String toSend = area.getText();
        //System.out.print(result);                    
        area.append(result);
        urlField.setText("");
        ImagePathField.setText("");
        urlField.requestFocus();           
        /*************************************************************************************************
        텍스트 에리어에 출력된 결과값을 텍스트 파일로 저장하고 싶음(근데 안됨ㅠㅠ)
        **************************************************************************************************/
        FileWriter toTxt = null;   
        try{
            toTxt = new FileWriter("saved.txt");
            toTxt.write(toSend);
        }
        catch(IOException ioe){}
    }
 
    /*public void actionPerformed2(ActionEvent e){
        FileWriter toTxt = null;   
        try{
            toTxt = new FileWriter("hahaha.txt");
            toTxt.write(sendStr);
        }
        catch(IOException ioe){}
    }
    */
 
    /***************************************************************
    기본적인 프레임 세팅
    ***************************************************************/
    public void view(){
        JFrame frame = new JFrame("A태그 생성기^^");    
        frame.add(layout());
        frame.setSize(1024, 768);
        frame.setVisible(true);        
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension f = frame.getSize();
        frame.setLocation((int) (screen.getWidth() - f.getWidth()) / 2, (int) (screen.getHeight() - f.getHeight()) / 2);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Toolkit toolkit = Toolkit.getDefaultToolkit(); // 이미지 아이콘 바꾸기
        Image img = toolkit.getImage("icon.gif");
        frame.setIconImage(img);   
    }
     
    /***************************************************************
    컨테이너 레이아웃
    ***************************************************************/
    JPanel layout(){ //입력모드
        JPanel hapPannel = new JPanel(); //upPannel + downPannel + savePannel
        JPanel upPannel = new JPanel();  //upPannel1 + upPannel2
        JPanel upPannel1 = new JPanel(); //upPannel1_1 + upPannel1_2
        JPanel upPannel1_1 = new JPanel();
        JPanel upPannel1_2 = new JPanel();
        JPanel upPannel2 = new JPanel(); //upPannel2_1 + upPannel2_2
        JPanel upPannel2_1 = new JPanel();
        JPanel upPannel2_2 = new JPanel();
        JPanel downPannel = new JPanel();  
        JPanel savePannel = new JPanel(); //저장버튼을 달기 위한 패널
         
        hapPannel.setLayout(new BorderLayout());       
        upPannel.setLayout(new BorderLayout());
         
        EnterKeyEvent_On_urlField();
        EnterKeyEvent_On_localPathField(); //이벤트 리스너 붙인곳
         
        upPannel1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        JLabel urlLabel = new JLabel("Target URL : ");
        urlLabel.setPreferredSize(new Dimension(100,30));
        upPannel1_1.add(urlLabel);     
        upPannel1_2.add(urlField);
        upPannel1.add(upPannel1_1);
        upPannel1.add(upPannel1_2);
 
        upPannel2.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        JLabel Pathlabel = new JLabel("Image Path : ");
        upPannel2_1.add(Pathlabel);
        Pathlabel.setPreferredSize(new Dimension(100,30));
        upPannel2_2.add(ImagePathField);
        upPannel2.add(upPannel2_1);
        upPannel2.add(upPannel2_2);
         
        upPannel.add(upPannel1,"North");
        upPannel.add(upPannel2,"South");
 
        area = new JTextArea();    
        area.setBorder(new LineBorder(new Color(179,179,179)));
        area.setFont(new Font(null, Font.PLAIN, 15));
        downPannel.add(area);      
 
        JScrollPane scroll = new JScrollPane(area);
        int len=  area.getText().length();
        area.setCaretPosition(len); //자동 스크롤   
        downPannel.setBorder(new LineBorder(new Color(179,179,179)));  
         
        saveToTextFile(); /*************안됌...ㅠㅠ***********/
        savePannel.add(button);
 
        hapPannel.add(upPannel,"North");       
        hapPannel.add(downPannel,"Center");
        hapPannel.add(savePannel, "South");
        hapPannel.add(scroll); 
 
        return hapPannel;
    }
 
        void EnterKeyEvent_On_urlField(){                  
            urlField = new JTextField();
            urlField.setPreferredSize(new Dimension(850, 30));                 
            //urlField.addActionListener(this);
        }
 
        void EnterKeyEvent_On_localPathField(){
            ImagePathField = new JTextField();
            ImagePathField.setPreferredSize(new Dimension(850, 30));                   
            ImagePathField.addActionListener(this);
        }
 
        void saveToTextFile(){
            button = new JButton("텍스트 파일로 저장");
            button.setPreferredSize(new Dimension(190, 55));
            button.setFont(new Font(null, Font.PLAIN, 12));
            button.setBackground(new Color(255,255,238,238));
            button.addActionListener(this);        
        }
 
    public static void main(String[] args)
    {
        TagGenerator obj = new TagGenerator();
        obj.view();
    }
}




자바를 좀만 만져본 사람이라면 매우 허접한 코드라는걸 한눈에 알아보겠지만;
저 프로그램을 통해 하룻동안 작성한 HTML 코드들을 텍스트 파일로 저장하는 단순한 기능이 제대로 작동하지 않는것이었다;;
혹시라도 자바 프로그래밍을 잘 아시는 분이 계시다면 조언을 해주면 매우 고맙겠다ㅠㅠ
지금은 여자친구가 그 회사를 그만두게 되어서 저걸 완성할 필요는 없지만.. 그래도 사람이 뭔가 시작을 하면 끝을 보고 싶은 법 아니겠음?ㅋㅋ


Comments
최근에 달린 댓글
Total
Today
Yesterday