復制以下代碼,修改相應地方即可:
成都創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務領域包括:成都網站設計、成都網站建設、企業(yè)官網、英文網站、手機端網站、網站推廣等服務,滿足客戶于互聯(lián)網時代的維西網站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網解決方案。努力成為您成熟可靠的網絡建設合作伙伴!
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));
}
如果是swing的話
JLabel jl = new JLabel("標簽");
jl.setBounds(10, 10, 10, 10);這是控制標簽位置;
控制文本的位置,你可以通過在“”里邊加空格控制
jl.setFont(new Font("宋體", Font.BOLD, 20));
這是控制文本字體,那個20就是大小選擇,可以改變的
我在你的基礎上稍微添加了一些代碼,現(xiàn)在可以改變字體大小了:
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 = 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]);
}
//定義字體大小數(shù)組,字體大小可以自行定義
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); //為設置字體大小的添加監(jiān)聽器
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();
}
}
package hao;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class ChatPanel extends JPanel {
private static final long serialVersionUID = 1L;
JButton send,record,saveRecord,image;
JTextArea inputArea;
JTextPane text;//注意用法****************************************************************************
JComboBox fontName = null, fontSize = null, fontStyle = null, fontColor = null,fontBackColor = null;
public StyledDocument doc = null; JScrollPane scrollPane;JPanel textChat;
JButton music;
public ChatPanel() {
setLayout(new BorderLayout());
text = new JTextPane();
text.setEditable(false);
doc = text.getStyledDocument();//跟蹤文本和圖片寫到該區(qū)域的位置*************************************
scrollPane = new JScrollPane(text);
//注意下面對JComboBox的巧用***********************************************************************
String[] str_name = { "宋體", "黑體", "Dialog", "Gulim" };
String[] str_Size = { "12", "14", "18", "22", "30", "40" };
String[] str_Style = { "常規(guī)", "斜體", "粗體", "粗斜體" };
String[] str_Color = { "黑色", "紅色", "藍色", "黃色", "綠色" };
String[] str_BackColor = { "無色", "灰色", "淡紅", "淡藍", "淡黃", "淡綠" };
fontName = new JComboBox(str_name);
fontSize = new JComboBox(str_Size);
fontStyle = new JComboBox(str_Style);
fontColor = new JComboBox(str_Color);
fontBackColor = new JComboBox(str_BackColor);
fontName.setBackground(new Color(255,153,255));
fontSize.setBackground(new Color(255,153,255));
fontStyle.setBackground(new Color(255,153,255));
fontColor.setBackground(new Color(255,153,255));
fontBackColor.setBackground(new Color(255,153,255));
Box box = Box.createVerticalBox();//創(chuàng)建一個可以容納多個Box組件的Box*******************************
Box box_1 = Box.createHorizontalBox();
Box box_2 = Box.createHorizontalBox();
Box box_4 = Box.createHorizontalBox();
box.add(box_1);
box.add(box_2);
box.add(box_4);
JLabel b1= new JLabel("字體~~"), b2 = new JLabel("樣式~~"),b3 = new JLabel("字號~~"),b4 = new JLabel("顏色~~"),b5 = new JLabel("背景~~");
b1.setBackground(new Color(255,153,255));
b2.setBackground(new Color(255,153,255));
b3.setBackground(new Color(255,153,255));
b4.setBackground(new Color(255,153,255));
b5.setBackground(new Color(255,153,255));
box_1.add(b1);
box_1.add(fontName);
box_1.add(Box.createHorizontalStrut(8));
box_1.add(b2);
box_1.add(fontStyle);
box_1.add(Box.createHorizontalStrut(8));
box_1.add(b3);
box_1.add(fontSize);
box_2.add(Box.createHorizontalStrut(8));
box_2.add(b4);
box_2.add(fontColor);
box_2.add(Box.createHorizontalStrut(8));
box_4.add(b5);
box_4.add(fontBackColor);
textChat = new JPanel();
textChat.setLayout(new BorderLayout());
textChat.setBackground(new Color(255,153,255));
inputArea = new JTextArea(3, 20);
inputArea.setLineWrap(true); //設置文本區(qū)的換行策略。88888*********************************
send = new JButton("發(fā)送");
record=new JButton("顯示記錄");
saveRecord=new JButton("儲存記錄");
image=new JButton("表情");
send.setBackground(new Color(255,153,255));
record.setBackground(new Color(255,153,255));
saveRecord.setBackground(new Color(255,153,255));
image.setBackground(new Color(255,153,255));
Box box_3 = Box.createHorizontalBox();
box_3.add(send); box_3.add(Box.createHorizontalStrut(8));//設置按鈕間距*************************888
box_3.add(record); box_3.add(Box.createHorizontalStrut(8)); //設置按鈕間距*************************888
box_3.add(saveRecord); box_3.add(Box.createHorizontalStrut(8));//設置按鈕間距*************************888
box_3.add(image);
box.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));//設置Box的邊框線********************
box_3.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));
textChat.add(box,BorderLayout.NORTH);
textChat.add(inputArea,BorderLayout.CENTER);
textChat.add(box_3, BorderLayout.SOUTH);
inputArea.requestFocus(true);
inputArea.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));//設置輸入窗口邊框線*******************
text.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),8));//設置輸入窗口邊框線*******************
JPanel audioPanel = new JPanel();//最上面的邊框************************************************************************
audioPanel.setBackground(new Color(255,153,255));
audioPanel.setLayout(new GridLayout(1,1));
music = new JButton("想聽就聽");
music.setPreferredSize(new Dimension(320,50));
music.setBorder(BorderFactory.createLineBorder(Color.BLACK,10));//設置輸入窗口邊框線*******************
audioPanel.add(music);
add(audioPanel, BorderLayout.NORTH);
add(scrollPane,BorderLayout.CENTER);
add(textChat, BorderLayout.SOUTH);
}
void insertIcon(ImageIcon image) {
text.setCaretPosition(doc.getLength());
text.insertIcon(image);
insert(new MessageStyle());//?????????????????????????????????????????????????????????????????????????????/
}
public void insert(MessageStyle attrib) {
try {
doc.insertString(doc.getLength(), attrib.getText() + "\n", attrib.getAttrSet());//寫完后接著換行************
} catch (BadLocationException e) {
e.printStackTrace();
}
}
public MessageStyle getMessageStyle(String line) {
MessageStyle att = new MessageStyle();
att.setText(line);
att.setName((String) fontName.getSelectedItem());
att.setSize(Integer.parseInt((String) fontSize.getSelectedItem()));
String temp_style = (String) fontStyle.getSelectedItem();
if (temp_style.equals("常規(guī)")) {
att.setStyle(MessageStyle.GENERAL);
}
else if (temp_style.equals("粗體")) {
att.setStyle(MessageStyle.BOLD);
}
else if (temp_style.equals("斜體")) {
att.setStyle(MessageStyle.ITALIC);
}
else if (temp_style.equals("粗斜體")) {
att.setStyle(MessageStyle.BOLD_ITALIC);
}
String temp_color = (String) fontColor.getSelectedItem();
if (temp_color.equals("黑色")) {
att.setColor(new Color(0, 0, 0));
}
else if (temp_color.equals("紅色")) {
att.setColor(new Color(255, 0, 0));
}
else if (temp_color.equals("藍色")) {
att.setColor(new Color(0, 0, 255));
}
else if (temp_color.equals("黃色")) {
att.setColor(new Color(255, 255, 0));
}
else if (temp_color.equals("綠色")) {
att.setColor(new Color(0, 255, 0));
}
String temp_backColor = (String) fontBackColor.getSelectedItem();
if (!temp_backColor.equals("無色")) {
if (temp_backColor.equals("灰色")) {
att.setBackColor(new Color(200, 200, 200));
}
else if (temp_backColor.equals("淡紅")) {
att.setBackColor(new Color(255, 200, 200));
}
else if (temp_backColor.equals("淡藍")) {
att.setBackColor(new Color(200, 200, 255));
}
else if (temp_backColor.equals("淡黃")) {
att.setBackColor(new Color(255, 255, 200));
}
else if (temp_backColor.equals("淡綠")) {
att.setBackColor(new Color(200, 255, 200));
}
}
return att;
}
}