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

📄 notepad.java

📁 基于原本对该课题的了解
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
				//粘贴			else if(e.getSource()==editMenu_Paste||e.getSource()==popupMenu_Paste)			{				editArea.requestFocus();				Transferable contents=clipBoard.getContents(this);		        if(contents==null) return;		        String text;		        text="";		        		        try{		            text=(String)contents.getTransferData(DataFlavor.stringFlavor);		        }		        catch(Exception exception){		        }		        		       	editArea.replaceRange(text,editArea.getSelectionStart(),editArea.getSelectionEnd());//		       			}				//删除		else if(e.getSource()==editMenu_Delete||e.getSource()==popupMenu_Delete)			{				editArea.requestFocus();				editArea.replaceRange("",editArea.getSelectionStart(),editArea.getSelectionEnd());//							}				//查找		else if(e.getSource()==editMenu_Find)			{				editArea.requestFocus();				{					editArea.requestFocus();					editArea.setCaretPosition(0);				}				mySearch();			}				//查找下一个(此功能尚未能很好实现,所以就先用查找功能来代替)			else if(e.getSource()==editMenu_FindNext)			{				mySearch();			}					//替换(与查找功能集成在一起了)		else if(e.getSource()==editMenu_Replace)		{			mySearch();		}				//转到			else if(e.getSource()==editMenu_GoTo)			{				final JDialog gotoDialog=new JDialog(this,"转到下列行");				JLabel gotoLabel=new JLabel("行数(L):");				final JTextField linenum=new JTextField(20);				linenum.setText("1");				linenum.selectAll();								JButton okButton=new JButton("确定");				okButton.addActionListener(new ActionListener(){										public void actionPerformed(ActionEvent e){						int totalLine = editArea.getLineCount(); 						int[] lineNumber = new int[totalLine + 1]; 						String s = editArea.getText(); 						int pos = 0, t = 0; 												while(true) { 							pos = s.indexOf('\12', pos); 							if(pos == -1) 							break; 							lineNumber[t++] = pos++; 						} 												int gt = 1; 						try { 							gt = Integer.parseInt(linenum.getText()); 						}						catch(NumberFormatException efe) { 							JOptionPane.showMessageDialog(null, "请输入行数!", "提示",JOptionPane.WARNING_MESSAGE); 							linenum.requestFocus(true);							return;						} 												if(gt < 2 || gt >= totalLine) { 							if(gt < 2) 								editArea.setCaretPosition(0); 							else 								editArea.setCaretPosition(s.length()); 						} 						else 							editArea.setCaretPosition(lineNumber[gt-2] + 1); 												gotoDialog.dispose(); 					} 									});								JButton cancelButton=new JButton("取消");				cancelButton.addActionListener(new ActionListener(){					public void actionPerformed(ActionEvent e){						gotoDialog.dispose();					}				});								Container con=gotoDialog.getContentPane();				con.setLayout(new FlowLayout());				con.add(gotoLabel);				con.add(linenum);				con.add(okButton);				con.add(cancelButton);								gotoDialog.setSize(200,100);				gotoDialog.setResizable(false);				gotoDialog.setLocation(300,280);				gotoDialog.setVisible(true);							}//"转到"处理结束				//插入时间日期			else if(e.getSource()==editMenu_TimeDate)			{				editArea.requestFocus();				SimpleDateFormat currentDateTime = new SimpleDateFormat("HH:mm yyyy-MM-dd"); 				editArea.insert(currentDateTime.format(new Date()),editArea.getCaretPosition());			}				//全选			else if(e.getSource()==popupMenu_SelectAll||e.getSource()==editMenu_SelectAll)			{				editArea.selectAll();			}					//自动换行		else if(e.getSource()==formatMenu_LineWrap)			{				if(formatMenu_LineWrap.getState()) 				{					editArea.setLineWrap(true);				} 				else					editArea.setLineWrap(false);			}				//字体设置		else if(e.getSource()==formatMenu_Font)			{				editArea.requestFocus();				new MyFont();						}				//设置字体颜色(前景色)		else if(e.getSource()==formatMenu_Color_FgColor)			{				editArea.requestFocus();				Color color=JColorChooser.showDialog(this,"更改字体颜色",Color.black);				if(color!=null)				{					editArea.setForeground(color);				} 				else					return;			}				//设置编辑区背景颜色		else if(e.getSource()==formatMenu_Color_BgColor)			{				editArea.requestFocus();				Color color=JColorChooser.showDialog(this,"更改背景颜色",Color.white);				if(color!=null)				{					editArea.setBackground(color);				} 				else					return;			}								}/*方法actionPerformed()结束*/	 class closeWindow extends WindowAdapter {/*在mainFrame类中定义用于响应关闭窗口事件的类,使它成WindowAdapter的子类*/     public void windowClosing(WindowEvent e){         //实现处理窗口关闭事件的方法windowClosing()        String currentValue = editArea.getText();        boolean isTextChange = (currentValue.equals(oldValue))?false:true;//用于判断当前文本内容是否发生变化        if(isTextChange&&isNewFile){            int i = JOptionPane.showConfirmDialog(Notepad.this,                    "文件的文字已经改变\n"+"想保存文件吗?",                    "文本编辑器",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);            if(i == JOptionPane.YES_OPTION){                editArea.requestFocus();				if(isNewFile){									String str=null;										JFileChooser fileChooser=new JFileChooser();					fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);					fileChooser.setApproveButtonText("确定");					fileChooser.setDialogTitle("另存为");										int result=fileChooser.showSaveDialog(null);										if(result==JFileChooser.CANCEL_OPTION){						statusLabel.setText(" 您没有选择任何文件");						return;					}											File saveFileName=fileChooser.getSelectedFile();									if(saveFileName==null||saveFileName.getName().equals(""))						JOptionPane.showMessageDialog(null,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE);					else {						try{							FileWriter fw=new FileWriter(saveFileName);							BufferedWriter bfw=new BufferedWriter(fw);							bfw.write(editArea.getText(),0,editArea.getText().length());							bfw.flush();							fw.close();														isNewFile=false;							currentFile=saveFileName;							oldValue=editArea.getText();														statusLabel.setText(" 当前打开文件:"+saveFileName.getAbsoluteFile());						}													catch(IOException ioException){											}									}				}				else				{					try{						FileWriter fw=new FileWriter(currentFile);						BufferedWriter bfw=new BufferedWriter(fw);						bfw.write(editArea.getText(),0,editArea.getText().length());						bfw.flush();						fw.close();					}												catch(IOException ioException){										}				}                System.exit(0);            }                             else if(i == JOptionPane.NO_OPTION){                System.exit(0);            }        }        else            System.exit(0);  //退出程序        } }			//main 方法	public static void main(String args[])	{		//设置界面为WindowsLookAndFeel		try 		{ 			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 		}		catch(Exception e) { 		} 			Notepad	frame = new Notepad();		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	}/*main 方法结束*/		//实现了接口UndoableListener的类UndoHandler	class UndoHandler implements UndoableEditListener { 		public void undoableEditHappened(UndoableEditEvent uee){ 		} 	}/*End of class UndoHandler */		//用于设置字体的类MyFont	class MyFont implements ActionListener{		final JDialog fontDialog;		final JTextField tfFont,tfSize,tfStyle;		final int fontStyleConst[]={Font.PLAIN,Font.BOLD,Font.ITALIC,Font.BOLD+Font.ITALIC};		final JList listStyle,listFont,listSize;		JLabel sample;				//构造函数MyFont		public MyFont(){					fontDialog = new JDialog(Notepad.this,"字体设置",true);			Container con=fontDialog.getContentPane();			con.setLayout(new FlowLayout(FlowLayout.LEFT));						Font currentFont=editArea.getFont();						JLabel lblFont=new JLabel("字体(F):");			lblFont.setPreferredSize(new Dimension(100,20));			JLabel lblStyle=new JLabel("字形(Y):");			lblStyle.setPreferredSize(new Dimension(100,20));			JLabel lblSize=new JLabel("大小(S):");			lblSize.setPreferredSize(new Dimension(100,20));			tfFont=new JTextField(15);			tfFont.setText(currentFont.getFontName());			tfFont.selectAll();			tfFont.setPreferredSize(new Dimension(200,20));			tfStyle=new JTextField(15);			if(currentFont.getStyle()==Font.PLAIN)				tfStyle.setText("常规");			else if(currentFont.getStyle()==Font.BOLD)				tfStyle.setText("粗体");			else if(currentFont.getStyle()==Font.ITALIC)				tfStyle.setText("斜体");			else if(currentFont.getStyle()==(Font.BOLD+Font.ITALIC))				tfStyle.setText("粗斜体");							tfFont.selectAll();			tfStyle.setPreferredSize(new Dimension(200,20));			tfSize=new JTextField(8);			tfSize.setText(currentFont.getSize()+"");			tfSize.selectAll();			tfSize.setPreferredSize(new Dimension(200,20));																			final String fontStyle[]={"常规","粗体","斜体","粗斜体"};			listStyle=new JList(fontStyle);									GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();			final String fontName[]=ge.getAvailableFontFamilyNames();			int defaultFontIndex=0;			for(int i=0;i<fontName.length;i++)				{					if(fontName[i].equals(currentFont.getFontName()))						{							defaultFontIndex=i;							break;						}				}			listFont=new JList(fontName);			listFont.setSelectedIndex(defaultFontIndex);						listFont.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);			listFont.setVisibleRowCount(7);			listFont.setFixedCellWidth(82);			listFont.setFixedCellHeight(20);			listFont.addListSelectionListener(				new ListSelectionListener(){					public void valueChanged(ListSelectionEvent event){						tfFont.setText(fontName[listFont.getSelectedIndex()]);						tfFont.requestFocus();						tfFont.selectAll();							updateSample();					}				}				);			listStyle.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);			if(currentFont.getStyle()==Font.PLAIN)				listStyle.setSelectedIndex(0);			else if(currentFont.getStyle()==Font.BOLD)				listStyle.setSelectedIndex(1);			else if(currentFont.getStyle()==Font.ITALIC)				listStyle.setSelectedIndex(2);			else if(currentFont.getStyle()==(Font.BOLD+Font.ITALIC))				listStyle.setSelectedIndex(3);						listStyle.setVisibleRowCount(7);			listStyle.setFixedCellWidth(99);			listStyle.setFixedCellHeight(20);			listStyle.addListSelectionListener(				new ListSelectionListener(){					public void valueChanged(ListSelectionEvent event){						tfStyle.setText(fontStyle[listStyle.getSelectedIndex()]);						tfStyle.requestFocus();						tfStyle.selectAll();						updateSample();						}				}				);									final String fontSize[]={"8","9","10","11","12","14","16","18","20","22","24","26","28","36","48","72"};			listSize=new JList(fontSize);			int defaultFontSizeIndex=0;			for(int i=0;i<fontSize.length;i++)			{				if(fontSize[i].equals(currentFont.getSize()+""))				{					defaultFontSizeIndex=i;					break;				}			}			listSize.setSelectedIndex(defaultFontSizeIndex);						listSize.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);			listSize.setVisibleRowCount(7);			listSize.setFixedCellWidth(39);			listSize.setFixedCellHeight(20);			listSize.addListSelectionListener(				new ListSelectionListener(){					public void valueChanged(ListSelectionEvent event){						tfSize.setText(fontSize[listSize.getSelectedIndex()]);						tfSize.requestFocus();						tfSize.selectAll();							updateSample();					}				}				);			fontOkButton=new JButton("确定");			fontOkButton.addActionListener(this);			JButton cancelButton=new JButton("取消");			cancelButton.addActionListener(new ActionListener(){				public void actionPerformed(ActionEvent e){					fontDialog.dispose();				}			});						sample=new JLabel("欢迎使用 Notepad");			sample.setHorizontalAlignment(SwingConstants.CENTER);			sample.setPreferredSize(new Dimension(300,50));						JPanel samplePanel=new JPanel();			samplePanel.setBorder(BorderFactory.createTitledBorder("示例"));			samplePanel.add(sample);						con.add(lblFont);			con.add(lblStyle);			con.add(lblSize);			con.add(tfFont);			con.add(tfStyle);			con.add(tfSize);			con.add(fontOkButton);			con.add(new JScrollPane(listFont));			con.add(new JScrollPane(listStyle));			con.add(new JScrollPane(listSize));			con.add(cancelButton);			con.add(samplePanel);			updateSample();									fontDialog.setSize(350,340);			fontDialog.setLocation(200,200);			fontDialog.setResizable(false);			fontDialog.setVisible(true);			}//构造函数结束						//更新示例显示的字体和风格大小等			public void updateSample(){				Font sampleFont=new Font(tfFont.getText(),fontStyleConst[listStyle.getSelectedIndex()],Integer.parseInt(tfSize.getText()));				sample.setFont(sampleFont);			}//End method updateSample						//设置文本编辑区的字体			public void actionPerformed(ActionEvent e){				if(e.getSource()==fontOkButton){					Font tempFont=new Font(tfFont.getText(),fontStyleConst[listStyle.getSelectedIndex()],Integer.parseInt(tfSize.getText()));					editArea.setFont(tempFont);					fontDialog.dispose();									}			}//End method actionPerformed	                        	}/*End of class MyFont*/		  } 

⌨️ 快捷键说明

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