JAVA的字体格式设置对话框代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
import java.awt.*;
import javax.swing.*;
/**
* 字体格式设置对话框
*/
public class FontFormat extends Dialog {
private JLabel nameLb;
private JLabel styleLb;
private JLabel sizeLb;
private JTextField nameTx;
private JTextField styleTx;
private JTextField sizeTx;
private JList nameLt;
private JList styleLt;
private JList sizeLt;
private JScrollPane jScrollPane1;
private JScrollPane jScrollPane2;
private JScrollPane jScrollPane3;
private JButton approve;
private JButton cancel;
private JFrame frame;
public Font font;
//字体‘逻辑名’集
private String[] fontNameSet = new String[]
{"Arial","Arial Black","Arial Narrow","Book Antiqua","Bookman Old Style",
"Bookshelf Symbol 7","Century Gothic","Comic Sans Ms","Courier New",
"Dialog","DialogInput","Dotum","DotumChe","FangSong_GB2312",
"Franklin Gothic Medium","FZShuTi","FZYaoTi","Garamond","Georgia",
"Gulim","GulimChe","Haettenschweiler","Impact","KaiTi_GB2312",
"Kingsoft Phonetic Plain","LiSu","Lucida Bright","Lucida Console",
"Lucida Sans","Lucida Sans Typewriter","Lucida Sans Unicode","Marlett",
"Microsoft Sans Serif","MingLiU","Monospaced","Monotype Corsiva",
"MS Gothic","MS PGothic","MS Reference Sans Serif","MS Reference Specialty",
"MS UI Gothic","MV Boli","NSimSun","Palatino Linotype","PMingLiU",
"SansSerif","Serif","SimHei","SimSun","Simsun (Founder Extended)",
"Simsun-PUA","STCaiyun","STFangsong","STXihei","TXingkai","STXinwei",
"STZhongsong","Symbol","Tahoma","Times New Roman","Trebuchet MS","Verdana",
"Webdings","Wingdings","Wingdings 2","Wingdings 3","YouYuan"};
//字体‘样式’集的字符串数组
private String[] fontStyleSet =
{"Plain","Italic","Bold","Bold Italic"};
//字体‘样式’集的常量数组
private Integer[] fontCon = {Font.PLAIN,Font.ITALIC,Font.BOLD,Font.BOLD|Font.ITALIC};
//字体‘大小’集
private String[] fontSizeSet =
{"6","7","8","9","10","11","12","14","16",
"18","20","22","24","26","28","36","48","72"};
/**
* 无参构造器
*/
public FontFormat() {
initGUI();
}
/**
* 传入父窗口引用的带参构造器
*/
public FontFormat(JFrame frame){
this.frame = frame; //父窗口中必须有一个public的Font对象
initGUI();
}
/**
* 字体格式选择器的界面初始化
*/
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setTitle("字体格式");
getContentPane().setLayout(null);
{
nameLb = new JLabel();
getContentPane().add(nameLb);
nameLb.setText("字体:");
nameLb.setBounds(34, 24, 90, 26);
nameLb.setFont(new java.awt.Font("SimSun",1,14));
}
{
styleLb = new JLabel();
getContentPane().add(styleLb);
styleLb.setText("字型:");
styleLb.setBounds(160, 24, 82, 23);
styleLb.setFont(new java.awt.Font("SimSun",1,14));
}
{
sizeLb = new JLabel();
getContentPane().add(sizeLb);
sizeLb.setText("大小:");
sizeLb.setBounds(273, 23, 54, 24);
sizeLb.setFont(new java.awt.Font("SimSun",1,14));
}
{
nameTx = new JTextField();
getContentPane().add(nameTx);
nameTx.setBounds(34, 54, 90, 22);
}
{
styleTx = new JTextField();
getContentPane().add(styleTx);
styleTx.setBounds(160, 54, 70, 21);
}
{
sizeTx = new JTextField();
getContentPane().add(sizeTx);
sizeTx.setBounds(273, 54, 54, 22);
}
{
approve = new JButton();
getContentPane().add(approve);
approve.setText("确定");
approve.setBounds(69, 240, 91, 28);
approve.setFont(new java.awt.Font("KaiTi_GB2312",1,16));
approve.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
approveActionPerformed(evt);
}
});
}
{
cancel = new JButton();
getContentPane().add(cancel);
cancel.setText("取消");
cancel.setBounds(203, 240, 91, 28);
cancel.setFont(new java.awt.Font("KaiTi_GB2312",1,16));
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
cancelActionPerformed(evt);
}
});
}
{
jScrollPane1 = new JScrollPane();
getContentPane().add(jScrollPane1);
jScrollPane1.setBounds(34, 74, 90, 104);
{
ListModel fontNameModel =
new DefaultComboBoxModel(fontNameSet);
nameLt = new JList();
jScrollPane1.setViewportView(nameLt);
nameLt.setModel(fontNameModel);
nameLt.setBounds(274, 193, 90, 86);
nameLt.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
nameLt.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
nameLtMouseClicked(evt);
}
});
}
}
{
jScrollPane2 = new JScrollPane();
getContentPane().add(jScrollPane2);
jScrollPane2.setBounds(160, 74, 70, 103);
{
ListModel fontStyleModel =
new DefaultComboBoxModel(fontStyleSet);
styleLt = new JList();
jScrollPane2.setViewportView(styleLt);
styleLt.setModel(fontStyleModel);
styleLt.setBounds(310, 215, 70, 102);
styleLt.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
styleLt.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
styleLtMouseClicked(evt);
}
});
}
}
{
jScrollPane3 = new JScrollPane();
getContentPane().add(jScrollPane3);
jScrollPane3.setBounds(273, 75, 54, 100);
{
ListModel fontSizeModel =
new DefaultComboBoxModel(fontSizeSet);
sizeLt = new JList();
jScrollPane3.setViewportView(sizeLt);
sizeLt.setModel(fontSizeModel);
sizeLt.setBounds(300, 218, 54, 102);
sizeLt.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
sizeLt.addMouseListener(new MouseAdap
ter() {
public void mouseClicked(MouseEvent evt) {
sizeLtMouseClicked(evt);
}
});
}
}
//设置默认字体格式为父窗口font对向的字体格式
if(frame.font==null){
nameTx.setText("SimSun");
styleTx.setText(fontStyleSet[0]);
sizeTx.setText("12");
nameLt.setSelectedValue("SimSun", true);
styleLt.setSelectedIndex(0);
sizeLt.setSelectedValue("12", true);
}
else{
int idxStyle = 0;
for(int i=0;i
idxStyle = i;
}
nameTx.setText(frame.font.getName());
styleTx.setText(fontStyleSet[idxStyle]);
sizeTx.setText(""+frame.font.getSize());
nameLt.setSelectedValue(frame.font.getName(), true);
styleLt.setSelectedIndex(idxStyle);
sizeLt.setSelectedValue(""+frame.font.getSize(), true);
}
pack();
this.setSize(374, 337);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 字体逻辑名列表的鼠标单击事件
*/
private void nameLtMouseClicked(MouseEvent evt) {
nameTx.setText(nameLt.getSelectedValue().toString());
}
/**
* 字体样式列表的鼠标单击事件
*/
private void styleLtMouseClicked(MouseEvent evt) {
styleTx.setText(styleLt.getSelectedValue().toString());
}
/**
* 字体大小列表的鼠标点击事件
*/
private void sizeLtMouseClicked(MouseEvent evt) {
sizeTx.setText(sizeLt.getSelectedValue().toString());
}
/**
* 确定按钮的触发事件
*/
private void approveActionPerformed(ActionEvent evt) {
String name = nameTx.getText();
int style = fontCon[styleLt.getSelectedIndex()];
int size = Integer.parseInt(sizeTx.getText());
font = new Font(name,style,size);
frame.font = font; //父窗口的Font对象
this.dispose();
}
/**
* 取消按钮的触发事件
*/
private void cancelActionPerformed(ActionEvent evt) {
this.dispose();
}
}