Java Swing中可以給每個控件設置字體格式和其他屬性的設置,示例如下:
創(chuàng)新互聯(lián)長期為數(shù)千家客戶提供的網(wǎng)站建設服務,團隊從業(yè)經(jīng)驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為疏附企業(yè)提供專業(yè)的做網(wǎng)站、成都網(wǎng)站建設,疏附網(wǎng)站改版等技術服務。擁有十多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
submit= new JButton("登陸");
submit.setFont(new Font("宋體", Font.PLAIN, 16));
三個參數(shù)分別表示: 字體,樣式(粗體,斜體等),字號
submit.setForeground(Color.RED);
這個表示給組件上的文字設置顏色Color.RED表示紅色
當然你也可以自己給RGB的值 比如 submit.setForeground(new Color(215,215,200));
Java設置label字體代碼如下:
ublic?class?SetColor?extends?JFrame{
JLabel?jlabel?=?new?JLabel("顏色,大小");
public?SetColor(){
this.setLayout(null);
jlabel.setBounds(0,?0,?200,?40);
jlabel.setFont(new?Font("",1,30));//設置字體大小
jlabel.setForeground(Color.BLUE);//設置字體顏色
this.add(jlabel);
this.setSize(200,200);
this.setVisible(true);
}
/**
*?@param?args
*/
public?static?void?main(String[]?args)?{
//?TODO?Auto-generated?method?stub
SetColor?sc?=?new?SetColor();
}}
用樣式定義,如:對p標簽下的字體:p {font-family: "微軟雅黑,新宋體,宋體";},而且可以定義多個字體,如果第一個字體支持就用第一個,第一個沒有用第二個,依次類推。
1、window --Preference
2、在出現(xiàn)的新窗體中選擇General--Appearance--Colors and Fonts,在窗體的右側選擇Basic--TextFonts,再點擊Edit就可以進入到修改頁面修改字體樣式了
復制以下代碼,修改相應地方即可:
private?static?void?loadIndyFont()
{
UIManager.put("CheckBox.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("Tree.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("Viewport.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("ProgressBar.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("RadioButtonMenuItem.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("FormattedTextField.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("ToolBar.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("ColorChooser.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("ToggleButton.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("Panel.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("TextArea.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("Menu.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("RadioButtonMenuItem.acceleratorFont",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("Spinner.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("Menu.acceleratorFont",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("CheckBoxMenuItem.acceleratorFont",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("TableHeader.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("TextField.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("OptionPane.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("MenuBar.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("Button.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("Label.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("PasswordField.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("InternalFrame.titleFont",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("OptionPane.buttonFont",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("ScrollPane.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("MenuItem.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("ToolTip.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("List.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("OptionPane.messageFont",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("EditorPane.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("Table.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("TabbedPane.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("RadioButton.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("CheckBoxMenuItem.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("TextPane.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("PopupMenu.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("TitledBorder.font",?new?java.awt.Font("宋體",?0,?12));
UIManager.put("ComboBox.font",?new?java.awt.Font("宋體",?0,?12));
}
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Insets;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
public class main {
static String tf_str=null;
public static void main(String[] args) {
Frame a = new Frame("打印");
a.setBounds(400, 300, 400, 300);
a.setLayout(new FlowLayout());
TextField b = new TextField(20);
Button c = new Button("確定");
Button e = new Button("紅色");
Button f = new Button("藍色");
JTextPane d=new JTextPane();
d.setMargin(new Insets(100,100, 100, 100));
a.add(b);
a.add(c);
a.add(d);
a.add(e);
a.add(f);
a.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
c.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
d.setText("");
tf_str = b.getText().trim();
b.setText("");
appendToPane(d, tf_str, Color.black);
b.requestFocus();
}
});
e.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
d.setText("");
appendToPane(d, tf_str, Color.RED);
}
});
f.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
d.setText("");
appendToPane(d, tf_str, Color.BLUE);
}
});
a.setVisible(true);
}
private static void appendToPane(JTextPane tp, String msg, Color c) {
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
aset = sc.addAttribute(aset, StyleConstants.FontFamily, "宋體");
aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);
int len = tp.getDocument().getLength();
tp.setCaretPosition(len);
tp.setCharacterAttributes(aset, false);
tp.replaceSelection(msg);
}
}