扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
Java Swing中可以给每个控件设置字体格式和其他属性的设置,示例如下:
创新互联长期为数千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为疏附企业提供专业的做网站、成都网站建设,疏附网站改版等技术服务。拥有十多年丰富建站经验和众多成功案例,为您定制开发。
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设置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、在出现的新窗体中选择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);
}
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流