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

📄 notepad.java

📁 编译原理的课程设计
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
						if(sourceStr.equals(""))
						{
							jta.setText(textTemp.toUpperCase());						
						}
						else
						{
							sourceStr = sourceStr.toUpperCase();
							textTemp = textTemp.substring(0, selectStart) +
									sourceStr + textTemp.substring(selectEnd);
							jta.setText(textTemp);
						}
					}
					catch(Exception ex)
					{
						JOptionPane.showMessageDialog(popToUpItem, 
						ex.toString() + ":\n输入有误!", "出错啦!",
						JOptionPane.INFORMATION_MESSAGE);
					}
				}
			});
		//转换为小写弹出菜单项
		popToLowItem.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					String textTemp = jta.getText();
					int selectStart = jta.getSelectionStart();
					int selectEnd = jta.getSelectionEnd();
					String sourceStr = textTemp.substring(selectStart, selectEnd);
					try
					{
						if(sourceStr.equals(""))
						{
							jta.setText(textTemp.toLowerCase());						
						}
						else
						{
							sourceStr = sourceStr.toLowerCase();
							textTemp = textTemp.substring(0, selectStart) +
									sourceStr + textTemp.substring(selectEnd);
							jta.setText(textTemp);
						}
					}
					catch(Exception ex)
					{
						JOptionPane.showMessageDialog(popToLowItem, 
						ex.toString() + ":\n输入有误!", "出错啦!",
						JOptionPane.INFORMATION_MESSAGE);
					}
				}
			});
		//大小写互换弹出菜单项
		popUpLowItem.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					String textTemp = jta.getText();
					StringBuffer temp = new StringBuffer();
					int selectStart = jta.getSelectionStart();
					int selectEnd = jta.getSelectionEnd();
					final int LEN = 10;
					int loop = 0;
					int last = 0;
					int length = 0;
					String sourceStr = textTemp.substring(selectStart, selectEnd);
					try
					{
						if(sourceStr.equals(""))
						{
							length = textTemp.length();
							loop = length / LEN;
							last = length % LEN;
							for(int i = 1; i <= loop; i++)
							{
								sourceStr = textTemp.substring((i-1)*LEN, i*LEN);
								sourceStr = Notepad.this.upLowSwitch(sourceStr, LEN);								
								temp = temp.append(sourceStr);
								
							}
							sourceStr = textTemp.substring(loop * LEN);
							sourceStr = Notepad.this.upLowSwitch(sourceStr, last);
							temp = temp.append(sourceStr);
							temp = temp.append(sourceStr);
							jta.setText(temp.toString());					
						}
						else
						{
							String sourceTemp = new String(sourceStr);
							length = sourceStr.length();
							loop = length / LEN;
							last = length % LEN;
							for(int i = 1; i <= loop; i++)
							{
								sourceStr = sourceTemp.substring((i-1)*LEN, i*LEN);
								sourceStr = Notepad.this.upLowSwitch(sourceStr, LEN);								
								temp = temp.append(sourceStr);
								
							}
							sourceStr = sourceTemp.substring(loop * LEN);
							sourceStr = Notepad.this.upLowSwitch(sourceStr, last);
							temp = temp.append(sourceStr);
							temp = new StringBuffer(textTemp.substring(0, selectStart)).append(
									temp).append(textTemp.substring(selectEnd));
							jta.setText(temp.toString());
						}
					}
					catch(Exception ex)
					{
						JOptionPane.showMessageDialog(popUpLowItem, 
						ex.toString() + ":\n输入有误!", "出错啦!",
						JOptionPane.INFORMATION_MESSAGE);
					}
				}
			});
		//定位弹出才当项
		popPosItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				try
				{
					int start0 = 0;
					String textTemp = jta.getText();
					int index = Integer.parseInt(JOptionPane.showInputDialog(popPosItem, 
					"定位到行数:", "定位到行", JOptionPane.PLAIN_MESSAGE));
					for(int i = 1; i < index; i++)
					{
						start0 = textTemp.indexOf("\n", start0) + 1;
						if(start0 == 0)
							throw new Exception("行数越界:");
					}
					jta.select(start0, textTemp.indexOf("\n", start0));					
				}
				catch(Exception ex)
				{
					JOptionPane.showMessageDialog(popPosItem, 
					ex.toString() + ":\n输入有误!", "出错啦!",
					JOptionPane.INFORMATION_MESSAGE);
				}
			}
		});
		//保存文件弹出菜单项
		popSaveItem.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					String str = jta.getText();
					BufferedWriter bw = null;
					if((fileName == null) || (fileDir == null))
					{
						FileDialog fdg = new FileDialog(Notepad.this, "保存", FileDialog.SAVE);
						fdg.setVisible(true);
						fileDir = fdg.getDirectory();
						fileName = fdg.getFile();
						try
						{
							bw = new BufferedWriter(new FileWriter(new File(fileDir, fileName)));
							bw.write(str);
							bw.close();
							Notepad.this.setTitle("CoffeeEditor " + "[" +
							fileDir + fileName + "]");
						}
						catch(Exception ex)
						{
							JOptionPane.showMessageDialog(popSaveItem, 
							ex.toString() + ":\n写入文件出错!", 
							"出错啦!", JOptionPane.INFORMATION_MESSAGE);
						}
					}
					else
					{
						try
						{
							bw = new BufferedWriter(new FileWriter(new File(fileDir, fileName)));
							bw.write(str);
							bw.close();
						}
						catch(Exception ex)
						{
							JOptionPane.showMessageDialog(popSaveItem, 
							ex.toString() + ":\n写入文件出错!", 
							"出错啦!", JOptionPane.INFORMATION_MESSAGE);
						}
					}
				}
			});
		//打开文件弹出菜单项
		popOpenItem.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					FileDialog fdg = new FileDialog(Notepad.this, "打开", FileDialog.LOAD);
					FileDialog fdgSave = new FileDialog(Notepad.this, "保存", FileDialog.SAVE);
					BufferedWriter bw = null;
					StringBuffer textStrb = new StringBuffer();
					BufferedReader br = null;
					String str = jta.getText();
					if(!str.equals(lastContent))
					{
						int message = JOptionPane.showConfirmDialog(popOpenItem,
						"是否保存文件?","保存",
						JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
						if(message == 0)
						{
							if((fileName == null) || (fileDir == null))
							{
								fdgSave.setVisible(true);
								fileDir = fdgSave.getDirectory();
								fileName = fdgSave.getFile();
								try
								{
									bw = new BufferedWriter(new FileWriter(new File(fileDir, fileName)));
									bw.write(str);
									bw.close();
									Notepad.this.setTitle("CoffeeEditor " + "[" +
										fileDir + fileName + "]");
								}
								catch(Exception ex)
								{
									JOptionPane.showMessageDialog(popOpenItem, 
									ex.toString() + ":\n写入文件出错!", 
									"出错啦!", JOptionPane.INFORMATION_MESSAGE);
									Notepad.this.setTitle("CoffeeEditor " + "[" +
										fileDir + fileName + "]");
								}
							}
							else
							{
								try
								{
									bw = new BufferedWriter(new FileWriter(new File(fileDir, fileName)));
									bw.write(str);
									bw.close();
								}
								catch(Exception ex)
								{
									JOptionPane.showMessageDialog(popOpenItem, 
									ex.toString() + ":\n写入文件出错!", 
									"出错啦!", JOptionPane.INFORMATION_MESSAGE);
								}
							}
						}
						else if(message == 2)//重新回到编辑窗口
						{
							return;
						}
					}
					try
					{
						fdg.setVisible(true);
						fileDir = fdg.getDirectory();
						fileName = fdg.getFile();
						br = new BufferedReader(new FileReader(new File(fileDir, fileName)));
						jta.setText("");
						while((str = br.readLine()) != null)
						{
							textStrb = textStrb.append(str).append("\n");
						}
						jta.setText(textStrb.toString());
						lastContent = jta.getText();
						br.close();
						Notepad.this.setTitle("CoffeeEditor " + "[" +
							fileDir + fileName + "]");
					}
					catch(Exception ex)
					{
						JOptionPane.showMessageDialog(popOpenItem, 
						ex.toString() + ":\n读取文件出错!", 
						"出错啦!", JOptionPane.INFORMATION_MESSAGE);
					}
				}
			});
		//新建文件弹出菜单项
		popNewItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{			
				FileDialog fdgNew = new FileDialog(Notepad.this, "新建", FileDialog.SAVE);
				FileDialog fdg = new FileDialog(Notepad.this, "保存", FileDialog.SAVE);				
				String str = jta.getText();
				BufferedWriter bw = null;
				if(!str.equals(lastContent))
				{
					int message = JOptionPane.showConfirmDialog(popNewItem,
					"是否保存文件?","保存",
					JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
					if(message == 0)
					{
						if((fileName == null) || (fileDir == null))
						{
							fdg.setVisible(true);
							fileDir = fdg.getDirectory();
							fileName = fdg.getFile();
							try
							{
								bw = new BufferedWriter(new FileWriter(new File(fileDir, fileName)));
								bw.write(str);
								bw.close();
							}
							catch(Exception ex)
							{
								JOptionPane.showMessageDialog(popNewItem, 
								ex.toString() + ":\n写入文件出错!", 
								"出错啦!", JOptionPane.INFORMATION_MESSAGE);
							}
						}
						else
						{
							try
							{
								bw = new BufferedWriter(new FileWriter(new File(fileDir, fileName)));
								bw.write(str);
								bw.close();
							}
							catch(Exception ex)
							{
								JOptionPane.showMessageDialog(popNewItem, 
								ex.toString() + ":\n写入文件出错!", 
								"出错啦!", JOptionPane.INFORMATION_MESSAGE);
							}
						}
					}
					else if(message == 2)//重新回到编辑窗口
					{
						return;
					}
				}
				//创建新文件
				fdgNew.setVisible(true);
				fileDir = fdgNew.getDirectory();
				fileName = fdgNew.getFile();
				try
				{
					bw = new BufferedWriter(new FileWriter(new File(fileDir, fileName)));
					jta.setText("");
					bw.close();
					Notepad.this.setTitle("CoffeeEditor " + "[" +
							fileDir + fileName + "]");
				}
				catch(Exception ex)
				{
					JOptionPane.showMessageDialog(popNewItem, 
					ex.toString() + ":\n新建文件出错!", 
					"出错啦!", JOptionPane.INFORMATION_MESSAGE);
				}					
			}
		});	
		//编辑区获取弹出菜单		
		jta.addMouseListener(new MouseAdapter()
			{
				public void mouseReleased(MouseEvent e)
				{
					if((e.getButton() == MouseEvent.BUTTON3) && e.isPopupTrigger())
					{
						popMenu.show(e.getComponent(), e.getX(), e.getY());
					}
				}
			});
		//控制台获取弹出菜单
		csta.addMouseListener(new MouseAdapter()
			{
				public void mouseReleased(MouseEvent e)
				{
					if((e.getButton() == MouseEvent.BUTTON3) && e.isPopupTrigger())
					{
						conPopMenu.show(e.getComponent(), e.getX(), e.getY());
					}
				}
			});
		//清屏弹出菜单项
		clsItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				csta.setText("");
			}
		});	
		//控制台保存
		fileItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				String conContent = csta.getText();
				if(conContent.equals(""))
				{
					JOptionPane.showMessageDialog(fileItem, 
						"没有要保存的内容!", "提示", JOptionPane.INFORMATION_MESSAGE);
				}
				else
				{
					FileDialog fdg = new FileDialog(Notepad.this, "保存", FileDialog.SAVE);
					fdg.setVisible(true);
					String dir = fdg.getDirectory();
					String file = fdg.getFile();
					try
					{
						BufferedWriter bw = new BufferedWriter(new FileWriter(new File(dir, file)));
						bw.write(csta.getText());
						bw.close();
					}
					catch(Exception ex)
					{
						JOptionPane.showMessageDialog(Notepad.this, 
							ex.toString() + ":\n写入文件出错!", 
							"出错啦!", JOptionPane.INFORMATION_MESSAGE);
						System.exit(0);
					}
				}
			}
		});	
		popMenu.add(popCopyItem);
		popMenu.add(popPasteItem);
		popMenu.add(popCutItem);
		popMenu.add(popDelItem);
		popChangeMenu.add(popToLowItem);
		popChangeMenu.add(popToUpItem);
		popChangeMenu.add(popUpLowItem);
		popMenu.add(popChangeMenu);
		popMenu.add(popSelectItem);
		popMenu.add(popFindItem);
		popMenu.add(popPosItem);
		popSubMenu.add(popSubSingleItem);
		popSubMenu.add(popSubWholeItem);
		popMenu.add(popSubMenu);
		popMenu.add(popSaveItem);
		popMenu.add(popOpenItem);
		popMenu.add(popNewItem);
		conPopMenu.add(fileItem);
		conPopMenu.add(clsItem);		
  		JScrollPane jScrollPane1 = new JScrollPane();  
  		JScrollPane jScrollPane2 = new JScrollPane();
  		jScrollPane1.getViewport().add(jta, null);
  		jScrollPane2.getViewport().add(csta, null);
  		JSplitPane jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, jScrollPane1, jScrollPane2);
  		contentPane.add(popMenu);
  		contentPane.add(conPopMenu);
  		setJMenuBar(menuBar);
		contentPane.add(jsp);
	}
	//启动程序
	public static void main(String[] args)
	{
		Notepad notepad = new Notepad();
		notepad.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);		
		notepad.setVisible(true);		
	}
}

⌨️ 快捷键说明

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