为java文本编辑器添加粗体和下划线功能
我正在开发一个文本编辑器,我有一个主框架,通常它可以正常工作,添加额外的单词删除,但我希望添加一个粗体和下划线的功能。我不知道该怎么做。如果有可能的话,有什么建议吗?代码如下:为java文本编辑器添加粗体和下划线功能,java,Java,我正在开发一个文本编辑器,我有一个主框架,通常它可以正常工作,添加额外的单词删除,但我希望添加一个粗体和下划线的功能。我不知道该怎么做。如果有可能的话,有什么建议吗?代码如下: import java.util.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.awt.image.*; class Main { public s
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
class Main
{
public static void main( String args[] ) //
{ //
System.out.println("Application");
(new Application()).setVisible(true); // Start application
} //
}
/*
* Copy of file in a buffer in memory
* Which is a collection of lines
*/
class Buffer
{
private ArrayList<Line> file = new ArrayList<Line>(0);
private int lines = 0;
public int getNoLines()
{
return file.size();
}
public String getLine( int i )
{
if ( i < lines )
{
return file.get(i).toString();
}
return "Error";
}
public void append( String s )
{
file.add( new Line( s ) );
lines++;
}
public void insert( int offset, int line, char c )
{
if ( line <= lines )
{
file.get(line).insert( offset, c );
}
}
public void delete( int offset, int line )
{
if ( line <= lines )
{
file.get(line).delete( offset );
}
}
public int getLineLength( int line )
{
return file.get( line ).getLineLength();
}
}
/*
* A line of text in the buffer
*/
class Line
{
private StringBuffer aLine;
public Line( String s )
{
aLine = new StringBuffer(256);
aLine.append( s );
}
public String toString()
{
return aLine.toString();
}
public void setLine( String s )
{
aLine = new StringBuffer(256);
aLine.append(s);
}
public void insert( int pos, char c )
{
if ( pos <= aLine.length() )
{
aLine.insert( pos, c );
}
}
public void delete( int pos )
{
if ( pos <= aLine.length() )
{
aLine.delete( pos, pos+1 );
}
}
public int getLineLength()
{
return aLine.length();
}
}
class Application extends JFrame // So graphical
{
private static final int H = 300; // Height of window
private static final int W = 400; // Width of window
private int x = 0;
private int y = 0;
Buffer file = new Buffer();
public Application()
{
// Fake reading in file
file.append( "Line 1" );
file.append( "Line 2" );
file.append( "----------" );
file.append( "Line 3" );
file.append( "Line 4" );
file.append( "----------" );
file.append( "Line 5" );
file.append( "Line 6" );
setSize( W, H ); // Size of application
addKeyListener( new Transaction() ); // Called when key press
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
class Transaction implements KeyListener // When character typed
{
public void keyPressed(KeyEvent e) // Obey this method
{
int ll = 0;
switch ( e.getKeyCode() ) // Character is
{
case KeyEvent.VK_LEFT: // Left Arrow
if ( x > 0 ) x--;
break;
case KeyEvent.VK_RIGHT: // Right arrow
ll = file.getLineLength( y );
if ( x < ll ) x++;
break;
case KeyEvent.VK_UP: // Up arrow
if ( y > 0 ) y--;
ll = file.getLineLength( y );
if ( x > ll ) x = ll;
break;
case KeyEvent.VK_DOWN: // Down arrow
int fl = file.getNoLines();
if ( y < fl-1 ) y++;
ll = file.getLineLength( y );
if ( x > ll ) x = ll;
break;
}
repaint(); // Call update method
}
public void keyReleased(KeyEvent e)
{
// Called on key release including specials
}
public void keyTyped(KeyEvent e)
{
char c = e.getKeyChar(); // Append printing char
if ( c >= ' ' && c <= '~' )
{
file.insert( x, y, c );
x++;
}
if ( c == (char) 127 ) // Delete char from file
{
if ( x > 0 )
{
file.delete( x-1, y );
x--;
}
}
repaint(); // Redraw screen
}
}
public void update( Graphics g ) // Called by repaint
{ //
drawPicture( (Graphics2D) g ); // Draw Picture
}
public void paint( Graphics g ) // When 'Window' is first
{ // shown or damaged
drawPicture( (Graphics2D) g ); // Draw Picture
}
private Dimension theAD; // Alternate Dimension
private BufferedImage theAI; // Alternate Image
private Graphics2D theAG; // Alternate Graphics
public void drawPicture( Graphics2D g ) // Double buffer
{ // allow re-size
Dimension d = getSize(); // Size of image
if ( ( theAG == null ) ||
( d.width != theAD.width ) ||
( d.height != theAD.height ) )
{ // New size
theAD = d;
theAI = (BufferedImage) createImage( d.width, d.height );
theAG = theAI.createGraphics();
if (true )
{
AffineTransform at = new AffineTransform();
at.setToIdentity();
at.scale( ((double)d.width)/W, ((double)d.height)/H );
theAG.transform(at);
}
}
drawActualPicture( theAG ); // Draw Picture
g.drawImage( theAI, 0, 0, this ); // Display on screen
}
public void drawActualPicture( Graphics2D g ) // Draw text
{
int WA= 20; // Offset Width
int HA= 40; // Offset Height for drawing text
Font font = new Font("Monospaced",Font.PLAIN,14);
g.setFont( font );
FontMetrics fm = g.getFontMetrics();
int WF = fm.charWidth( '.' ); // Width pixels
int HF = fm.getHeight(); // Height pixels
// Clear drawing area
g.setPaint( Color.white ); // Paint Colour
g.fill( new Rectangle2D.Double( 0, 0, W, H ) );
// Display text of file in black
g.setPaint( Color.black );
for ( int i=0; i<file.getNoLines(); i++ )
{
g.drawString( file.getLine(i), WA, i*HF + HA );
}
// Draw cursor current position
g.setPaint( Color.red );
g.drawLine( WA + x*WF, HA + y*HF, WA + (x)*WF, HA + (y-1)*HF );
}
}
import java.util.*;
导入javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
导入java.awt.geom.*;
导入java.awt.image.*;
班长
{
公共静态void main(字符串args[])//
{ //
System.out.println(“应用程序”);
(新应用程序()).setVisible(true);//启动应用程序
} //
}
/*
*在内存缓冲区中复制文件
*这是一组行的集合
*/
类缓冲区
{
私有ArrayList文件=新ArrayList(0);
私有整数行=0;
公共int getNoLines()
{
返回file.size();
}
公共字符串getLine(inti)
{
如果(i<行)
{
返回文件.get(i).toString();
}
返回“错误”;
}
公共void追加(字符串s)
{
添加(新行);
行++;
}
公共空白插入(整数偏移、整数行、字符c)
{
如果(行ll)x=ll;
打破
}
repaint();//调用update方法
}
公共无效密钥已释放(密钥事件e)
{
//在包括特价在内的关键发布中被调用
}
public void keyTyped(KeyEvent e)
{
char c=e.getKeyChar();//追加打印字符
如果(c>=''&&c 0)
{
删除(x-1,y);
x--;
}
}
重新绘制();//重绘屏幕
}
}
public void update(Graphics g)//通过重新绘制调用
{ //
drawPicture((Graphics2D)g);//绘制图片
}
public void paint(图形g)//第一个是“Window”时
{//显示或损坏
drawPicture((Graphics2D)g);//绘制图片
}
私有维度theAD;//替代维度
专用缓冲区映像theAI;//交替映像
专用图形2d theAG;//交替图形
public void drawPicture(Graphics2D g)//双缓冲区
{//允许重新调整大小
维度d=getSize();//图像的大小
if((theAG==null)||
(d.宽度!=A.宽度)||
(d.高度!=A.高度)
{//新尺寸
theAD=d;
theAI=(BuffereImage)createImage(d.宽度,d.高度);
theAG=theAI.createGraphics();
如果(真)
{
AffineTransform at=新的AffineTransform();
at.setToIdentity();
刻度(((双)直径宽度)/W、((双)直径高度)/H);
变形(at);
}
}
绘制实际图片(theAG);//绘制图片
g、 drawImage(theAI,0,0,this);//显示在屏幕上
}
public void drawActualPicture(Graphics2D g)//绘制文本
{
int WA=20;//偏移宽度
int HA=40;//图形文本的偏移高度
Font Font=新字体(“等距”,普通字体,14);
g、 setFont(字体);
FontMetrics fm=g.getFontMetrics();
int WF=fm.charWidth('.');//宽度像素
int HF=fm.getHeight();//高度像素
//净绘图区域
g、 setPaint(Color.white);//绘制颜色
g、 填充(新矩形2D.双(0,0,W,H));
//以黑色显示文件的文本
g、 setPaint(颜色为黑色);
对于(int i=0;i如果您想要粗体文本,请使用
Font theFont = new Font("Serif", Font.BOLD, 12);
如果要在其下面加下划线:
Map<TextAttribute, Integer> fontAttributes = new HashMap<TextAttribute, Integer>();
fontAttributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
Font boldUnderline = new Font("Serif",Font.BOLD, 12).deriveFont(fontAttributes);
MapFontAttributes=newHashMap();
fontAttributes.put(TextAttribute.UNDERLINE,TextAttribute.UNDERLINE\ON);
字体粗体下划线=新字体(“衬线”,Font.BOLD,12)。衍生字体(fontAttributes);
如果您想要粗体文本,请使用
Font theFont = new Font("Serif", Font.BOLD, 12);
如果要在其下面加下划线:
Map<TextAttribute, Integer> fontAttributes = new HashMap<TextAttribute, Integer>();
fontAttributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
Font boldUnderline = new Font("Serif",Font.BOLD, 12).deriveFont(fontAttributes);
MapFontAttributes=newHashMap();
fontAttributes.put(TextAttribute.UNDERLINE,TextAttribute.UNDERLINE\ON);
字体粗体下划线=新字体(“衬线”,Font.BOLD,12)。衍生字体(fontAttributes);
您是否考虑过先使用现有编辑器?文本编辑器不使用粗体和下划线。我想您是在开发文字处理器。您是否考虑过先使用现有编辑器?文本编辑器不使用粗体和下划线。我想您是在开发文字处理器。