java swing 记事本_java 写的swing记事本

这篇博客介绍了如何使用Java Swing库创建一个简单的记事本应用程序。文章涵盖了从创建主窗口、菜单栏、文本区域,到添加文件操作、编辑功能和右键菜单的完整过程。代码示例展示了如何实现文件打开、保存、复制、粘贴、查找和替换等功能。
摘要由CSDN通过智能技术生成

最近用java写的swing源代码:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

public class Notepad3

{

public static void main(String [] args)

{

Window mainWin = new Window("New - Notepad3");

}

}

class Window extends JFrame implements ActionListener

{

JMenuBar menubar;

JTextArea mainText;

JFileChooser openFile;

JFileChooser saveFile;

JPopupMenu contextMenu;

JMenuItem popupcopy, popupcut, popuppaste, popupdel;

JMenu file;

JMenuItem New, Open, Save, SaveAs, NewWindow, Encode, Exit;

JMenu edit;

JMenuItem redo, undo, cut, copy, paste, del, find, findNext, replace;

String findStr = null; // 要查找的字符串-findStr

JMenu help;

JMenuItem about;

JMenu view;

JMenuItem font, linenum, wordCounts;

JCheckBoxMenuItem wordWrap;

//mainText上方的按钮,没有用处的

JButton buttonRun, buttonRun2;

Icon icon1;

//在mainText下面添加状态栏;

JLabel wordCount;

Window(String s)

{

setTitle(s);

setBounds(100,100,800,600);

setVisible(true);

setResizable(true);

{

//添加工具栏

menubar = new JMenuBar();

//添加菜单-文件

file = new JMenu("File");

New = new JMenuItem("New", new ImageIcon("new.gif"));

Open = new JMenuItem("Open", new ImageIcon("open.gif"));

Save = new JMenuItem("Save", new ImageIcon("save.gif"));

SaveAs = new JMenuItem("Save as", new ImageIcon("saveas.gif"));

NewWindow= new JMenuItem("New Window", new ImageIcon("newwindow.gif"));

Encode = new JMenuItem("Encode", new ImageIcon("encode.gif"));

Exit= new JMenuItem("Exit", new ImageIcon("exit.gif"));

New.setMnemonic('N');//为“新建”设置快捷键Ctrl+N

New.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));

Open.setAccelerator(KeyStroke.getKeyStroke('O'));

//menuSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));

openFile = new JFileChooser("d:/"); //打开文件夹的对象

file.add(New);

file.add(Open);

file.addSeparator();

file.add(Save);

file.add(SaveAs);

file.addSeparator();

file.add(NewWindow);

file.add(Encode);

file.add(Exit);

New.addActionListener(this);

Open.addActionListener(this);

Save.addActionListener(this);

//添加菜单-编辑

edit= new JMenu("Edit");

redo = new JMenuItem("Redo");

undo = new JMenuItem("Undo");

cut = new JMenuItem("Cut");

copy = new JMenuItem("Copy");

paste = new JMenuItem("Paste");

del= new JMenuItem("Delete");

find = new JMenuItem("Find");

findNext = new JMenuItem("FindNext");

replace = new JMenuItem("Replace");

edit.add(redo);

edit.add(undo);

edit.addSeparator();

edit.add(cut);

edit.add(copy);

edit.add(paste);

edit.add(del);

edit.addSeparator();

edit.add(find);

edit.add(findNext);

edit.add(replace);

copy.addActionListener(this);

cut.addActionListener(this);

paste.addActionListener(this);

del.addActionListener(this);

find.addActionListener(this);

findNext.addActionListener(this);

replace.addActionListener(this);

//添加菜单-View

view = new JMenu("View");

font = new JMenuItem("Font");

wordWrap = new JCheckBoxMenuItem("Word Wrap", false);

linenum = new JMenuItem("Line Numbers");

wordCounts= new JMenuItem("Word Counts");

view.add(font);

view.add(wordWrap);

view.addSeparator();

view.add(linenum);

view.add(wordCounts);

wordCounts.addActionListener(this);

wordWrap.addActionListener(this);

//添加菜单-Help

help = new JMenu("Help");

about = new JMenuItem("About");

help.add(about);

about.addActionListener(this);

//添加到菜单栏

menubar.add(file);

menubar.add(edit);

menubar.add(view);

menubar.add(help);

setJMenuBar(menubar);

}

//添加文本区域

mainText = new JTextArea("haha");

//添加文本区域到窗口,并增加滚动条

add(new JScrollPane(mainText), BorderLayout.CENTER);

//右键显示的监听事件

mainText.addMouseListener(new MouseAdapter(){

public void mousePressed(MouseEvent e){

if(e.getModifiers()==InputEvent.BUTTON3_MASK)

contextMenu.show(mainText,e.getX(),e.getY());

}

});

//添加右键菜单项目

JPopupMenu contextMenu = new JPopupMenu();

popupcopy = new JMenuItem("copy");

popupcut = new JMenuItem("cut");

popuppaste = new JMenuItem("paste");

popupdel = new JMenuItem("Delete");

contextMenu.add(popupcut);

contextMenu.add(popupcopy);

contextMenu.add(popuppaste);

contextMenu.add(popupdel);

popupcopy.addActionListener(this);

popupcut.addActionListener(this);

popuppaste.addActionListener(this);

popupdel.addActionListener(this);

icon1= new ImageIcon("a.jag");

buttonRun = new JButton(icon1);

buttonRun.setMnemonic('d');

add(buttonRun,BorderLayout.NORTH);

buttonRun.addActionListener(this);

wordCount = new JLabel("本文共" + mainText.getText().length() + "个字");

add(wordCount,BorderLayout.SOUTH);

validate();

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

}

public void actionPerfoemed(ActionEvent e)

{

int i=0;

i=(i+1)%2;

if(i==1)

buttonRun.setIcon(icon1);

}

//文本区域的监听事件

public void actionPerformed(ActionEvent e)

{

if (e.getSource()==copy || e.getSource()==popupcopy)

mainText.copy();

else if (e.getSource()==cut || e.getSource()==popupcut)

mainText.cut();

else if (e.getSource()==paste || e.getSource()==popuppaste)

mainText.paste();

else if (e.getSource()==wordCounts)

wordCount.setText("本文共" + mainText.getText().length() + "个字");

else if (e.getSource()==about)

JOptionPane.showConfirmDialog(this, "\n Power By Yantze\n mail:yantze@126.com", "关于", JOptionPane.CLOSED_OPTION );

else if (e.getSource()==wordWrap)

mainText.setLineWrap(!mainText.getLineWrap());

else if (e.getSource()==del || e.getSource() == popupdel)

mainText.replaceRange("",mainText.getSelectionStart(),mainText.getSelectionEnd()); //删除选中的文本

else if (e.getSource()==find || e.getSource()==findNext)//实现查找和查找下一个的功能

{

if(findStr==null || e.getSource()==find )

{

findStr= JOptionPane.showInputDialog(this, "请输入要找的字符串(Regex-open)!");

if ((findStr!=null)&&!mainText.equals(""))

{

int position =mainText.getText().indexOf(findStr);

if(position!=-1)

{

mainText.setSelectionStart(position);

mainText.setSelectionEnd(position + findStr.length());

}

else JOptionPane.showMessageDialog(this, "未查找到字符串!");

}

}

else

{

if ((findStr!=null)&&!findStr.equals(""))

{

int position = mainText.getText().indexOf(findStr, mainText.getSelectionStart()+1) ;

if(position!=-1){

mainText.setSelectionStart(position);

mainText.setSelectionEnd(position+findStr.length());

}

else JOptionPane.showMessageDialog(this, "不存在要查找的字符串!");

}

}

}

else if(e.getSource()==replace){

findStr = null;

String replaceStr;

String str;

findStr= JOptionPane.showInputDialog(this, "请输入替换的字符串!(Regex-open)");

if ((findStr!=null)&&!mainText.equals("")){

int position =mainText.getText().indexOf(findStr);

if(position!=-1){

mainText.setSelectionStart(position);

mainText.setSelectionEnd(position+findStr.length());

replaceStr = JOptionPane.showInputDialog(this,"请输入替换的字符串!");

if(replaceStr!=null){

str = mainText.getText().replace(findStr, replaceStr);

mainText.setText(str);

}

}

else JOptionPane.showMessageDialog(this, "没有你要替换的字符串");

}

else

JOptionPane.showMessageDialog(this, "没有你想替换的的字符!");

}

else if (e.getSource()==New)

{

int n=JOptionPane.showConfirmDialog(this,"在新建之前,想保存文件吗?","新建文档",JOptionPane.YES_NO_CANCEL_OPTION);

if(n == JOptionPane.YES_OPTION)

{

mainText.setText(null);

}

if(n == JOptionPane.NO_OPTION)

{

mainText.setText(null);

}

}

else if (e.getSource()==Open)

{

int n = openFile.showOpenDialog(null);

if (n == JFileChooser.APPROVE_OPTION) //这段代码用到了java.io.*;

{

File file = openFile.getSelectedFile();

try

{

FileReader readFile = new FileReader(file);

BufferedReader in = new BufferedReader(readFile);

String s = null;

while ((s=in.readLine()) != null)

mainText.append(s + "\n");

}

catch(IOException ee){}

}

}

else if (e.getSource()==Save)

{

/*

saveFile = new JFileChooser();

saveFile .setApproveButtonText("确定");

saveFile .setDialogTitle("另存为");

int result = saveFile.showSaveDialog(this);

if (result == JFileChooser.CANCEL_OPTION) {

statusLabel.setText("没有选择任何文件");

return;

}

File saveFileName = fileChooser.getSelectedFile();

if (saveFileName == null || saveFileName.getName().equals(""))

JOptionPane.showMessageDialog(this, "不合法的文件名", "不合法的文件名",

JOptionPane.ERROR_MESSAGE);

else {

try {

FileWriter fw = new FileWriter(saveFileName);

BufferedWriter bfw = new BufferedWriter(fw);

bfw.write(mainText.getText(), 0, mainText.getText().length());

bfw.flush();

fw.close();

isNewfile = false;

file = saveFileName;

oldValue = tex.getText();

statusLabel.setText(" 当前打开文件:" + saveFileName.getAbsoluteFile());

} catch (IOException ioException) {

}

}

*/

try{

int result = saveFile.showSaveDialog(this);

saveFile.setApproveButtonText("确定");

File save = saveFile.getSelectedFile();

FileWriter writeOut = new FileWriter(save);

writeOut.write(mainText.getText());

writeOut.flush();

writeOut.close();

}catch (IOException eee){

System.out.println("Error writing file");

}

}

}

}

效果图:

a9f9b75d2535ff03549be2ef0cd4097b.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值