简体   繁体   中英

JTextArea default font very small in Windows

I'm using platform look-and-fell and on Linux my JTextArea is pretty readable But on Windows it uses "Monospaced 9" and the text is very small.

Why and what is the best way to fix that?

Why default Windows look-and-fell uses such small font in JTextArea?

不是创建新字体,最好是派生现有字体,因为这样你就可以通过平台外观保存字体集,并且还可以避免unicode字符的问题:

textArea.setFont(textArea.getFont().deriveFont(12f)); // will only change size to 12pt

Here's a solution that you can use to change all JTextAreas at once instead of using setFont() every time you add new text area:

UIManager.getDefaults().put("TextArea.font", UIManager.getFont("TextField.font"));

Call this on start of your application, after setting the Look and Feel.

Most L&Fs use the same font for JTextArea and JTextField, it's strange that Windows doesn't.

If you want a consistent look then use the Nimbus or Metal look and feel instead of the OS default. That will also allow you to tweak any settings. Plus I personally I think the Nimbus Look and Feel is much smoother looking than the others.

You can use the JTextArea1.setFont(Font(String name, int style, int size)) method to specify the specific type of font for a JTextArea component. As an example

jTextArea1.setFont(new Font("Arial Black", Font.BOLD, 8));


import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

public class NewJFrame extends javax.swing.JFrame {

    private JTextArea jTextArea1;
    private JTextArea jTextArea2;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                NewJFrame inst = new NewJFrame();
                inst.setLocationRelativeTo(null);
                inst.setVisible(true);
            }
        });
    }

    public NewJFrame() {
        super();
        initGUI();
    }

    private void initGUI() {
        try {
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            {
                jTextArea1 = new JTextArea();
                getContentPane().add(jTextArea1, BorderLayout.NORTH);
                jTextArea1.setText("This is a fox running slow");
                jTextArea1.setFont(new Font("Arial Black", Font.BOLD, 8));
                jTextArea1.setPreferredSize(new java.awt.Dimension(164, 114));
            }
            {
                jTextArea2 = new JTextArea();
                getContentPane().add(jTextArea2, BorderLayout.SOUTH);
                jTextArea2.setText("This is a fox running slow");
                jTextArea2.setFont(new Font("Book Antiqua", Font.ITALIC, 12));
                jTextArea2.setPreferredSize(new java.awt.Dimension(384, 129));
            }
            pack();
            setSize(400, 300);
        } catch (Exception e) {
            //add your error handling code here
            e.printStackTrace();
        }
    }

}

I've just used TextField font in TextArea...

textArea = new JTextArea();
textArea.setFont(UIManager.getFont("TextField.font"));

Just do

textArea.setFont(new Font("Arial", Font.PLAIN, 16));

That changes all of the text inside of the textarea to the same size font.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM