DDDDigtal 4ensics

[Java] 메모장 만들기 본문

카테고리 없음

[Java] 메모장 만들기

Dx4 2023. 6. 17. 12:45
package notepad;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JFileChooser;

public class main_form {

	private JFrame frmNotepad;
	private String filepath = null;
	private JTextArea textArea;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					main_form window = new main_form();
					window.frmNotepad.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public main_form() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		
		
		
		
		
		frmNotepad = new JFrame();
		frmNotepad.setTitle("null - notepad");
		frmNotepad.setBounds(100, 100, 800, 600);
		frmNotepad.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JScrollPane scrollPane = new JScrollPane();
		GroupLayout groupLayout = new GroupLayout(frmNotepad.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 784, Short.MAX_VALUE)
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 561, Short.MAX_VALUE)
		);
		
		textArea = new JTextArea();
		scrollPane.setViewportView(textArea);
		frmNotepad.getContentPane().setLayout(groupLayout);
		
		JMenuBar menuBar = new JMenuBar();
		frmNotepad.setJMenuBar(menuBar);
		
		JMenu fileM = new JMenu("파일");
		JMenu editM = new JMenu("편집");
		
		menuBar.add(fileM);
		menuBar.add(editM);
		
		JMenuItem save = new JMenuItem("저장");
		JMenuItem save_as = new JMenuItem("다른 이름으로 저장");
		JMenuItem open = new JMenuItem("열기");
		JMenuItem exit = new JMenuItem("저장하지 않고 닫기");
		
		fileM.add(save);
		fileM.add(save_as);
		fileM.addSeparator();
		fileM.add(open);
		fileM.addSeparator();
		fileM.add(exit);
		
		save.addActionListener(new MenuActionListener());
		save_as.addActionListener(new MenuActionListener());
		open.addActionListener(new MenuActionListener());
		exit.addActionListener(new MenuActionListener());
		
		
		
		
		
		
	}
	
	class MenuActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			String cmd = e.getActionCommand();
			switch(cmd) {

			case "저장" : 
				if(filepath == null) {
					try {
						JFileChooser jfc = new JFileChooser();
				        int returnVal = jfc.showSaveDialog(null);
				        if(returnVal == 0) {
				        	filepath = jfc.getSelectedFile().toString();
				        }
				        File fi = new File(filepath);
				        frmNotepad.setTitle(fi.getName() + " - notepad");
						BufferedOutputStream bs = new BufferedOutputStream(new FileOutputStream(filepath));
						String str = textArea.getText();
						bs.write(str.getBytes());
						bs.close();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (NullPointerException e1) {
						
					}
				} else {
					try {
					BufferedOutputStream bs = new BufferedOutputStream(new FileOutputStream(filepath));
					String str = textArea.getText();
					bs.write(str.getBytes());
					bs.close();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
				
				break;
			case "열기" : 
				String strs = "";
				textArea.setText(strs);
				try {
				JFileChooser jfc = new JFileChooser();
		        int returnVal = jfc.showSaveDialog(null);
		        if(returnVal == 0) {
		        	filepath = jfc.getSelectedFile().toString();
		        }
		        BufferedReader br = new BufferedReader(new FileReader(filepath));
		        String str;
		        File fi = new File(filepath);
		        frmNotepad.setTitle(fi.getName() + " - notepad");
		        while((str = br.readLine()) != null) {
		        	textArea.append(str + System.lineSeparator());
		        }
		        br.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			
			}
			
		}
	}
	
}

그냥 친구가 만들어달래서 만들어봄 

 

열기 저장 밖에 안만듬. 나라면 글꼴 변경, 다른 이름으로 저장 등등 다양한 기능을 더 만들 수 있을 듯 틀만 잡아달라고 부탁했으니 이정도만 함.

저장도 잘되고 비록 txt파일은 아니지만 열기도 잘 됨 한자도 잘 출력된다.

귀찮지만 마지막 테스트