이 문서에서는 Java로 구현된 간단한 메모장을 설명합니다. 참고할 수 있도록 모든 사람과 공유하세요. 세부사항은 다음과 같습니다:
이전에 Windows API를 사용하여 작성한 것만큼 보기에 좋지는 않은 것 같습니다. . .
JDK 버전: 1.7.0
효과는 아래와 같습니다.
소스 코드는 다음과 같습니다.
import java.io.*; import java.awt.event.* /** * 기본 창 * @author Neo Smith */ class PadFrame은 Frame을 확장합니다. private MenuBar mb; private MenuEdit; private MenuItem[] miFile; private TextArea ta; /** * 내부 클래스 * 메시지 핸들 */ 클래스 EventExit 구현; ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0) } } 클래스 SystemExit는 WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0) } } 클래스 EventMenuClose는 ActionListener { public void actionPerformed( ActionEvent e) { ta.setText(null) } } 클래스 EventOpenFile은 ActionListener를 구현합니다. void actionPerformed(ActionEvent e) { //OpenFile 대화 상자 만들기 FileDialog dlg = new FileDialog(frame,"Open Files",FileDialog.LOAD) dlg.show() if((strPath = dlg.getDirectory(); ) != null) { //선택한 파일의 전체 경로를 가져옵니다. strPath += dlg.getFile(); //파일 열기 try { FileInputStream fis = new FileInputStream(strPath); BufferedInputStream(fis) byte[] buf = new byte[3000]; (buf); ta.append(new String(buf,0,len)) } catch(예외 ex) { ex.printStackTrace(); } } } /** * 구성 방법 * Menu 및 TextArea 구성 요소 추가 * @param strTitle */ public PadFrame(String strTitle) { super(strTitle); this.setSize(900, 630); //메뉴바 생성 mb = new MenuBar(); Menu("파일"); menuEdit = new Menu("편집"); miFile = new MenuItem[]{new MenuItem("열기"),new MenuItem("닫기")}; .setMenuBar(mb); mb.add(menuFile) for(int i = 0; i < miFile.length ; ++i) { menuFile.add(miFile[i]) } //이벤트 핸들 추가 setMenuEventHandle(new EventExit(),"File",2); setMenuEventHandle(new EventOpenFile(),"File", 0); setMenuEventHandle(new EventMenuClose(),"파일",1); SystemExit()); //TextArea 구성요소 추가 ta = new TextArea(30,30); this.add(ta) } public void setMenuEventHandle(ActionListener al,String strMenu,int index) { if(strMenu == "File ") { miFile[index].addActionListener(al); } } public int getMenuItemAmount(String strMenu) { if("File" == strMenu) { return miFile.length; } return -1; } public static void main(String[] args) { PadFrame f = new PadFrame("NotePad"); }이 글이 모든 사람의 Java 프로그래밍에 도움이 되기를 바랍니다.