この記事では、Java で実装された単純なメモ帳について説明します。皆さんの参考に共有してください。詳細は以下のとおりです。
これは、以前 Windows API を使用して作成したものほど見栄えが良くないように感じます。 。 。
JDKバージョン: 1.7.0
効果は以下の通りです。
ソースコードは次のとおりです。
import java.io.*; import java.awt.*; /** * メインウィンドウ * @author Neo Smith */ class PadFrame extends Frame { private MenuBar mb; private Menu menuEdit; private MenuItem[] miFile; private TextArea ta; /** * 内部クラス * メッセージ ハンドル */ class EventExit; ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } class SystemExit extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } class EventMenuCloseimplements ActionListener { public void actionPerformed( ActionEvent e) { ta.setText(null) } } class EventOpenFile は ActionListener { public void actionPerformed(ActionEvent e) { //OpenFile ダイアログを作成します FileDialog dlg = new FileDialog(frame,"Open Files",FileDialog.LOAD); strPath; ) != null) { //選択したファイルのフルパスを取得 strPath += dlg.getFile(); // ファイルを開く try { FileInputStream fis = new BufferedInputStream(fis); byte[] buf = new byte[3000]; (buf ); ta.append(new String(buf,0,len)); 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(menuEdit); miFile.length ; ++i) { menuFile.add(miFile[i]) } // イベントハンドルを追加 setMenuEventHandle(new EventExit(),"File", 0); setMenuEventHandle(new EventMenuClose(),"ファイル",1); SystemExit()); //TextArea コンポーネントを追加します ta = new TextArea(30,30); this.add(ta) } public void setMenuEventHandle(ActionListener al,String strMenu,intindex) { if(strMenu == " ") { 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 プログラミングに役立つことを願っています。