好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java仿Windows记事本源代码分享

本文实例为大家分享了java仿windows记事本的具体代码,供大家参考,具体内容如下

先上截图:



源代码:

?

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

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

import java.awt.*;

import javax.swing.*;

import javax.swing.filechooser.filenameextensionfilter;

import javax.swing.undo.undomanager;

import java.awt.event.*;

import java.io.*;

 

/**

  *

  * @author cjbi@outlook测试数据

  * @created 2015年7月6日 上午11:22:24

  */

public class jnotepad extends jframe implements actionlistener {

 

  jmenubar menubar = new jmenubar();

  jmenu file = new jmenu( "文件(f)" );

  jmenu edit = new jmenu( "编辑(e)" );

  jmenu format = new jmenu( "格式(o)" );

  jmenu help = new jmenu( "帮助(h)" );

  jmenuitem create = new jmenuitem( "新建" );

  jmenuitem open = new jmenuitem( "打开..." );

  jmenuitem save = new jmenuitem( "保存" );

  jmenuitem saveas = new jmenuitem( "另存为..." );

  jmenuitem exit = new jmenuitem( "退出" );

  jmenuitem undo = new jmenuitem( "撤销" );

  jmenuitem cut = new jmenuitem( "剪切" );

  jmenuitem copy = new jmenuitem( "复制" );

  jmenuitem paste = new jmenuitem( "粘贴" );

  jmenuitem findrep = new jmenuitem( "查找替换" );

  jmenuitem selectall = new jmenuitem( "全选" );

  jmenuitem font = new jmenuitem( "字体" );

  jmenuitem about = new jmenuitem( "关于" );

  jmenuitem cut2 = new jmenuitem( "剪切(x)" );

 

  jmenuitem copy2 = new jmenuitem( "复制(c)" );

  jmenuitem paste2 = new jmenuitem( "粘贴(v)" );

  jmenuitem selectall2 = new jmenuitem( "全选(a)" );

  public static jtextarea textarea = new jtextarea();

  undomanager um = new undomanager();

  jscrollpane scroll = new jscrollpane(textarea, scrollpaneconstants.vertical_scrollbar_always, scrollpaneconstants.horizontal_scrollbar_as_needed);

  jpopupmenu popup = new jpopupmenu();

  string pathselect;

 

  // 获取屏幕尺寸

  dimension screensize = toolkit.getdefaulttoolkit().getscreensize();

 

  public jnotepad() {

 

   // 此处定义键盘快捷键

   // menubar

   file.setmnemonic(keyevent.vk_f);

   edit.setmnemonic(keyevent.vk_e);

   format.setmnemonic(keyevent.vk_o);

   help.setmnemonic(keyevent.vk_h);

   // menuitem

   create.setaccelerator(keystroke.getkeystroke(keyevent.vk_n, actionevent.ctrl_mask));

   open.setaccelerator(keystroke.getkeystroke(keyevent.vk_o, actionevent.ctrl_mask));

   save.setaccelerator(keystroke.getkeystroke(keyevent.vk_s, actionevent.ctrl_mask));

   undo.setaccelerator(keystroke.getkeystroke(keyevent.vk_z, actionevent.ctrl_mask));

   cut.setaccelerator(keystroke.getkeystroke(keyevent.vk_x, actionevent.ctrl_mask));

   copy.setaccelerator(keystroke.getkeystroke(keyevent.vk_c, actionevent.ctrl_mask));

   paste.setaccelerator(keystroke.getkeystroke(keyevent.vk_v, actionevent.ctrl_mask));

   findrep.setaccelerator(keystroke.getkeystroke(keyevent.vk_f, actionevent.ctrl_mask));

   selectall.setaccelerator(keystroke.getkeystroke(keyevent.vk_a, actionevent.ctrl_mask));

 

   // 事件监听者

   save.addactionlistener( this );

   create.addactionlistener( this );

   open.addactionlistener( this );

   saveas.addactionlistener( this );

   exit.addactionlistener( this );

   undo.addactionlistener( this );

   cut.addactionlistener( this );

   copy.addactionlistener( this );

   paste.addactionlistener( this );

   selectall.addactionlistener( this );

   font.addactionlistener( this );

   about.addactionlistener( this );

   cut2.addactionlistener( this );

   copy2.addactionlistener( this );

   paste2.addactionlistener( this );

   selectall2.addactionlistener( this );

   findrep.addactionlistener( this );

   // 设置撤销文本的管理器

   textarea.getdocument().addundoableeditlistener(um);

   textarea.setfont(format.font);

   // 文件

   file.add(create);

   file.add(open);

   file.add(save);

   file.add(saveas);

   file.addseparator();

   file.add(exit);

 

   // 编辑

   edit.add(undo);

   edit.addseparator();

   edit.add(cut);

   edit.add(copy);

   edit.add(paste);

   edit.addseparator();

   edit.add(findrep);

   edit.addseparator();

   edit.add(selectall);

 

   // 格式

   format.add(font);

 

   // 帮助

   help.add(about);

 

   // 菜单栏

   menubar.add(file);

   menubar.add(edit);

   menubar.add(format);

   menubar.add(help);

 

   // 右键菜单

   popup.add(cut2);

   popup.add(copy2);

   popup.add(paste2);

   popup.addseparator();

   popup.add(selectall2);

 

   // 添加到文本域容器

   textarea.add(popup);

 

   // 匿名内部类监听器右键动作

   textarea.addmouselistener( new mouseadapter() {

    public void mousereleased(mouseevent e) {

     if (e.getbutton() == mouseevent.button3) {

      popup.show(textarea, e.getx(), e.gety());

     }

    }

   });

 

   // 边界布局

   this .add(menubar, borderlayout.north);

   this .add(scroll, borderlayout.center);

   this .settitle( "记事本" );

   this .setsize( 500 , 400 );

   this .setlocationrelativeto( null );

   this .seticonimage( new imageicon( this .getclass().getresource( "/icon/notepad.png" )).getimage()); //图标放在源目录的icon文件夹

   this .setdefaultcloseoperation(jframe.dispose_on_close);

   this .setvisible( true );

 

  }

 

  // 重写actionperformed

  @override

  public void actionperformed(actionevent e) {

   // event对象发生源

   if (e.getsource() == open) {

 

    jfilechooser chooser = new jfilechooser();

    filenameextensionfilter filter = new filenameextensionfilter( "文本文档(*.txt)" , "txt" );

    chooser.setfilefilter(filter);

    chooser.setdialogtitle( "文件打开" );

    chooser.showopendialog( null );

    chooser.setvisible( true );

 

    try {

     pathselect = chooser.getselectedfile().getpath();

     filereader wjl = new filereader(pathselect);

     bufferedreader hcl = new bufferedreader(wjl);

     string s = "" , zfc = "" ;

     while ((s = hcl.readline()) != null ) {

      zfc += (s + "\n" );

     }

     textarea.settext(zfc);

 

    } catch (exception e1) {

    }

   }

 

   if (e.getsource() == saveas) { // 另存为

 

    jfilechooser chooser = new jfilechooser();

    filenameextensionfilter filter = new filenameextensionfilter( "文本文档(*.txt)" , "txt" );

    chooser.setfilefilter(filter);

    chooser.setdialogtitle( "另存为" );

    chooser.showsavedialog( null );

    chooser.setvisible( true );

 

    printstream ps;

    try {

     string select = chooser.getselectedfile().getpath();

     ps = new printstream(select);

     system.setout(ps);

     system.out.println( this .textarea.gettext());

 

    } catch (exception e1) {

    }

   }

 

   if (e.getsource() == save && (pathselect == null )) { // 保存

    jfilechooser chooser = new jfilechooser();

 

    chooser.setdialogtitle( "保存" );

    chooser.showsavedialog( null );

    chooser.setvisible( true );

 

    printstream ps;

    try {

     pathselect = chooser.getselectedfile().getpath();

     ps = new printstream(pathselect);

     system.setout(ps);

     system.out.println( this .textarea.gettext());

 

    } catch (exception e1) {

    }

   } else if (e.getsource() == save && !(pathselect == null )) {

    printstream ps;

    try {

     ps = new printstream(pathselect);

     system.setout(ps);

     system.out.println( this .textarea.gettext());

    } catch (filenotfoundexception e1) {

    }

   }

 

   if (e.getsource() == create) {

    textarea.settext( "" );

    pathselect = null ;

   }

 

   if (e.getsource() == exit) {

    system.exit( 0 );

   }

 

   if (e.getsource() == undo) {

    if (um.canundo()) {

     um.undo();

    }

   }

 

   if (e.getsource() == cut || e.getsource() == cut2) {

    textarea.cut();

   } else if (e.getsource() == copy || e.getsource() == copy2) {

    textarea.copy();

   } else if (e.getsource() == paste || e.getsource() == paste2) {

    textarea.paste();

   } else if (e.getsource() == findrep) {

    new findandreplace(textarea);

   }

 

   else if (e.getsource() == selectall || e.getsource() == selectall2) {

    textarea.selectall();

   }

   if (e.getsource() == font) {

    new format(textarea);

   }

   if (e.getsource() == about) {

    new about();

   }

 

  }

 

  public static void main(string[] args) {

   new jnotepad();

  }

 

}

 

class findandreplace extends jdialog implements actionlistener { // 查找和替换

  jlabel findlabel = new jlabel( "查找内容:" );

  jlabel replabel = new jlabel( " 替换为:" );

  jtextfield findtf = new jtextfield( 8 );

  jtextfield reptf = new jtextfield( 8 );

  jbutton findbtn = new jbutton( "查找" );

  jbutton repbtn = new jbutton( "替换" );

  jpanel findpn = new jpanel();

  jpanel reppn = new jpanel();

  jtextarea textarea;

 

  string text;

  boolean flg = false ;

  int len;

  int start = 0 ;

  int k = 0 ;

 

  public findandreplace(jtextarea textarea) {

 

   this .textarea = textarea;

 

   findpn.add(findlabel);

   findpn.add(findtf);

   findpn.add(findbtn);

   reppn.add(replabel);

   reppn.add(reptf);

   reppn.add(repbtn);

   this .add(findpn);

   this .add(reppn);

 

   findbtn.addactionlistener( this );

   repbtn.addactionlistener( this );

 

   this .settitle( "查找和替换" );

   this .setlayout( new gridlayout( 2 , 1 ));

   // this.setbounds(400, 200, 300, 140);

   this .pack();

   this .setlocationrelativeto( null );

   this .setresizable( false );

   this .setvisible( true );

   this .setdefaultcloseoperation(jdialog.dispose_on_close);

  }

 

  @suppresswarnings ( "deprecation" )

  public void actionperformed(actionevent e) {

   string findtext = findtf.gettext();

   string reptext = reptf.gettext();

   text = textarea.gettext();

   if (e.getsource() == findbtn) {

    findbtn.setlabel( "下一个" );

    if (findtext != null ) {

     len = findtext.length();

     start = text.indexof(findtext, k);

     k = start + len;

     textarea.select(start, start + len);

     flg = true ;

     if (start == - 1 ) {

      joptionpane.showmessagedialog( null , "已到文件尾部!" , "提示" , joptionpane.information_message);

      start = 0 ;

      k = 0 ;

      flg = false ;

     }

    }

   } else if (e.getsource() == repbtn) {

    if (flg) {

     textarea.replacerange(reptext, start, start + len);

     flg = false ;

    }

   }

  }

}

 

// 字体格式

class format extends jdialog implements actionlistener {

 

  public static int style = 0 ; // 全局变量类型,默认值为0

  public static int size = 16 ; // 全局变量字体大小,默认值为16

  public static font font = new font( "新宋体" , style, size); // 全局变量字体,默认值为新宋体

 

  jpanel pn = new jpanel();

  jpanel okcelpn = new jpanel();

  jpanel fontpn = new jpanel();

  jpanel ptpn = new jpanel();

  jlabel fontlabel = new jlabel( "字体: " );

  jlabel fontstylelabel = new jlabel( " 字形: " );

  jlabel ptlabel = new jlabel( "  磅值: " );

  jbutton ok = new jbutton( "确定" );

  jbutton cancel = new jbutton( "取消" );

  graphicsenvironment e = graphicsenvironment.getlocalgraphicsenvironment(); // 获取系统中可用的字体的名字

  string[] fontname = e.getavailablefontfamilynames(); // 获取系统中可用的字体的名字

  string[] fonttype = { "常规" , "倾斜" , "粗体" , "粗偏斜体" };

  jlist fontlist = new jlist(fontname);

  jlist fonttypelist = new jlist(fonttype);

  jscrollpane fontscroll = new jscrollpane(fontlist);

  jscrollpane fonttypescroll = new jscrollpane(fonttypelist);

 

  jtextarea textarea;

  spinnermodel spinnermodel = new spinnernumbermodel(size, // initial value

    0 , // min

    100 , // max

    2 // step

  );

  jspinner spinner = new jspinner(spinnermodel);

 

  public format(jtextarea textarea) {

   this .textarea = textarea;

   ok.addactionlistener( this );

   cancel.addactionlistener( this );

 

   pn.setlayout( new gridlayout( 2 , 1 ));

   pn.add(fontpn);

   pn.add(ptpn);

 

   fontpn.add(fontlabel);

   fontpn.add(fontscroll);

   fontpn.add(fontstylelabel);

   fontpn.add(fonttypescroll);

 

   ptpn.add(ptlabel);

   ptpn.add(spinner);

 

   fontlist.setvisiblerowcount( 5 );

   fontlist.setfixedcellwidth( 60 );

   fontlist.setselectedindex( 50 );

   fontlist.setselectedvalue(font.getfontname(), true );

 

   fonttypelist.setvisiblerowcount( 5 );

   fonttypelist.setselectedindex(style);

   okcelpn.add(ok);

   okcelpn.add(cancel);

 

   okcelpn.setlayout( new flowlayout(flowlayout.right));

 

   this .add(pn, borderlayout.center);

   this .add(okcelpn, borderlayout.south);

 

   this .settitle( "字体" );

   this .pack();

   this .setlocationrelativeto( null );

   this .setresizable( false );

   this .setvisible( true );

   this .setdefaultcloseoperation(jframe.dispose_on_close);

  }

 

  public void actionperformed(actionevent e) {

   if (e.getsource() == ok) {

    system.out.println(fontlist.getselectedvalue());

    style = this .type();

    size = integer.parseint(spinner.getvalue().tostring());

    font = new font((string) fontlist.getselectedvalue(), style, size);

    textarea.setfont(font);

    this .dispose();

    system.out.println(type());

   } else if (e.getsource() == cancel) {

    this .dispose();

   }

  }

 

  private int type() {

   if (fonttypelist.getselectedvalue().equals( "倾斜" )) {

    return 1 ;

   } else if (fonttypelist.getselectedvalue().equals( "粗体" )) {

    return 2 ;

   } else if (fonttypelist.getselectedvalue().equals( "粗偏斜体" )) {

    return 3 ;

   } else

    return 0 ;

  }

 

}

 

class about extends jdialog { // 关于窗口

 

  about() {

   joptionpane.showmessagedialog( null , " 作者:cjb 版本:v1.5\n\n 联系:cjbi@outlook测试数据" , "关于" ,

     joptionpane.plain_message);

  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

查看更多关于Java仿Windows记事本源代码分享的详细内容...

  阅读:26次