[英]Set font width in a JTextPane
我正在嘗試在JTextPane中設置字體寬度,但是不知何故我沒有發現如何做到這一點。
這是我已經擁有的代碼:
Font resultFont = new Font("Courier", Font.PLAIN, 12);
JTextPane resultPane = new JTextPane();
resultPane.setText(result);
resultPane.setFont(resultFont);
resultPane.setEditable(false);
add(resultPane);
如果有可能拉伸字體,那真的很酷,因為我想顯示一些ASCII藝術。
您是要拉伸字符還是僅調整“字距調整”(字符之間的間距)? 如果僅是字距調整:您可以在此處找到解決方案
我認為您正在尋找等寬字體: SWT-與OS無關的方式來獲取等寬字體
這是一個例子:
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class TestProject extends JPanel{
public TestProject(){
super();
JTextPane ta = new JTextPane();
//If you want to adjust your line spacing too
MutableAttributeSet set = new SimpleAttributeSet();
StyleConstants.setLineSpacing(set, -.2f);
ta.setParagraphAttributes(set, false);
//Making font monospaced
ta.setFont(new Font("Monospaced", Font.PLAIN, 20));
//Apply your ascii art
ta.setText(" .-.\n(o o) boo!\n| O \\\n \\ \\\n `~~~'");
//Add to panel
add(ta, BorderLayout.CENTER);
}
public static void main(String args[])
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setContentPane(new TestProject());
frame.pack();
frame.setVisible(true);
}
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.