บทความนี้จะอธิบายเกี่ยวกับแผ่นจดบันทึกอย่างง่ายที่ใช้งานใน Java แบ่งปันกับทุกคนสำหรับการอ้างอิงของคุณ รายละเอียดมีดังนี้:
ฉันรู้สึกว่าสิ่งนี้ไม่ดูดีเท่าที่ฉันเคยเขียนโดยใช้ Windows API มาก่อน - -
เวอร์ชัน JDK: 1.7.0
เอฟเฟกต์ดังแสดงด้านล่าง:
ซอร์สโค้ดมีดังนี้:
import java.io.*; import java.awt.*; import java.awt.event.*; เมนูส่วนตัวแก้ไข; MenuItem ส่วนตัว [] miFile; เฟรมส่วนตัวสุดท้าย = this; /** * คลาสภายใน * ตัวจัดการข้อความ */ คลาส EventExit ActionListener { โมฆะสาธารณะ actionPerformed (ActionEvent e) { System.exit (0); } } คลาส SystemExit ขยาย WindowAdapter { โมฆะสาธารณะ windowClosing (WindowEvent e) { System.exit (0); } } คลาส EventMenuClose ดำเนินการ ActionListener { สาธารณะ void actionPerformed ( ActionEvent e) { ta.setText(null); } } คลาส EventOpenFile ใช้งาน ActionListener { public การดำเนินการเป็นโมฆะ (ActionEvent e) { // สร้าง OpenFile Dialog FileDialog dlg = new FileDialog (frame, "Open Files", FileDialog.LOAD); String strPath; if ((strPath = dlg.getDirectory () ) != null) { //รับเส้นทางแบบเต็มของไฟล์ที่เลือก strPath += dlg.getFile(); //เปิดไฟล์ ลอง { FileInputStream fis = new FileInputStream(strPath); BufferedInputStream bis = new BufferedInputStream(fis); byte[] buf = new byte[3000]; (buf ); ta.append(สตริงใหม่(buf,0,len)); bis.close(); } เช่น) { ex.printStackTrace(); ** * วิธีการก่อสร้าง * การเพิ่มเมนูและส่วนประกอบ TextArea * @param strTitle */ public PadFrame(String strTitle) { super(strTitle); this.setSize(900, 630); //สร้างแถบเมนู mb = new MenuBar(); menuFile = new Menu("File"); menuEdit = new Menu("Edit"); miFile = new MenuItem[]{new MenuItem("Open"),new MenuItem("ปิด"),new MenuItem("Exit"}); .setMenuBar(mb); mb.add(menuFile); mb.add(menuEdit); สำหรับ(int i = 0; i < miFile.length ; ++i) { menuFile.add(miFile[i]); } //เพิ่มตัวจัดการเหตุการณ์ setMenuEventHandle(new EventExit(),"File",2); setMenuEventHandle(new EventOpenFile(),"File", 0); setMenuEventHandle (ใหม่ EventMenuClose (), "ไฟล์", 1); this.addWindowListener (new SystemExit()); // เพิ่มองค์ประกอบ TextArea ta = new TextArea (30,30); this.add (ta); } โมฆะสาธารณะ setMenuEventHandle (ActionListener al, String strMenu, int index) { if (strMenu == "ไฟล์ ") { miFile[index].addActionListener(al); } } สาธารณะ int getMenuItemAmount(String strMenu) { if("File" == strMenu) { return miFile.length; } สาธารณะคงเป็นโมฆะ main (String [] args) { PadFrame f = new PadFrame ("NotePad"); }ฉันหวังว่าบทความนี้จะเป็นประโยชน์กับการเขียนโปรแกรม Java ของทุกคน