【问题标题】:JTextArea default font very small in WindowsWindows 中的 JTextArea 默认字体非常小
【发布时间】:2011-09-21 15:14:48
【问题描述】:

我使用的是平台外观,在 Linux 上我的 JTextArea 非常易读 但在 Windows 上它使用“Monospaced 9”并且文本非常小。

为什么以及最好的解决方法是什么?

为什么默认的 Windows 外观在 JTextArea 中使用这么小的字体?

【问题讨论】:

  • hmm,无法重现:在我的系统 (Vista) 上,字体是等宽的 16。通常,WindowLookAndFeel 使用操作系统字体设置 - 所以您的系统设置可能太小了?你的win版本是什么?

标签: java swing look-and-feel jtextarea


【解决方案1】:

您可以使用JTextArea1.setFont(Font(String name, int style, int size)) 方法为JTextArea 组件指定特定类型的字体。举个例子

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

}

【讨论】:

  • 我不想指定字体,我只想指定字体的大小:) 应该使用默认字体
  • jTextArea 没有 getStyle() 方法。 setFont 方法不接受 3 个参数。
【解决方案2】:

这是一个解决方案,您可以使用它来一次更改所有 JTextAreas,而不是每次添加新文本区域时都使用 setFont():

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

在设置外观和感觉之后,在应用程序启动时调用它。

大多数 L&F 对 JTextArea 和 JTextField 使用相同的字体,奇怪的是 Windows 没有。

【讨论】:

    【解决方案3】:

    与其创建新字体,不如derive现有字体,因为这样可以保存平台观感设置的字体,也可以避免unicode字符的问题:

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

    【讨论】:

      【解决方案4】:

      就做吧

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

      这会将 textarea 内的所有文本更改为相同大小的字体。

      【讨论】:

        【解决方案5】:

        我刚刚在 TextArea 中使用了 TextField 字体...

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

        【讨论】:

          【解决方案6】:

          如果您想要一致的外观,请使用 Nimbus 或 Metal 外观而不是操作系统默认值。这也将允许您调整任何设置。另外,我个人认为 Nimbus 的外观和感觉比其他的更流畅。

          【讨论】:

          • 在 Windows 上它看起来与操作系统窗口非常不同。我真的更喜欢“系统”的外观和感觉
          • 尽管如此,Nimbus 和 Metal 在任何平台上看起来都很陌生,有些用户真的不喜欢它。我希望花哨的皮肤应用的时代已经过去了。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-05
          • 2020-10-14
          • 1970-01-01
          • 1970-01-01
          • 2016-02-20
          • 2019-12-11
          相关资源
          最近更新 更多