开发者

Java JTextArea font

I have a custom font installed on my PC called "BMW1". I'm trying to loop through all entries in this font and display them in a JTextArea.

I have the following code:

JTextArea displayArea = new JTextArea();
Font font = new Font("BMW1", Font.PLAIN, 72);
displayArea.setFont(font);

String sample = "";
for (int cu开发者_如何学编程rrent = 0; current < 300; current++)
    sample += new Character((char)current).toString() + "\n";

displayArea.setText(sample);

When I run my program, it just prints out those little boxes (which I assume means it couldn't find a font entry for that iteration).

Am I doing something wrong here? Is JTextArea the best option for this sort of thing? Any suggestions on how to do this?


I'm not sure I can give you a full answer - but the loop in your code is wrong.

String sample = "";
for (int current = 0; current < 300; current++)
    sample += new Character((char)current).toString() + "\n";

The casting of 'current' into a 'char' will create a 'char' representing the ASCII value of 'current'. The first 27 characters in the ASCII table are non printable - so this might be the reason for your boxes.

Try starting from 65 till 90 ('A' - 'Z') to see if it works.


Check out the Font.canDisplay(...) methods to help you determine if your font can be used.

I've used a JTextArea for this purpose.

Here is a simple demo that lists the font available on your machine:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;

public class ComboBoxFonts extends JFrame implements ItemListener
{
    JTextArea textArea;
    JComboBox comboBox;

    public ComboBoxFonts()
    {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment ();
        Font [] fonts = ge.getAllFonts ();
        comboBox = new JComboBox( fonts );
        comboBox.setRenderer( new MyFontRenderer() );
        comboBox.addItemListener( this );
        getContentPane().add( comboBox, BorderLayout.SOUTH );
        textArea= new JTextArea("Some text", 3, 20);
        getContentPane().add( new JScrollPane( textArea ) );
    }

    public void itemStateChanged(ItemEvent e)
    {
        Font font = (Font)e.getItem();
        textArea.setFont( font.deriveFont( textArea.getFont().getSize2D() ) );
        comboBox.setFont( font.deriveFont( comboBox.getFont().getSize2D() ) );
    }

    public static void main(String[] args)
    {
        ComboBoxFonts frame = new ComboBoxFonts();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

    class MyFontRenderer extends BasicComboBoxRenderer
    {
        public Component getListCellRendererComponent(
            JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
        {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

            Font font = (Font)value;
            setFont( font.deriveFont(12.0f) );
            setText( font.getName() );

            return this;
        }
    }

}


Used Font canDisplay() methods to determine that Java can't display this font.

I ended up switching to C# which has better support for custom fonts (at least in this particular case).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜