本文目錄一覽:
java 字體設置
1、對字體的操作
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setFontFamily(attr, family);
setCharacterAttributes(editor, attr, false);
family為字體
2、對字體大小的操作
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setFontSize(attr, size);
setCharacterAttributes(editor, attr, false);
size為字型大小
3、是否是粗體的操作
StyledEditorKit kit = getStyledEditorKit(editor);
MutableAttributeSet attr = kit.getInputAttributes();
boolean bold = (StyleConstants.isBold(attr)) ? false : true;
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setBold(sas, bold);
setCharacterAttributes(editor, sas, false);
4、是否是斜體的操作
StyledEditorKit kit = getStyledEditorKit(editor);
MutableAttributeSet attr = kit.getInputAttributes();
boolean italic = (StyleConstants.isItalic(attr)) ? false : true;
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setItalic(sas, italic);
setCharacterAttributes(editor, sas, false);
5、是否有下劃線的操作
StyledEditorKit kit = getStyledEditorKit(editor);
MutableAttributeSet attr = kit.getInputAttributes();
boolean underline = (StyleConstants.isUnderline(attr)) ? false
: true;
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setUnderline(sas, underline);
setCharacterAttributes(editor, sas, false);
6、左中右對齊的處理
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setAlignment(attr, a);
setParagraphAttributes(editor, attr, false);
public static final void setParagraphAttributes(JEditorPane editor,
AttributeSet attr, boolean replace) {
int p0 = editor.getSelectionStart();
int p1 = editor.getSelectionEnd();
StyledDocument doc = getStyledDocument(editor);
doc.setParagraphAttributes(p0, p1 – p0, attr, replace);
}
a:0:左,1:中,2:右
7、文本字體顏色的設置
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setForeground(attr, fg);
setCharacterAttributes(editor, attr, false);
fg:為color
8、文本背景顏色的設置
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setBackground(attr, bg);
setCharacterAttributes(editor, attr, false);
java 設置字體格式
Java
Swing中可以給每個控制項設置字體格式和其他屬性的設置,示例如下:
submit=
new
JButton(“登陸”);
submit.setFont(new
Font(“宋體”,
Font.PLAIN,
16));
三個參數分別表示:
字體,樣式(粗體,斜體等),字型大小
submit.setForeground(Color.RED);
這個表示給組件上的文字設置顏色Color.RED表示紅色
當然你也可以自己給RGB的值
比如
submit.setForeground(new
Color(215,215,200));
java 如何設置字體格式?
Java Swing中可以給每個控制項設置字體格式和其他屬性的設置,示例如下:\x0d\x0asubmit= new JButton(“登陸”);\x0d\x0asubmit.setFont(new Font(“宋體”, Font.PLAIN, 16));\x0d\x0a三個參數分別表示: 字體,樣式(粗體,斜體等),字型大小\x0d\x0a submit.setForeground(Color.RED);\x0d\x0a這個表示給組件上的文字設置顏色Color.RED表示紅色\x0d\x0a當然你也可以自己給RGB的值 比如 submit.setForeground(new Color(215,215,200));
Java中怎麼設置JLabel的字體樣式,大小,顏色?
1、打開Myeclipse的相關界面,在Window那裡點擊Preferences。
2、彈出設置的對話框,選擇General下的Appearance進入。
3、點擊Colors and Fonts按鈕,需要在右側選擇Java。
4、選擇Java Editor Text Font,並點擊Edit。
5、通過設置對應的參數以後,直接確定返回。
6、這樣一來會看到圖示的結果,即可設置JLabel的字體樣式,大小,顏色了。
原創文章,作者:ZUCTR,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/330260.html