代码如下
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class TableDialogFont extends JDialog {
private JList list_font_size;
private JList list_font_style;
private JList list_font_name;
private JTextField text_font_size;
private JTextField text_font_style;
private JTextField text_font_name;
private String fontName;
private String fontSize;
private String fontStyle;
private Font font_new;
private JLabel prve_label;
private JComponent component;
public Font getFont(){
return font_new;
}
private void actionForward(ListSelectionEvent arg0){
//加入选择的值
text_font_name.setText(String.valueOf(list_font_name.getSelectedValue()));
text_font_size.setText(String.valueOf(list_font_size.getSelectedValue()));
text_font_style.setText(String.valueOf(list_font_style.getSelectedValue()));
//处理预览
String name = text_font_name.getText();
String size = text_font_size.getText();
String style = text_font_style.getText();
if(!size.equals("null")){
int intSize = Integer.parseInt(size);
if(style.equals("常规"))
font_new = new Font(name,Font.PLAIN,intSize);
if(style.equals("加粗"))
font_new = new Font(name,Font.BOLD,intSize);
if(style.equals("倾斜"))
font_new = new Font(name,Font.ITALIC,intSize);
if(style.equals("加粗_倾斜"))
font_new = new Font(name,Font.BOLD+Font.ITALIC,intSize);
prve_label.setFont(font_new);
}
}
/**
* 初始化设置字体对话框
* */
public void setInitFont(Font f,JComponent c){
if(f==null)
f = TableModelConest.getInstance().getOtherFont();
component = c;
fontName = f.getFontName();
fontSize = String.valueOf(f.getSize());
//风格
boolean isBold = f.isBold();
boolean isIt = f.isItalic();
boolean isPlain = f.isPlain();
if(isBold&&!isIt)
fontStyle = "加粗";
if(!isBold&&isIt)
fontStyle = "倾斜";
if(isPlain)
fontStyle = "常规";
if(isBold&&isIt)
fontStyle = "加粗_倾斜";
list_font_name.setSelectedValue(fontName,true);
list_font_style.setSelectedValue(fontStyle,true);
list_font_size.setSelectedValue(fontSize,true);
text_font_name.setText(fontName);
text_font_style.setText(fontStyle);
text_font_size.setText(fontSize);
}
/**
* Create the dialog
*/
public TableDialogFont() {
super();
setResizable(false);
setBounds(100, 100, 386, 375);
final JPanel panel = new JPanel();
panel.setLayout(null);
getContentPane().add(panel, BorderLayout.CENTER);
final JLabel label = new JLabel();
label.setText("字体:");
label.setBounds(10, 11, 35, 20);
panel.add(label);
text_font_name = new JTextField();
text_font_name.setBackground(new Color(255, 255, 255));
text_font_name.setEditable(false);
text_font_name.setBounds(10, 32, 120, 21);
panel.add(text_font_name);
final JLabel label_1 = new JLabel();
label_1.setText("字形:");
label_1.setBounds(151, 11, 35, 20);
panel.add(label_1);
text_font_style = new JTextField();
text_font_style.setBackground(new Color(255, 255, 255));
text_font_style.setEditable(false);
text_font_style.setBounds(151, 32, 120, 21);
panel.add(text_font_style);
final JLabel label_2 = new JLabel();
label_2.setText("大小:");
label_2.setBounds(285, 12, 35, 20);
panel.add(label_2);
text_font_size = new JTextField();
text_font_size.setBackground(new Color(255, 255, 255));
text_font_size.setEditable(false);
text_font_size.setBounds(285, 32, 73, 21);
panel.add(text_font_size);
final JLabel label_3 = new JLabel();
label_3.setText("预览:");
label_3.setBounds(151, 192, 35, 20);
panel.add(label_3);
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(component!=null)
component.setFont(getFont());
setVisible(false);
}
});
button.setText("确定");
button.setBounds(154, 296, 100, 25);
panel.add(button);
final JButton button_1 = new JButton();
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
setVisible(false);
}
});
button_1.setText("取消");
button_1.setBounds(258, 296, 100, 25);
panel.add(button_1);
prve_label = new JLabel();
prve_label.setHorizontalAlignment(SwingConstants.CENTER);
prve_label.setOpaque(true);
prve_label.setBackground(new Color(255, 255, 255));
prve_label.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
prve_label.setText("字体样式 AaBbCc");
prve_label.setBounds(151, 218, 207, 63);
panel.add(prve_label);
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 59, 120, 262);
panel.add(scrollPane);
list_font_name = new JList();
list_font_name.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent arg0) {
actionForward(arg0);
}
});
list_font_name.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list_font_name.setModel(new DefaultComboBoxModel(getFontList()));
scrollPane.setViewportView(list_font_name);
list_font_style = new JList();
list_font_style.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent arg0) {
actionForward(arg0);
}
});
list_font_style.setBounds(151, 59, 120, 124);
list_font_style.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list_font_style.setModel(new DefaultComboBoxModel(getFontStyleList()));
panel.add(list_font_style);
final JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(285, 59, 73, 124);
panel.add(scrollPane_1);
list_font_size = new JList();
list_font_size.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent arg0) {
actionForward(arg0);
}
});
list_font_size.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list_font_size.setModel(new DefaultComboBoxModel(getFontSizeList()));
scrollPane_1.setViewportView(list_font_size);
//
}
/**
* 取得字体风格设置
* */
private String[] getFontStyleList(){
return new String[]{"常规","倾斜","加粗","加粗_倾斜"};
}
/**
* 取得字体大小列表
* */
private String[] getFontSizeList(){
return new String[]{"6","8","9","10","11","12","14","16","18","20","22","24","26","28","36","48","72"};
}
/**
* 取得字体列表
* */
private String[] getFontList(){
java.awt.GraphicsEnvironment eq = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNames = eq.getAvailableFontFamilyNames();
return fontNames;
}
}
调用方法
例:为textArea设置字体选择,初始化字体为textArea原来的字体
TableDialogFont fontDlg = new TableDialogFont();
JTextArea textArea = new JTextArea();
Font f = textArea.getFont();
fontDlg.setInitFont(f,textArea);
fontDlg.setTitle("设置字体");
fontDlg.setLocationRelativeTo(null);
fontDlg.setModal(true);
fontDlg.setVisible(true);