java JTextArea 默认字体在 Windows 中非常小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6461506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JTextArea default font very small in Windows
提问by javapowered
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.
我正在使用平台外观,在 Linux 上,我的 JTextArea 可读性很强,但在 Windows 上,它使用“等宽 9”,文本非常小。
Why and what is the best way to fix that?
为什么以及解决这个问题的最佳方法是什么?
Why default Windows look-and-fell uses such small font in JTextArea?
为什么默认的 Windows look-and-fell 在 JTextArea 中使用这么小的字体?
回答by Denis Tulskiy
Instead of creating new font, it is better to deriveexisting font, because this way you'll save the font set by platform look and feel, and it may also avoid problems with unicode characters:
与其创建新字体,不如派生现有字体,因为这样您将保存平台外观设置的字体,并且还可以避免 unicode 字符的问题:
textArea.setFont(textArea.getFont().deriveFont(12f)); // will only change size to 12pt
回答by Speedstone
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:
这是一个解决方案,您可以使用它一次更改所有 JTextAreas,而不是每次添加新文本区域时都使用 setFont():
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.
大多数 L&F 对 JTextArea 和 JTextField 使用相同的字体,奇怪的是 Windows 没有。
回答by jzd
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.
如果您想要一致的外观,请使用 Nimbus 或 Metal 外观,而不是操作系统默认值。这也将允许您调整任何设置。另外,我个人认为 Nimbus 的外观和感觉比其他的要流畅得多。
回答by Rakesh
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(Font(String name, int style, int size))
方法为 JTextArea 组件指定特定的字体类型。举个例子
jTextArea1.setFont(new Font("Arial Black", Font.BOLD, 8));
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();
}
}
}
回答by javapowered
I've just used TextField font in TextArea...
我刚刚在 TextArea 中使用了 TextField 字体...
textArea = new JTextArea();
textArea.setFont(UIManager.getFont("TextField.font"));
回答by javapowered
Just do
做就是了
textArea.setFont(new Font("Arial", Font.PLAIN, 16));
textArea.setFont(new Font("Arial", Font.PLAIN, 16));
That changes all of the text inside of the textarea to the same size font.
这会将 textarea 内的所有文本更改为相同大小的字体。