`
xiao_hua
  • 浏览: 38792 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

实现如UltraEdit-32查找功能,高亮显示,滚动条居中

阅读更多
项目需求,需实现如题的小功能,由于刚接触swing几天,对他的线程机制不是很懂,可能demo中的代码有不妥之处,望大牛们指点一二,谢谢哈
package com.swing.study01;

import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;


public class Study14 extends JFrame 
{
	public JTextArea ja = new JTextArea();
	public javax.swing.JScrollPane jScrollPane = new JScrollPane(ja);

	public Study14()  {

		jScrollPane.setBounds(0, 160, 500, 300);
		add(jScrollPane);
		try {
			ReadFile.readFile("D:\\test\\test.txt",
	ja);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws IOException,
			InterruptedException, BadLocationException {
 
				Study14 f = new Study14();
				f.setVisible(true);
				f.setBounds(400, 300, 800, 500);
				f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				f.addWindowListener(new WindowAdapter() {
					@Override
					public void windowClosed(WindowEvent e) {
						System.exit(0);
					}
				});
				
				int index = f.ja.getText().indexOf("目标字符");
				f.ja.setCaretPosition(index);
				int line = -1;
				line = f.ja.getLineOfOffset(index);
				//高亮
				lineHighlighter(f.ja,line,new Color(23, 239, 255));
				Thread.sleep(50);
		
				//公式=目标所在行 * (文本域高/文本总行数) - 滚动条高/2
				int h = line * (f.ja.getHeight() / f.ja.getLineCount()) - (f.jScrollPane.getHeight()/2);
			    f.jScrollPane.getVerticalScrollBar().setValue(h);
 
	}
	
	  /**
     * 方法描述:高亮显示行
     * @param area
     * @param line
     */
    public static void lineHighlighter(JTextArea area,int line,Color color)
    {
    	 //单击 高亮   
        DefaultHighlighter h = (DefaultHighlighter) area.getHighlighter();   
        MyHighlightPainter p = new MyHighlightPainter(   
                color);   
       try 
       {   
            int start = area.getLineStartOffset(line);   
            int end = area.getLineEndOffset(line);  
            h.removeAllHighlights();   
            h.addHighlight(start, end, p);   
        } catch (BadLocationException e1)
        {   
           // e1.printStackTrace();   
        }   
     }
}
class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter 
{   
    public MyHighlightPainter(Color color)
    {   
        super(color);   
    }   
}   

class ReadFile {
	public static void readFile(String fileName, JTextArea area)
			throws IOException {
		FileReader reader = null;
		BufferedReader bf = null;
		try {
			reader = new FileReader(new File(fileName));
			bf = new BufferedReader(reader);
			String line = null;
			while ((line = bf.readLine()) != null) {
				area.append(line + "\n");
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			reader.close();
			bf.close();
		}

	}
}

1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics