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

📄 jishiben.java

📁 自己用java做的一个仿windows的记事本 希望各位前辈多加指点
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		
		shifoukeyong();
	}
	
	public static void main(String [] a){
		JiShiBen jsb = new JiShiBen();
		jsb.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == exit) {
			int alert = 0;
			if (area.getText().trim().length() > 0) {
				alert = JOptionPane.showConfirmDialog(null, "文件没保存,确定要退出吗?",
						"警告", JOptionPane.YES_NO_OPTION);
				if (alert == JOptionPane.NO_OPTION) {
					sfd.setVisible(true);
					String fname = sfd.getDirectory() + sfd.getFile();
					try {
						FileOutputStream fs = new FileOutputStream(fname);
						fs.write(area.getText().getBytes());
						fs.close();
					} catch (IOException ioe) {
					}
				}
			}
			frm.setVisible(false);
			System.exit(0);
		}else if (e.getSource() == open) {
			ofd.setVisible(true);
			String fname = ofd.getDirectory() + ofd.getFile();
			try {
				FileInputStream fi = new FileInputStream(fname);
				byte fc[] = new byte[fi.available()];
				fi.read(fc);
				area.setText(new String(fc));
				fi.close();
			} catch (IOException ioe) {}
			shifoukeyong();
		} else if (e.getSource() == save) {
			sfd.setVisible(true);
			String fname = sfd.getDirectory() + sfd.getFile();
			try {
				FileOutputStream fs = new FileOutputStream(fname);
				fs.write(area.getText().getBytes());
				fs.close();
			} catch (IOException ioe) {
			}
		}else if(e.getSource() == huanhang){
			if(huanhang.getText().charAt(0) == '√'){
				area.setLineWrap(false);//设置自动换行
				huanhang.setText("自动换行");
			}else if(huanhang.getText().charAt(0) != '√'){
				area.setLineWrap(true);
				huanhang.setText("√自动换行");
			}
		}if(e.getSource()==ziti){
			if(isfont==false){
				s1.setVisible(true);
				area.setFont(s1.font2);
				isfont = true;
			}else{
				s1.setVisible(true);
			}
		}if(e.getSource()==tihuan){
			TihuanDialog.setVisible(true);
		}if(e.getSource()==searchok){
			this.serch();
		}
		if(e.getSource()==searchcancel){
			TihuanDialog.setVisible(false);
		}
		if(e.getSource()==changeok){
			this.change();
		}
		if(e.getSource()==changeall){
			this.changeall();
		}if(e.getSource() == about){
			JOptionPane.showMessageDialog(this, "软件技术7班  张帅");
		}if(e.getSource() == time){
			area.setText(area.getText() +new Date().toString());
		}if(e.getSource() == zhuangtai){
			if(zhuangtai.getText().charAt(0) == '√'){
				setBar.setVisible(false);
				zhuangtai.setText("状态栏");
			}else if(zhuangtai.getText().charAt(0) != '√'){
				setBar.setVisible(true);
				zhuangtai.setText("√状态栏");
			}
		}if(e.getSource() == zhuandao){
			String locline = JOptionPane.showInputDialog("请输入行数:");
			if (locline == null)
				locline = "0";// 消除String为空时转换为int的异常
			if (Integer.parseInt(locline) > area.getLineCount()) {
				JOptionPane.showMessageDialog(null, "超出了文件范围!", "定位到行",
						JOptionPane.INFORMATION_MESSAGE);
			} else {
				int index = 0;
				int count = 1;
				while (count < Integer.parseInt(locline)) {
					index = area.getText().indexOf('\n', index);
					index++;
					count++;
				}
				area.setSelectionStart(index);
				if (Integer.parseInt(locline) == area.getLineCount()) {
					area.setSelectionEnd(area.getText().length());
				} else
					area.setSelectionEnd(area.getText().indexOf('\n', index));
				area.setSelectedTextColor(Color.blue);
			}
		}if(e.getSource() == all){
			area.selectAll();
		}if(e.getSource() == jianqie){
			area.cut();
		}if(e.getSource() == fuzhi){
			area.copy();
		}if(e.getSource() == zhantie){
			area.paste();
		}if(e.getSource() == shanchu){
			area.replaceSelection("");
		}
	}
	
    // 处理点击窗体右上角关闭按钮事件的方法,
	protected void processWindowEvent(WindowEvent e) {
		if (e.getID() == WindowEvent.WINDOW_CLOSING) {
			int alert = 0;
			if (area.getText().trim().length() > 0) {
				alert = JOptionPane.showConfirmDialog(null, "文件没保存,确定要退出吗?",
						"警告", JOptionPane.YES_NO_OPTION);
				if (alert == JOptionPane.NO_OPTION) {
					sfd.setVisible(true);
					String fname = sfd.getDirectory() + sfd.getFile();
					try {
						FileOutputStream fs = new FileOutputStream(fname);
						fs.write(area.getText().getBytes());
						fs.close();
					} catch (IOException ioe) {
					}
				}
			}
			frm.setVisible(true);
			System.exit(0);
		}
	}
	
	public void shifoukeyong(){
		if(area.getText().equals("")){
			chexiao.setEnabled(false);
			jianqie.setEnabled(false);
			fuzhi.setEnabled(false);
			shanchu.setEnabled(false);
			tihuan.setEnabled(false);
			zhuandao.setEnabled(false);
		}else{
			chexiao.setEnabled(true);
			jianqie.setEnabled(true);
			fuzhi.setEnabled(true);
			shanchu.setEnabled(true);
			tihuan.setEnabled(true);
			zhuandao.setEnabled(true);
		}
	}
	
	public void serch(){
		String needsearch=searchtext.getText();
		String main=area.getText();
		String temp;
		if(needsearch.equals("")){
			JOptionPane.showMessageDialog(this,"关键字为空!","警告",JOptionPane.WARNING_MESSAGE);
		}
		else if(needsearch.length()>main.length()){
			JOptionPane.showMessageDialog(this,"没有找到关键字","提示",JOptionPane.INFORMATION_MESSAGE);
		}
		else{
			int position=area.getSelectionEnd()-needsearch.length()+1;
			if(position>=main.length()-needsearch.length() ||position<0){
				position=0;
			}
			for(;position<=main.length()-needsearch.length();position++){
				temp=main.substring(position,position+needsearch.length());
				if(temp.equals(needsearch)){
					area.setSelectionStart(position);
					area.setSelectionEnd(position+needsearch.length());
					break;
				}
				else if(position>=main.length()-needsearch.length()){
					area.setSelectionStart(0);
					area.setSelectionEnd(0);
					JOptionPane.showMessageDialog(this,"没有找到关键字","提示",JOptionPane.INFORMATION_MESSAGE);
					break;
				}
			}
		}
	}
	public void change(){
		String changeword=changetext.getText();
		if(area.getSelectionStart()!=area.getSelectionEnd()){
			area.replaceRange(changeword,area.getSelectionStart(),area.getSelectionEnd());
		}
		else if(area.getSelectionStart()==area.getSelectionEnd()){
			JOptionPane.showMessageDialog(this,"先搜索再替换","警告",JOptionPane.WARNING_MESSAGE);
		}
	}
	
	public void keyPressed(KeyEvent e) {
		shifoukeyong();
	}

	public void keyReleased(KeyEvent e) {
		shifoukeyong();
		gbwz();
	}

	public void keyTyped(KeyEvent e) {
		// TODO 自动生成方法存根
		shifoukeyong();
	}
	
	public void changeall(){
		String needsearch=searchtext.getText();
		String changeword=changetext.getText();
		String main=area.getText();
		String temp;
		for(int i=0;i<=main.length()-needsearch.length();i++){
			temp=main.substring(i,i+needsearch.length());
			if(temp.equals(needsearch)){
				area.setSelectionStart(i);
				area.setSelectionEnd(i+needsearch.length());
				area.replaceRange(changeword,area.getSelectionStart(),area.getSelectionEnd());
				main=area.getText();
			}
		}
	}
	
	public void gbwz(){
		if(!area.getText().equals("")&&area.getCaretPosition()!=0){
			int i_xy = area.getCaretPosition();	
			String s_all = area.getText().substring(0,i_xy);
			String[] array = s_all.split("\n");
			int i_ln = 1,i_col = 1;
			for(int i=0;i<array.length-1;i++){
				if(i_xy>=array[i].length()){
					i_ln++;
					i_xy = i_xy - array[i].length()-1;
				}
			}
			i_col = i_xy;
			setBar.setText("ln: "+i_ln+" , Col: "+i_col);
		}
	}

	public void mouseClicked(MouseEvent arg0){}

	public void mouseEntered(MouseEvent arg0){}

	public void mouseExited(MouseEvent arg0){}

	public void mousePressed(MouseEvent arg0){}

	public void mouseReleased(MouseEvent arg0){}
}

⌨️ 快捷键说明

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