十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
复制以下代码,修改相应地方即可:
公司主营业务:网站设计制作、成都网站制作、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联公司推出五原免费做网站回馈大家。
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));
}
如果就设置字体,即大小,加一句setFont就行了
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;
public class Q1{
JWindow window = new JWindow();
Q1(String pText){
//获取当前计算机屏幕尺寸
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
//定义标记显示信息
JLabel jl = new JLabel(pText,JLabel.CENTER);
Font d=new Font("楷体",Font.BOLD,10);
jl.setFont(d); //注意这句
//添加组件到窗口
window.getContentPane().add(jl, BorderLayout.CENTER);
// 设置窗口尺寸
window.setSize(300, 100);
//设置窗口显示位置
int w = window.getSize().width;
int h = window.getSize().height;
int x = (dim.width-w)/2;
int y = (dim.height-h)/2;
//移动窗口到X,Y坐标。这里时屏幕的中央
window.setLocation(x, y);
}
//隐藏窗口
public void hidden(){
window.setVisible(false);
}
//显示窗口
public void show(){
window.setVisible(true);
}
public static void main(String[] args){
//
Q1 jw = new Q1("沟通无距离!平行线聊天系统...o(∩_∩)o...");
jw.show();
//显示延时
try{
Thread.sleep(4000);
}catch(InterruptedException ie){
System.out.println("sleep error!!");
}
//调用隐藏
//jw.hidden();
}
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
}
如果要设置字体颜色,你可以自定义继承JLabel的类,在paintComponent(Graphics g)方法中,用g.setColor();g.drawString()做。
设置背景的话在setFont下加一句setBackground()方法,传Image类进去。
整好,前两天看了一篇博文,讲的就是swing界面的统一,大家互相学习。代码参考如下
/**
* 统一设置字体,父界面设置之后,所有由父界面进入的子界面都不需要再次设置字体
* 感谢下博文的作者chenxuejiakaren
*/
private static void InitGlobalFont(Font font) {
FontUIResource fontRes = new FontUIResource(font);
for (EnumerationObject keys = UIManager.getDefaults().keys();
keys.hasMoreElements(); ) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof FontUIResource) {
UIManager.put(key, fontRes);
}
}
在main方法中,初始化窗口时,加入下面这句:
InitGlobalFont(new Font("alias", Font.PLAIN, 12)); //统一设置字体就可以了。
注意:InitGlobalFont 这个方法的调用时在swing界面的入口界面里。
我在你的基础上稍微添加了一些代码,现在可以改变字体大小了:
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
class FontDialog extends JDialog implements ItemListener, ActionListener {
JComboBox list;
JComboBox listSize; //显示可以选择的字体大小
JTextArea text;
Font font;
JButton yes, cancel;
JComponent com;
FontDialog(JComponent com) {
this点抗 = com;
setModal(true);
yes = new JButton("Yes");
cancel = new JButton("cancel");
yes.addActionListener(this);
cancel.addActionListener(this);
text = new JTextArea(2, 25);
list = new JComboBox();
listSize = new JComboBox();
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
String fontName[] = ge.getAvailableFontFamilyNames();
for (int i = 0; i fontName.length; i++){
list.addItem(fontName[i]);
}
//定义字体大小数组,字体大小可以自行定义
int[] fontSize = new int[20];
for (int i = 0; i fontSize.length; i++){
fontSize[i] = i+5;
listSize.addItem(fontSize[i]);
}
list.addItemListener(this);
listSize.addItemListener(this); //为设置字体大小的添加监听器
Container con = getContentPane();
con.setLayout(new FlowLayout());
Box boxH1 = Box.createHorizontalBox(), boxH2 = Box
.createHorizontalBox(), boxH3 = Box.createHorizontalBox();
Box boxBase = Box.createVerticalBox();
boxH1.add(list);
boxH1.add(listSize); //添加字体大小设置到面板中
boxH2.add(text);
boxH3.add(yes);
boxH3.add(cancel);
boxBase.add(boxH1);
boxBase.add(boxH2);
boxBase.add(boxH3);
con.add(boxBase);
setBounds(100, 100, 280, 170);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
validate();
}
public void itemStateChanged(ItemEvent e) {
String name = (String) list.getSelectedItem();
Integer size = (Integer) listSize.getSelectedItem(); //获得设置的字体大小
font = new Font(name, Font.PLAIN, size);
text.setFont(font);
text.setText("hello,奥运");
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == yes) {
com.setFont(font);
setVisible(false);
} else if (e.getSource() == cancel) {
setVisible(false);
}
}
}
class Dwindow extends JFrame implements ActionListener {
JTextArea text;
JToolBar bar;
Container con;
JButton buttonFont;
Dwindow() {
buttonFont = new JButton("设置字体");
text = new JTextArea("显示内容");
buttonFont.addActionListener(this);
bar = new JToolBar();
bar.add(buttonFont);
con = getContentPane();
con.add(bar, BorderLayout.NORTH);
con.add(new JScrollPane(text));
setBounds(60, 60, 300, 300);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttonFont) {
FontDialog dialog = new FontDialog(text);
dialog.setVisible(true);
}
}
}
public class DialogExample {
public static void main(String args[]) {
new Dwindow();
}
}