第5周作业-字体设置——略修改成简单文字编辑器

基于丁老师第五周作业需求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);
				
			}
	
		}

	}


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值