0

我正在开发一个文本编辑器,我有主框架,通常它工作正常,添加额外的单词删除,但我希望为其添加粗体和下划线功能。我不知道该怎么做。是否有可能,如果有任何建议,代码如下:

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 );

    }
}
4

1 回答 1

5

如果您想要粗体文本,请使用

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);

Java中下划线字体的常量值是多少?

于 2012-05-09T11:22:09.817 回答