在java中实现编辑器 [英] implement editor in java
问题描述
我正在使用java开发一个简单的c-editor,我想在其中添加以下功能...我已经实现了简单的空格和单词,但是当有花括号时会出现缩进问题(例如,如果有那里有空格分开的开口支架,它不起作用。也就是说,关闭的支架没有与开口支架正确对齐。)
欢迎使用解决方案。这是我的代码实现了缩进..
I am developing a simple c-editor using java and I want to add following feature in it...I have implemented for simple space and word but having issues with indention when there are curly braces(for example, if there are space separated opening braces are there,it is not working.Also, the closing braces are not properly aligned with the opening braces).
Solutions are welcomed.Here is my code which implement the indention..
import java.awt.*;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.text.*;
public class Indent extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
public Indent()
{
JTextPane textComponent = new JTextPane();
JScrollPane scrollPane = new JScrollPane( textComponent );
getContentPane().add( scrollPane );
ActionMap am = textComponent.getActionMap();
am.put(DefaultEditorKit.insertBreakAction, new IndentAction());
}
public static class IndentAction extends TextAction
{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Creates this object with the appropriate identifier.
*/
public IndentAction()
{
super(DefaultEditorKit.insertBreakAction);
}
/**
* The operation to perform when this action is triggered.
*
* @param e the action event
*/
public void actionPerformed(ActionEvent e)
{
JTextComponent target = getTextComponent(e);
if (target == null) return;
if ((! target.isEditable()) || (! target.isEnabled()))
{
UIManager.getLookAndFeel().provideErrorFeedback(target);
return;
}
try
{
// Determine which line we are on
Document doc = target.getDocument();
Element rootElement = doc.getDefaultRootElement();
int selectionStart = target.getSelectionStart();
int line = rootElement.getElementIndex( selectionStart );
StringBuilder indent = new StringBuilder(new String(""));
// Get the text for this line
int start = rootElement.getElement(line).getStartOffset();
int end = rootElement.getElement(line).getEndOffset();
int length = end - start;
String text = doc.getText(start, length);
int offset = 0;
boolean posBrace = text.contains("{");
boolean closeBrace = text.contains("}");
// Get the number of white spaces characters at the start of the line
for (offset = 0; offset < length; offset++)
{
char c = text.charAt(offset);
if (c != ' ' && c != '\t')
break;
}
// When splitting the text include white space at start of line
// else do default processing
if (selectionStart - start > offset)
{
indent.append( text.substring(0, offset) );
if( !posBrace )
{
indent.insert(0, "\n");
}
else
{
indent.insert(0, "\n\t");
}
target.replaceSelection(indent.toString() );
if(closeBrace)
{
indent.deleteCharAt(indent.indexOf("\t"));
target.replaceSelection(indent.toString() );
}
}
else
target.replaceSelection("\n");
}
catch(BadLocationException ble) {}
}
}
public static void main(String[] args)
{
Indent frame =new Indent();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.setSize( new Dimension( 300, 100) );
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}
推荐答案
在你的程序中,需要更换以下条件:
In your program, need to replace below condition :
if(f==1)
target.replaceSelection( "\n\t");
使用此代码:
with this code:
if(f==1)
{
target.replaceSelection( "\n");
target.replaceSelection("\t //write your code");
target.replaceSelection( "\n");
target.replaceSelection("}");
f=0;
}
编译并运行程序输出将是:
Compile and run the program the out put will be :
方法{
//编写代码
}
Method{
//write your code
}
这只是一个示例代码。希望对你有所帮助。
问候,
Panduranga。
This is only a sample code. Hope it helps you.
Regards,
Panduranga.
这篇关于在java中实现编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!