theCEED.com - Research your Interests!!
  • MEMBERSHIP PROFILE
  • bar
  • No member information.
  • login_button
  • sign_up_button
no_image

Fun Coding Contest

About the ceed

재밌는 코딩 대회 페이지입니다 :)

대회 일정 및 소개[클릭하세요]
Recommend : 9 I Total List : 58 I 2017-01-09
favorite
27번 주제 (윈도우즈 프로그램 짜기) 2017.01.23 17:07:53
Nickname : 1. alphablade Recommend : 1 View : 346 btn_recommend.png declaration

Please enter reasons of report.

declaration

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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import javax.swing. *;
import java.awt.event. *;
import java.io. *;
import java.awt. *;
import javax.swing.filechooser. *;
 
public class Project extends JFrame {
    Container contentPane;
    JTextArea TX  = new JTextArea(null,15,25),TY  = new JTextArea(null,6,10);
    JTextField tf  = new JTextField(10);
    JButton b  = new JButton("편집 버튼");
    String s1  = TX.getText();
 
    Project() {
        setTitle("미니 프로젝트 만들기");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane  = getContentPane();
        contentPane.setLayout(new FlowLayout());
        createMenu();
        setSize(500,500);
        setVisible(true);
    }
    void createMenu(){
        Font F1  = new Font("돋움",Font.PLAIN,20);
 
        JMenuBar mb  = new JMenuBar();
        JMenu File  = new JMenu("File(F)");
        JMenu Edit  = new JMenu("Edit(E)");
 
        JMenuItem [] fi  = new JMenuItem[3];
        JMenuItem [] ei  = new JMenuItem[2];
 
        String[] FileItem  = {"Open","Save","Exit"};
        String[] EditItem  = {"Find","Change"};
        
        
        File.setFont(F1);
        Edit.setFont(F1);
        b.setFont(F1);
        b.addActionListener(new MenuActionListener());
        
        
 
        for(int i =0;i <fi.length;i + +){
            fi[i]  = new JMenuItem(FileItem[i]);
            if(i = =0)
                fi[i].setAccelerator(KeyStroke.getKeyStroke('O', Event.CTRL_MASK));
            else if(i = =1)
                fi[i].setAccelerator(KeyStroke.getKeyStroke('S', Event.CTRL_MASK));
            else
                fi[i].setAccelerator(KeyStroke.getKeyStroke('E', Event.CTRL_MASK));
            fi[i].addActionListener(new MenuActionListener());
            fi[i].setFont(F1);
            File.add(fi[i]);
        }
        for(int i =0;i <ei.length;i + +){
            ei[i]  = new JMenuItem(EditItem[i]);
            if(i = =0)
                ei[i].setAccelerator(KeyStroke.getKeyStroke('F', Event.CTRL_MASK));
            else
                ei[i].setAccelerator(KeyStroke.getKeyStroke('C', Event.CTRL_MASK));
            ei[i].addActionListener(new MenuActionListener());
            ei[i].setFont(F1);
            Edit.add(ei[i]);
        }
        mb.add(File);
        mb.add(Edit);
        setJMenuBar(mb);
        TX.setFont(F1);
        contentPane.add(new JScrollPane(TX));
        contentPane.add(b);
    }
    class MyDialog extends JDialog{
        JButton ok  = new JButton("OK");
        public MyDialog(JFrame frame,String title){
            super(frame,title);
            setLayout(new FlowLayout());
            add(tf);
            add(ok);
            add(TY);
 
            setSize(200,200);
            ok.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    setVisible(false);
                }
            });
        }
    }
    class MenuActionListener implements ActionListener{
        JFileChooser chooser;
        MenuActionListener() {
            chooser  = new JFileChooser();
        }
        public void actionPerformed(ActionEvent e){
 
            String cmd  = e.getActionCommand();
            FileNameExtensionFilter filter  = new FileNameExtensionFilter("*.txt 파일""txt");
            chooser.setFileFilter(filter);
            if(cmd.equals("Open")){
                int ret  = chooser.showOpenDialog(null);
                if(ret  ! = JFileChooser.APPROVE_OPTION){
                    JOptionPane.showMessageDialog(null"파일을 선택하지 않았습니다","경고",JOptionPane.WARNING_MESSAGE);
                    return;
                }
                String content  = "";
                String filePath  = chooser.getSelectedFile().getPath();
                BufferedReader br  = null;
                InputStreamReader isr  = null;
                FileInputStream fis  = null;
                File file  = new File(filePath);
                String temp  = "";
                try {
                    fis  = new FileInputStream(file);
                    isr  = new InputStreamReader(fis, "UTF-8");
                    br  = new BufferedReader(isr);
                    while( (temp  = br.readLine())  ! = null) {
                        content + = temp  + "\n";
                    }
                    TX.setText("");
                    TX.append(content);
                    TX.setEditable(false);
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
 
                } catch (Exception e1) {
                    e1.printStackTrace();
 
                } finally {
                    try {
                        fis.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    try {
                        isr.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    try {
                        br.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
            else if(cmd.equals("편집 버튼")){
                TX.setEditable(true);
                b.setText("다시 바꾸기");
            }
            else if(cmd.equals("다시 바꾸기")){
                TX.setEditable(false);
                b.setText("편집 버튼");
            }
            else if(cmd.equals("Save")){
                String s2  = TX.getText();
                System.out.println(s2);
                if(s2.length() ! =0){
                    chooser.showSaveDialog(contentPane);
                    File f = chooser.getSelectedFile();
                    int x  = JOptionPane.showConfirmDialog(null,"저장하시겠습니까?","저장",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
                    if(x = =JOptionPane.YES_OPTION){
                        String[] str =(TX.getText()).split("\n");//spilt을 사용해 TextArea의 텍스트를 가져온다.
                        try{
                            FileWriter fw = new FileWriter(f);
                            String text ="";
                            for(int i =0;i <str.length;i + +)
                                text =text +str[i] +"\r\n";
                            fw.write(text);
                            fw.flush();
                            fw.close();
                        }catch(IOException e2){}
                        JOptionPane.showMessageDialog(null,"잘 저장되었습니다.","저장완료",JOptionPane.INFORMATION_MESSAGE);
                    }
                } else {
                    JOptionPane.showMessageDialog(null,"저장할 내용이 없습니다.","에러",JOptionPane.INFORMATION_MESSAGE);
                }
            }
            else if(cmd.equals("Exit")){
                System.exit(0);
            }
            else if(cmd.equals("Find")){
                MyDialog finddialog;
                finddialog  = new MyDialog(null,"Find Text");
                finddialog.setVisible(true);
 
            }
        }
    }
    public static void main(String[] args) {
        new Project();
    }
}
cs

메모장 프로그램

 

실행 결과 ↓

 

 

열기(Ctrl + O) 눌렀을때 ↓

 

 

저장했을 경우 → 

 

문자열을 찾는 기능 (아직 구현되지 않음) → 

list
Comments

name

write a comment
TOTAL  0 Comments
* No comments. Please write comments