java简易编辑器

 https://blog.csdn.net/dongze2/article/details/103654771 这里给出了实现一个编辑器所需要的数据结构。下面采用line span的数据结构来实现一个简易的编辑器。

实现后的编辑器具有的功能如下:1、输入和删除字符 2、正常的换行和鼠标响应 3、正常的上下左右键控制4、保存功能。

很多功能还没有实现,计划在以后实现的包括1、鼠标拖拽选择文本 2、文本的复制粘贴 3、对某一个具体编程语言的高亮,以及语法纠错 4、滚动条 5、对中文的支持

采用了Swing编写程序,在写之前看到好多人说Swing过时了之类的,一度冲动的想学一下javaFX,后来想到了没有什么框架能一直存在,主要的是算法。于是还是用Swing写了。

整体结构很简单,一个JFrame内置一个自定义的JComponent,最终效果:

 没错,我准备做一个verilog编辑器。

首先让程序在屏幕中间显示

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	int screen_width=(int)screenSize.getWidth();
	int screen_height=(int)screenSize.getHeight();
	this.setBounds(screen_width>>2, screen_height>>2, screen_width>>1, screen_height>>1);

所有的文字按照line span结构存在一个linkedlist当中。当打开一个文件时候,我们需要将文件读入

		line_number=new LinkedList<>();
		//if the file is given;
		if(string!=null)
		{
			//read the file to the linkedlist
			file_name=string;
			File f= new File(string);
			String str;
			if(f.exists())
			{
				try {
					 BufferedReader reader=new BufferedReader(new FileReader(f));
					while((str=reader.readLine())!=null) line_number.add(str);
					reader.close();
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}else
			{
				try {
					f.createNewFile();
					line_number.add("");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}else//if is a new page
		{
			line_number.add("");
		}

光标由一个线程控制,每隔一段时间就闪一下,当然,移动光标时候要让他常亮

		Runnable change_cursor_state=new Runnable() {
			
			@Override
			public void run() {
				int i=0;
				while(true)
				{
					try {
						Thread.sleep(1);
						if(lock)
						{
							//if move the cursor,recount the delay time
							lock=false;
							i=0;
						}
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					i++;
					if(i>300)
					{
						//change the state of the cursor
						i=0;
						if(cursor_state) cursor_state=false;
						else cursor_state=true;
						MainComponent.this.repaint();
					}
					
					
					//MainComponent.this.validate();
					
				}
				
			}
		};

编辑器被设计为可以滚动的状态,滚动时的偏移值需要被保存下来,同时光标相对空间的位置也要被记录下来,两者相加可以得到光标相对于文本的绝对位置。由于目前这个编辑器只支持英文,同时字体采用了等宽的宋体,所以控件可通过直接的单击计算出字符的相对位置。相对位置在大多数情况下不变,只有在编辑器大小改变的时候可能改变。每次编辑器大小改变的时候。都要重新计算一些必要的参数

	@Override
	public void invalidate() {
		//System.out.println("invalidate");
		//char length and width
		char_height=getGraphics().getFontMetrics().getHeight();	
		char_width=getGraphics().getFontMetrics().charWidth('w');
		//editor length and width
		bound_rect=getBounds();
		//how many chars can show at height and length
		height_char_number=bound_rect.height/char_height;
		width_char_number=bound_rect.width/char_width;
		super.invalidate();
	}

绘制的时候,根据屏幕的偏移值进行绘制。不在空间内的文本可以不去绘制。

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		g.setColor(Color.BLACK);
		g.fillRect(bound_rect.x, bound_rect.y, bound_rect.width, bound_rect.height);
		//System.out.println("paint");
		g.setFont(new Font("宋体", Font.PLAIN, 18));
		g.setColor(Color.WHITE);
		int y=-show_height;
		int x=-show_width;
		for(Iterator<String> iterator=line_number.iterator();iterator.hasNext();)
		{
			String string=iterator.next();
			y++;
			//if the data is on the screen
			if(y>0)
			{		
				g.drawString(string, x*char_width, y*char_height);
				
			}
		}
		//draw the cursor
		if(cursor_state)
		{
			g.drawLine(char_width*cursor_width, cursor_height*char_height+2, char_width*cursor_width, cursor_height*char_height+char_height+2);
		}

	}
	

然后是对鼠标和键盘的监听,光标的位置是相对位置,鼠标单击的也是相对位置,但是键盘的按键输入需要在绝对位置输入,只要做好了转换,便很简单了

@Override
			public void mouseClicked(MouseEvent e) {
//				System.out.println(e.getX()/char_width+","+e.getY()/char_height);
				//which used to decide the cursor's position and assure cursor not overboard
				int temp_height=e.getY()/char_height+show_height;
				if(temp_height>line_number.size()-1) temp_height=line_number.size()-1;
				int temp_width=e.getX()/char_width+show_width;
				int compare_width=line_number.get(temp_height).length();
				//according absolute position to calculate the relative position
				if(temp_width>compare_width)
				{
					if(compare_width>width_char_number-1)
					{
						cursor_width=width_char_number-1;
						show_width=compare_width-cursor_width;
					}else
					{
						show_width=0;
						cursor_width=compare_width;
					}
				}else
				{
					cursor_width=temp_width-show_width;
				}					
				cursor_height=temp_height-show_height;
				up_down=false;
				cursor_state=true;
				lock=true;
				repaint();
				
			}

最后文件需要保存,根据打开的是新文件还是存在的文件,分别进行不同的操作

public void save_file()
	{
		if(file_name!=null)
		{
			try {
				BufferedWriter writer=new BufferedWriter(new FileWriter(new File(file_name)));
				for(Iterator<String> iterator=line_number.iterator();iterator.hasNext();)
				{
					String string=iterator.next();
					writer.write(string);
					writer.newLine();
				}
				writer.flush();
				writer.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else
		{
			JFileChooser chooser=new JFileChooser();
			int returnVal=chooser.showSaveDialog(new JPanel());
			if(returnVal==JFileChooser.APPROVE_OPTION) {
				file_name=chooser.getSelectedFile().getPath();
				File file=new File(file_name);
				if(file.exists()) file.delete();
				try {
					file.createNewFile();
					BufferedWriter writer=new BufferedWriter(new FileWriter(file));
					for(Iterator<String> iterator=line_number.iterator();iterator.hasNext();)
					{
						String string=iterator.next();
						writer.write(string);
						writer.newLine();
					}
					writer.flush();
					writer.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}

至此,简易的文本编辑器便实现了。

下载地址:https://download.csdn.net/download/dongze2/12047702

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是使用 Java Swing 中的 JFrame 实现简易文件编辑器的示例代码: ```java import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.filechooser.FileNameExtensionFilter; public class SimpleEditor extends JFrame implements ActionListener { private JTextArea textArea; private JButton openButton, saveButton; public SimpleEditor() { // 设置窗口的标题和大小 setTitle("Simple Editor"); setSize(500, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); // 创建文本区域并添加到滚动面板中 textArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textArea); getContentPane().add(scrollPane, BorderLayout.CENTER); // 创建按钮并添加到面板中 openButton = new JButton("Open"); openButton.addActionListener(this); saveButton = new JButton("Save"); saveButton.addActionListener(this); JPanel buttonPanel = new JPanel(); buttonPanel.add(openButton); buttonPanel.add(saveButton); getContentPane().add(buttonPanel, BorderLayout.NORTH); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == openButton) { // 创建文件选择器并设置过滤器 JFileChooser fileChooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("Text Files", "txt"); fileChooser.setFileFilter(filter); // 显示文件选择器并处理用户选择的文件 int result = fileChooser.showOpenDialog(this); if (result == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); try { BufferedReader reader = new BufferedReader(new FileReader(file)); String line; while ((line = reader.readLine()) != null) { textArea.append(line + "\n"); } reader.close(); } catch (Exception ex) { ex.printStackTrace(); } } } else if (e.getSource() == saveButton) { // 创建文件选择器并设置过滤器 JFileChooser fileChooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("Text Files", "txt"); fileChooser.setFileFilter(filter); // 显示文件选择器并处理用户选择的文件 int result = fileChooser.showSaveDialog(this); if (result == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); try { BufferedWriter writer = new BufferedWriter(new FileWriter(file)); writer.write(textArea.getText()); writer.close(); } catch (Exception ex) { ex.printStackTrace(); } } } } public static void main(String[] args) { // 创建并显示简易文件编辑器窗口 SimpleEditor editor = new SimpleEditor(); editor.setVisible(true); } } ``` 该程序创建了一个窗口,其中包括一个文本区域和两个按钮,用于打开和保存文件。当用户单击“Open”按钮时,程序会显示一个文件选择器,用户可以选择要打开的文件。程序会将打开的文件中的文本显示在文本区域中。当用户单击“Save”按钮时,程序会显示一个文件选择器,用户可以选择要保存的文件。程序会将文本区域中的文本保存到所选的文件中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

繁华渲染悲凉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值