⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jnotepad.java

📁 1. 可显示当前鼠标的行列位置 2. 实现字符串的查找与替换 3. 实现记事本中的常用菜单项 4. *实现文件的打开与保存 5. *设置文本的字体
💻 JAVA
字号:
import java.awt.*; 
import java.awt.event.*; 
import java.awt.datatransfer.*; 
import java.io.*; 
import javax.swing.*; 
import javax.swing.event.*; 
import javax.swing.text.*; 

public class JNotepad extends JPanel 
{ 
JTextArea jta = new JTextArea("", 18, 24); //定义文本区域

JScrollPane jsp = new JScrollPane(jta); //定义滚动框

JMenuBar jmb = new JMenuBar(); //定义菜单栏
JMenu file = new JMenu("文件"); //定义菜单
JMenu edit = new JMenu("编辑"); 
JMenu help = new JMenu("帮助"); 
JMenu search = new JMenu("查找"); 

JToolBar toolBar = new JToolBar(); //定义工具栏

JMenuItem jmi; //定义菜单项

Clipboard clipbd = getToolkit().getSystemClipboard(); 


String text = ""; 

public JNotepad()  //构造函数
{ 
class newL implements ActionListener  //新建文本区域jta的监听类
{
public void actionPerformed(ActionEvent e) 
{ 
jta.setDocument(new PlainDocument()); 
} 
} 

class openL implements ActionListener //打开文件的监听类
{ 
public void actionPerformed(ActionEvent e) 
{ 
JFileChooser fc = new JFileChooser();     //实现保存文件和打开文件的标准对话框
int returnVal = fc.showDialog(JNotepad.this, "Open file"); 
if(returnVal == JFileChooser.APPROVE_OPTION) 
{ 
String file = fc.getSelectedFile().getPath(); //用file保存文件名
if(file == null) 
{ 
return; 
} 
try              //打开文件的异常处理
{ 
Reader in = new FileReader(file); 
char[] buff = new char[4096]; 
int nch; 
while((nch = in.read(buff, 0, buff.length)) != -1) 
{ 
jta.setDocument(new PlainDocument()); 
jta.append(new String(buff, 0, nch)); 
} 
} 
catch (IOException io) 
{ 
System.err.println("IOException: " + io.getMessage()); 
} 
} 
else 
{ 
return; 
} 
} 
} 

class saveAs implements ActionListener  //另存为的监听类
{ 
public void actionPerformed(ActionEvent e) 
{ 
JFileChooser fc = new JFileChooser(); 
int returnVal = fc.showSaveDialog(JNotepad.this); 
if(returnVal == JFileChooser.APPROVE_OPTION) 
{ 
String savefile = fc.getSelectedFile().getPath(); 
if(savefile == null) 
{ 
return; 
} 
else 
{ 
String docToSave = jta.getText(); 
if(docToSave != null) 
{ 
FileOutputStream fstrm = null; 
BufferedOutputStream ostrm = null; 
try       //开始保存的异常处理
{ 
fstrm = new FileOutputStream(savefile); 
ostrm = new BufferedOutputStream(fstrm); 
byte[] bytes = null; 
try 
{ 
bytes = docToSave.getBytes(); 
} 
catch(Exception e1) 
{ 
e1.printStackTrace(); 
} 
ostrm.write(bytes); 
} 
catch(IOException io) 
{ 
System.err.println("IOException: " + 
io.getMessage()); 
} 
finally 
{ 
try 
{ 
ostrm.flush(); 
fstrm.close(); 
ostrm.close(); 
} 
catch(IOException ioe) 
{ 
System.err.println("IOException: " + 
ioe.getMessage()); 
} 
} 
} 
} 
} 
else 
{ 
return; 
} 
} 
} 



class exitL implements ActionListener //退出程序的监听类
{ 
public void actionPerformed(ActionEvent e) 
{ 
System.exit(0); 
} 
} 

class copyL implements ActionListener   //复制的监听类
{ 
public void actionPerformed(ActionEvent e) 
{ 
String selection = jta.getSelectedText();  
StringSelection clipString = new StringSelection(selection); 
clipbd.setContents(clipString, clipString); 
} 
} 

class cutL implements ActionListener   //剪切的监听类
{ 
public void actionPerformed(ActionEvent e) 
{ 
String selection = jta.getSelectedText(); 
StringSelection clipString = new StringSelection(selection); 
clipbd.setContents(clipString, clipString); 
jta.replaceRange("", jta.getSelectionStart(), 
jta.getSelectionEnd()); 
} 
} 

class pasteL implements ActionListener  //粘贴的监听类
{ 
public void actionPerformed(ActionEvent e) 
{ 
Transferable clipData = clipbd.getContents(JNotepad.this); 
try 
{ 
String clipString = 
(String)clipData.getTransferData( 
DataFlavor.stringFlavor); 
jta.replaceRange(clipString, 
jta.getSelectionStart(), jta.getSelectionEnd()); 
} 
catch(Exception ex) 
{ 
} 
} 
} 

class deleteL implements ActionListener   //删除的监听类
{ 
public void actionPerformed(ActionEvent e) 
{ 
String selection = jta.getSelectedText(); 
jta.replaceRange("", jta.getSelectionStart(), 
jta.getSelectionEnd()); 
} 
} 

class selectAllL implements ActionListener   //全选的监听类
{ 
public void actionPerformed(ActionEvent e) 
{ 
jta.selectAll(); 
} 
} 

class findL implements ActionListener  //查找窗口的监听类
{ 
public void actionPerformed(ActionEvent e) 
{ 
String find = ""; 
find = JOptionPane.showInputDialog( 
"Find what: "); 
} 
} 

class findNextL implements ActionListener  //查找下一个的监听类
{ 
public void actionPerformed(ActionEvent e) 
{ 
} 
} 

class aboutL implements ActionListener   //关于的监听类
{ 
public void actionPerformed(ActionEvent e) 
{ 
JOptionPane.showMessageDialog(null, 
"JNotepad 1.0 开发者:曾兆明");  //版权
} 
} 

class jtaL implements ActionListener   //文本区域的监听类
{ 
public void actionPerformed(ActionEvent e) 
{ 
} 
} 

file.add(jmi = new JMenuItem("新建", KeyEvent.VK_N)); //file菜单中加入子菜单
jmi.addActionListener(new newL());                   //设置子菜单的监听者
file.add(jmi = new JMenuItem("打开", KeyEvent.VK_O)); 
jmi.addActionListener(new openL()); 
file.add(jmi = new JMenuItem("另存为", KeyEvent.VK_S)); 
jmi.addActionListener(new saveAs());
file.add(jmi = new JMenuItem("保存",KeyEvent.VK_M));   
file.addSeparator(); 
file.add(jmi = new JMenuItem("退出", KeyEvent.VK_X)); 
jmi.addActionListener(new exitL()); 

edit.add(jmi = new JMenuItem("复制", KeyEvent.VK_C)); //edit菜单中加入子菜单
jmi.addActionListener(new copyL());                   //设置子菜单的监听者
edit.add(jmi = new JMenuItem("剪切", KeyEvent.VK_T)); 
jmi.addActionListener(new cutL()); 
edit.add(jmi = new JMenuItem("粘贴", KeyEvent.VK_P)); 
jmi.addActionListener(new pasteL()); 
edit.add(jmi = new JMenuItem("删除", KeyEvent.VK_D)); 
jmi.addActionListener(new deleteL()); 
edit.addSeparator(); 
edit.add(jmi = new JMenuItem("全选", KeyEvent.VK_A)); 
jmi.addActionListener(new selectAllL()); 

search.add(jmi = new JMenuItem("查找", KeyEvent.VK_F)); //search菜单中加入子菜单
jmi.addActionListener(new findL());                     //设置子菜单的监听者
search.add(jmi = new JMenuItem("查找下一个", KeyEvent.VK_N)); 
jmi.addActionListener(new findNextL()); 

help.add(jmi = new JMenuItem("关于", KeyEvent.VK_A)); //help菜单中加入子菜单
jmi.addActionListener(new aboutL());                   //设置监听者

setLayout(new BorderLayout());      //设置布局方式
file.setMnemonic(KeyEvent.VK_F); 
jmb.add(file);                     //菜单栏中加入file菜单
edit.setMnemonic(KeyEvent.VK_E); 
jmb.add(edit); 
search.setMnemonic(KeyEvent.VK_S); 
jmb.add(search); 
jmb.add(Box.createHorizontalGlue()); 
help.setMnemonic(KeyEvent.VK_H); 
jmb.add(help); 

toolBar.setFloatable(true); //设置工具栏为用户可拖动 
addButtons(toolBar);   //向工具栏中加入按钮 

add(jmb, BorderLayout.NORTH); 
add(toolBar, BorderLayout.CENTER); 
add(jsp, BorderLayout.SOUTH); 

jta.getCaret().setVisible(true); //显示文本区域
jta.setCaretPosition(0); 
} 

public static void main(String args[])   //程序的主函数
{ 
JFrame f = new JFrame();     //定义窗口类
JNotepad applet = new JNotepad();  //创建程序对象
f.setTitle("JNotepad");   //设置窗口标题
f.setBackground(Color.red);  //设置窗口的背景颜色
f.getContentPane().add(applet, BorderLayout.CENTER); 
f.addWindowListener(new appCloseL()); //添加关闭的监听者
f.setSize(1000, 800);  //设置窗口大小
f.setVisible(true); //显示窗口
f.pack(); 
} 

protected void addButtons(JToolBar toolBar)  //添加按钮
{ 
JButton button = new JButton(new ImageIcon("images/new.gif")); //定义按钮并设置图标
button.setToolTipText("新建");  //加入提示文本:新建文件的按钮
button.addActionListener(new ActionListener()  //设置监听者
{ 
public void actionPerformed(ActionEvent e) 
{ 
jta.setDocument(new PlainDocument()); 
} 
}); 
toolBar.add(button);   //往工具栏中加入按钮

JButton button1 = new JButton(new ImageIcon("images/open.gif")); 
button1.setToolTipText("打开");   //打开文件的按钮
button1.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 
JFileChooser fc = new JFileChooser(); 
int returnVal = fc.showDialog(JNotepad.this, "Open file"); 
if(returnVal == JFileChooser.APPROVE_OPTION) 
{ 
String file = fc.getSelectedFile().getPath(); 
if(file == null) 
{ 
return; 
} 
try 
{ 
Reader in = new FileReader(file); 
char[] buff = new char[4096]; 
int nch; 
while((nch = in.read(buff, 0, buff.length)) != -1) 
{ 
jta.setDocument(new PlainDocument()); 
jta.append(new String(buff, 0, nch)); 
} 
} 
catch (IOException io) 
{ 
System.err.println("IOException: " + io.getMessage()); 
} 
} 
else 
{ 
return; 
} 
} 
}); 
toolBar.add(button1); 

JButton button2 = new JButton(new ImageIcon("images/save.gif")); 
button2.setToolTipText("另存为");   //另存为的按钮 
button2.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 
JFileChooser fc = new JFileChooser(); 
int returnVal = fc.showSaveDialog(JNotepad.this); 
if(returnVal == JFileChooser.APPROVE_OPTION) 
{ 
String savefile = fc.getSelectedFile().getPath(); 
if(savefile == null) 
{ 
return; 
} 
else 
{ 
String docToSave = jta.getText(); 
if(docToSave != null) 
{ 
FileOutputStream fstrm = null; 
BufferedOutputStream ostrm = null; 
try 
{ 
fstrm = new FileOutputStream(savefile); 
ostrm = new BufferedOutputStream(fstrm); 
byte[] bytes = null; 
try 
{ 
bytes = docToSave.getBytes(); 
} 
catch(Exception e1) 
{ 
e1.printStackTrace(); 
} 
ostrm.write(bytes); 
} 
catch(IOException io) 
{ 
System.err.println("IOException: " + 
io.getMessage()); 
} 
finally 
{ 
try 
{ 
ostrm.flush(); 
fstrm.close(); 
ostrm.close(); 
} 
catch(IOException ioe) 
{ 
System.err.println("IOException: " + 
ioe.getMessage()); 
} 
} 
} 
} 
} 
else 
{ 
return; 
}
} 
}); 
toolBar.add(button2); 

JButton button2$ = new JButton(new ImageIcon("images/save.gif")); 
button2$.setToolTipText("保存");
toolBar.add(button2$); 
JButton button3 = new JButton(new ImageIcon("images/copy.gif")); 
button3.setToolTipText("复制");    //复制文本的按钮
button3.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 
String selection = jta.getSelectedText(); 
StringSelection clipString = new StringSelection(selection); 
clipbd.setContents(clipString, clipString); 
} 
}); 
toolBar.add(button3); 

JButton button4 = new JButton(new ImageIcon("images/cut.gif")); 
button4.setToolTipText("删除");   //删除文本的按钮
button4.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 
String selection = jta.getSelectedText(); 
StringSelection clipString = new StringSelection(selection); 
clipbd.setContents(clipString, clipString); 
jta.replaceRange("", jta.getSelectionStart(), 
jta.getSelectionEnd()); 
} 
}); 
toolBar.add(button4); 

JButton button5 = new JButton(new ImageIcon("images/paste.gif")); 
button5.setToolTipText("粘贴");      //粘贴文本的按钮 
button5.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 
Transferable clipData = clipbd.getContents(JNotepad.this); 
try 
{ 
String clipString = 
(String)clipData.getTransferData( 
DataFlavor.stringFlavor); 
jta.replaceRange(clipString, 
jta.getSelectionStart(), jta.getSelectionEnd()); 
} 
catch(Exception ex) 
{ 
} 
} 
}); 
toolBar.add(button5); 

JButton button6 = new JButton(new ImageIcon("images/about.gif")); 
button6.setToolTipText("程序版本");     //程序版本信息的按钮
button6.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 
JOptionPane.showMessageDialog(null, 
"JNotepad 1.0 开发者:曾兆明"); 
} 
}); 
toolBar.add(button6); 
} 

protected static final class appCloseL extends WindowAdapter //定义关闭窗口的适配器类
{ 
public void windowClosing(WindowEvent e) 
{ 
System.exit(0); 
} 
} 
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -