📄 notepad_font_frame.java
字号:
/**
*
*/
package com.edu.sccp.snail.notepad.view;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/**
* @author 韩丽丽
* @version 2008-1-5
*/
public class Notepad_Font_Frame extends JInternalFrame implements ListSelectionListener, ActionListener{
private static final long serialVersionUID = -6096922286275217743L;
private JList notepad_list_name;
private JList notepad_list_type;
private JList notepad_list_size;
private JLabel notepad_label_name;
private JLabel notepad_label_type;
private JLabel notepad_label_size;
private JLabel notepad_label_preview;
private JLabel notepad_label_previewmessage;
private JPanel notepad_panel_frame;
private JPanel notepad_panel_name;
private JPanel notepad_panel_type;
private JPanel notepad_panel_size;
private JScrollPane notepad_jscrollpane_name;
private JScrollPane notepad_jscrollpane_type;
private JScrollPane notepad_jscrollpane_size;
private JTextField notepad_jtxtfield_name;
private JTextField notepad_jtxtfield_type;
private JTextField notepad_jtxtfield_size;
private JButton notepad_jbutton_ok;
private JButton notepad_jbutton_cansole;
private Notepad_newFile notepad_newFile;
private Font font;
/**
* 构造方法
* @author 韩丽丽
* @version 2008-1-5
*/
public Notepad_Font_Frame( Notepad_newFile newFile){
super("字体设置",true,true,true,true);
notepad_newFile = newFile;
init_notepad_font_frame();
show_notepad_font_frame();
}
public Notepad_Font_Frame(){
super("字体设置",true,true,true,true);
init_notepad_font_frame();
show_notepad_font_frame();
}
/**
* @value 向窗体添加控件,设置控件的属性
* @author 韩丽丽
* @version 2008-1-5
*/
private void init_notepad_font_frame(){
String notepad_string_name[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
String notepad_string_type[] = {"常规","斜体","粗体","粗斜体"};
String notepad_string_size[] = {"8","9","10","11","12","14","16","18","20","22","24","26","28","36","48",
"72","初号","小初","一号","小一","二号","小二","三号","小三","四号","小四","五号",
"小五","六号","小六","七号","八号"};
//生成控件
notepad_list_name = new JList(notepad_string_name);
notepad_list_type = new JList(notepad_string_type);
notepad_list_size = new JList(notepad_string_size);
notepad_jscrollpane_name = new JScrollPane(notepad_list_name);
notepad_jscrollpane_type = new JScrollPane(notepad_list_type);
notepad_jscrollpane_size = new JScrollPane(notepad_list_size);
notepad_label_name = new JLabel("字体:");
notepad_label_type = new JLabel("字形:");
notepad_label_size = new JLabel("大小:");
notepad_label_preview = new JLabel("预览:");
notepad_label_previewmessage = new JLabel("明天会更好",SwingConstants.LEFT);
notepad_jtxtfield_name = new JTextField(140);
notepad_jtxtfield_type = new JTextField(90);
notepad_jtxtfield_size = new JTextField(80);
notepad_jbutton_ok = new JButton("确定");
notepad_jbutton_cansole = new JButton("取消");
notepad_panel_frame = new JPanel();
notepad_panel_name = new JPanel();
notepad_panel_type = new JPanel();
notepad_panel_size = new JPanel();
//初始化控件
notepad_list_name.setVisibleRowCount(5);
notepad_list_type.setVisibleRowCount(5);
notepad_list_size.setVisibleRowCount(5);
notepad_set_previewmessage();
notepad_panel_frame.setLayout(null);
//设置控件的位置和大小
notepad_label_name.setBounds(10,10,40,20);
notepad_jtxtfield_name.setBounds(10,35,210,20);
notepad_panel_name.setBounds(10, 51, 210, 110);
notepad_label_type.setBounds(230, 10, 40, 20);
notepad_jtxtfield_type.setBounds(230,35,45,20);
notepad_panel_type.setBounds(230, 51, 45, 110);
notepad_label_size.setBounds(285,10,40,20);
notepad_jtxtfield_size.setBounds(285,35,50,20);
notepad_panel_size.setBounds(285,51,50,110);
notepad_jbutton_ok.setBounds(345, 35, 70,20);
notepad_jbutton_cansole.setBounds(345,60, 70, 20);
notepad_label_preview.setBounds(10, 170, 40, 20);
notepad_label_previewmessage.setBounds(50, 170, 350, 100);
//给控件添加监听器
notepad_list_name.addListSelectionListener(this);
notepad_list_type.addListSelectionListener(this);
notepad_list_size.addListSelectionListener(this);
notepad_jbutton_ok.addActionListener(this);
notepad_jbutton_cansole.addActionListener(this);
//设置默认字体
String notepad_string_fontname = notepad_string_get_fontname();
int notepad_int_fonttype = notepad_int_get_fontType();
int notepad_int_fontsize = notepad_int_get_fontsize();
font = new Font(notepad_string_fontname,notepad_int_fonttype,notepad_int_fontsize);
//向panel添加控件
notepad_panel_name.add(notepad_jscrollpane_name);
notepad_panel_type.add(notepad_jscrollpane_type);
notepad_panel_size.add(notepad_jscrollpane_size);
notepad_panel_frame.add(notepad_jtxtfield_name);
notepad_panel_frame.add(notepad_jtxtfield_type);
notepad_panel_frame.add(notepad_jtxtfield_size);
notepad_panel_frame.add(notepad_label_name);
notepad_panel_frame.add(notepad_label_type);
notepad_panel_frame.add(notepad_label_size);
notepad_panel_frame.add(notepad_panel_name);
notepad_panel_frame.add(notepad_panel_type);
notepad_panel_frame.add(notepad_panel_size);
notepad_panel_frame.add(notepad_jbutton_ok);
notepad_panel_frame.add(notepad_jbutton_cansole);
notepad_panel_frame.add(notepad_label_preview);
notepad_panel_frame.add(notepad_label_previewmessage);
this.getContentPane().add(notepad_panel_frame);
//对jlist控件和jtextfield倥控件内容进行初始化
notepad_list_name.setSelectedValue("宋体",true);
notepad_list_type.setSelectedIndex(0);
notepad_list_size.setSelectedValue("小四", true);
notepad_jtxtfield_name.setText(String.valueOf(notepad_list_name.getSelectedValue()));
notepad_jtxtfield_type.setText(String.valueOf(notepad_list_type.getSelectedValue()));
notepad_jtxtfield_size.setText(String.valueOf(notepad_list_size.getSelectedValue()));
this.addInternalFrameListener(new InternalFrameAdapter(){
public void internalFrameClosed(InternalFrameEvent arg0) {
try {
setClosed(true);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}
});
}
/**
* @value 显示窗体,并对窗体做初始化
* @author 韩丽丽
* @version 2008-1-5
*/
private void show_notepad_font_frame(){
this.setSize(450,300);
this.setResizable(false);
this.show();
}
private void notepad_set_previewmessage(){//设置预览信息字体
String notepad_string_fontname = notepad_string_get_fontname();
int notepad_int_fonttype = notepad_int_get_fontType();
int notepad_int_fontsize = notepad_int_get_fontsize();
font = new Font(notepad_string_fontname,notepad_int_fonttype,notepad_int_fontsize);
notepad_label_previewmessage.setFont(font);
}
public void valueChanged(ListSelectionEvent le) {//list控件的监听,处理
if(le.getSource() == notepad_list_name){
notepad_jtxtfield_name.setText(String.valueOf(notepad_list_name.getSelectedValue()));
notepad_set_previewmessage();
}
if(le.getSource() == notepad_list_type){
notepad_jtxtfield_type.setText(String.valueOf(notepad_list_type.getSelectedValue()));
notepad_set_previewmessage();
}
if(le.getSource() == notepad_list_size){
notepad_jtxtfield_size.setText(String.valueOf(notepad_list_size.getSelectedValue()));
notepad_set_previewmessage();
}
}
/**
* @value 返回字体的名称
* @author 韩丽丽
* @version 2008-1-5
*/
private String notepad_string_get_fontname(){
return notepad_jtxtfield_name.getText();
}
/**
* @value 返回字体大小
* @author 韩丽丽
* @version 2008-1-5
*/
private int notepad_int_get_fontsize(){
int notepad_int_fontsize = 0;
String notepad_string_fontsize = notepad_jtxtfield_size.getText();
if(notepad_string_fontsize.equals("八号"))
notepad_int_fontsize = 4;
if(notepad_string_fontsize.equals("七号"))
notepad_int_fontsize = 5;
if(notepad_string_fontsize.equals("小六"))
notepad_int_fontsize = 6;
if(notepad_string_fontsize.equals("六号"))
notepad_int_fontsize = 7;
if(notepad_string_fontsize.equals("8"))
notepad_int_fontsize = 8;
if(notepad_string_fontsize.equals("9") || notepad_string_fontsize.equals("小五"))
notepad_int_fontsize = 9;
if(notepad_string_fontsize.equals("10") || notepad_string_fontsize.equals("五号"))
notepad_int_fontsize = 10;
if(notepad_string_fontsize.equals("11"))
notepad_int_fontsize = 11;
if(notepad_string_fontsize.equals("12") || notepad_string_fontsize.equals("小四"))
notepad_int_fontsize = 12;
if(notepad_string_fontsize.equals("14") || notepad_string_fontsize.equals("四号"))
notepad_int_fontsize = 14;
if(notepad_string_fontsize.equals("小三"))
notepad_int_fontsize = 15;
if(notepad_string_fontsize.equals("16") || notepad_string_fontsize.equals("三号"))
notepad_int_fontsize = 16;
if(notepad_string_fontsize.equals("18") || notepad_string_fontsize.equals("小二"))
notepad_int_fontsize = 18;
if(notepad_string_fontsize.equals("20"))
notepad_int_fontsize = 20;
if(notepad_string_fontsize.equals("22") || notepad_string_fontsize.equals("二号"))
notepad_int_fontsize = 22;
if(notepad_string_fontsize.equals("24") || notepad_string_fontsize.equals("小一"))
notepad_int_fontsize = 24;
if(notepad_string_fontsize.equals("26") || notepad_string_fontsize.equals("一号"))
notepad_int_fontsize = 26;
if(notepad_string_fontsize.equals("28"))
notepad_int_fontsize = 28;
if(notepad_string_fontsize.equals("36") || notepad_string_fontsize.equals("小初"))
notepad_int_fontsize = 36;
if(notepad_string_fontsize.equals("初号"))
notepad_int_fontsize = 40;
if(notepad_string_fontsize.equals("48"))
notepad_int_fontsize = 48;
if(notepad_string_fontsize.equals("72"))
notepad_int_fontsize = 72;
return notepad_int_fontsize;
}
/**
* @value 返回字形
* @author 韩丽丽
* @version 2008-1-5
*/
private int notepad_int_get_fontType(){
String notepad_string_type = notepad_jtxtfield_type.getText();
int notepad_int_fontType = 0;
if(notepad_string_type.equals("常规"))
notepad_int_fontType = Font.PLAIN;
if(notepad_string_type.equals("粗体"))
notepad_int_fontType = Font.BOLD;
if(notepad_string_type.equals("斜体"))
notepad_int_fontType = Font.ITALIC;
if(notepad_string_type.equals("粗斜体"))
notepad_int_fontType = Font.BOLD + Font.ITALIC;
return notepad_int_fontType;
}
/**
* @value 对文本框中的内容变字体
* @author 韩丽丽
* @version 2008-1-5
* @return user select the Font
*/
private void notepad_deal_font(){//对文本框中的内容变zi'ti
//try{
//if(notepad_newFile.jta.getSelectedText() == null)
notepad_newFile.jta.setFont(font);
/*else{
int notepad_start = notepad_newFile.jta.getSelectionStart();
int notepad_end = notepad_newFile.jta.getSelectionEnd();
font.canDisplayUpTo(notepad_newFile.jta.getText(),notepad_start,notepad_end);
}*/
//}catch(IllegalArgumentException e ){
//e.printStackTrace();
//}
}
/**
* @value 命令按钮的监听,处理
* @author 韩丽丽
* @version 2008-1-5
* @return user select the Font
*/
public void actionPerformed(ActionEvent ae) {
if(ae.getSource() == notepad_jbutton_ok){
int notepad_int_fontType = notepad_int_get_fontType();
int notepad_int_fontsize = notepad_int_get_fontsize();
String notepad_string_fontname = notepad_string_get_fontname();
font = new Font(notepad_string_fontname,notepad_int_fontType,notepad_int_fontsize);//生成新的字体
notepad_deal_font();
}
if(ae.getSource() == notepad_jbutton_cansole){
this.setVisible(false);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -