基于丁老师第五周作业需求http://blog.csdn.net/dyz1982/article/details/22710247
第一需求修改成简单文本编辑器
修改得好累。。。
不多说,上代码
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
public class TheNewFontSet {
public static void main(String[] args) {
fontsetFrame frame = new fontsetFrame();//实例化窗口
}
}
class fontsetFrame extends JFrame{
static JTextArea ta;//文本域
static newTopPanel tp = new newTopPanel();//顶部面板
static Font f;//字体
static String FontFamily = "宋体";//字体
static int fontType = 0;//字体类型
static int fontSize = 20;//字体大小
public fontsetFrame(){
this.setTitle("简易文本编辑");
ta = new JTextArea("java欢迎你!");//实例化文本域
//添加控件
this.add("North",tp);
this.add(ta);
this.setSize(400, 300);//设置大小
this.setVisible(true);//设置可见
this.setLocationRelativeTo(null);//设置位置居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//响应关闭
}
public static void setFontSize(int x){//设置字体大小
fontSize = x;
trysetfont(FontFamily,fontType,fontSize);
}
public static void setFontFamily(String str){//设置字体系列
FontFamily = str;
trysetfont(FontFamily,fontType,fontSize);
}
public static void setFontType(int x){//设置字体类型
fontType = x;
trysetfont(FontFamily,fontType,fontSize);
}
public static void trysetfont(String FontFamily,int fontType,int fontSize){//设置字体
f = new Font(FontFamily,fontType,fontSize);
ta.setFont(f);
}
}
class newTopPanel extends JPanel implements ItemListener{
static JComboBox list;//字体系列
static JComboBox SizeList;//字体大小
static JComboBox typelist;//字体类型
@SuppressWarnings("unchecked")
public newTopPanel(){
list = new JComboBox(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames());//获取字体
SizeList = new JComboBox();//字体大小
for(int count =1;count<=150;count++)
SizeList.addItem(count);
typelist = new JComboBox();//添加字体类型
typelist.addItem("无");
typelist.addItem("加粗");
typelist.addItem("斜体");
typelist.addItem("加粗+斜体");
//廷加控件
this.add(list);
this.add(SizeList);
this.add(typelist);
//添加响应
SizeList.addItemListener(this);
list.addItemListener(this);
typelist.addItemListener(this);
}
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==list){//响应字体系列
fontsetFrame.setFontFamily(list.getSelectedItem().toString());
}else if(e.getSource()==SizeList){//响应大小
fontsetFrame.setFontSize(Integer.parseInt(SizeList.getSelectedItem().toString()));
}else if(e.getSource()==typelist){//响应类型
if(typelist.getSelectedItem().toString().equals("无")){
fontsetFrame.setFontType(0);
}
else if(typelist.getSelectedItem().toString().equals("加粗")){
fontsetFrame.setFontType(Font.BOLD);
}
else if(typelist.getSelectedItem().toString().equals("斜体")){
fontsetFrame.setFontType(Font.ITALIC);
}
else if(typelist.getSelectedItem().toString().equals("加粗+斜体")){
fontsetFrame.setFontType(Font.BOLD+Font.ITALIC);
}
}
}
}