11111

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
public class ArtFont extends JFrame implements  ActionListener,ItemListener {
	JComboBox fontType;// 字体样式下拉框
	JComboBox fontSize;// 字体大小下拉框
	JComboBox windowStyle;// 窗体样式下拉框
	JCheckBox boldBx;// 粗体按钮
	JCheckBox italicBx;// 斜体按钮
	JButton colorBtn;// 颜色按钮;
	JButton name;
	String[] fontNames;// 字体名称;
	String[] fontSizes;// 字体大小;
	JLabel label;// 输入提示标签;
	JTextField inputText;// 文字输入框;
	JTextArea txtArea;// 文字显示区;
	JPanel northPanel;// 字体设置;
	JPanel centerPanel;// 显示效果区
	JPanel southPanel;// 样式设置
	Font font;
	int boldStyle, italicStyle, underlineStyle;
	int fontSizeStyle;
	String fontNameStyle;
	Color colorStyle = Color.black;// 设置字体的默认颜色为黑色;
	String[] style = { "默认显示效果", "Windows显示效果", "Unix显示效果" };
 
	public ArtFont() {
		super("字体设置");
		// 设置默认字体
		boldStyle = 0;
		italicStyle = 0;
		underlineStyle = 0;
		fontSizeStyle = 10;
		fontNameStyle = "宋体";
		font = new Font(fontNameStyle, boldStyle + italicStyle, fontSizeStyle);
 
		northPanel = getNorthPanel1();
		centerPanel = getCenterPanel1();
		southPanel = getSouthPanel1();
		// 设置容器;
		Container container = getContentPane();
		container.setLayout(new BorderLayout());
		// 将northPanel添加到窗体的北部
		container.add(northPanel, BorderLayout.NORTH);
		// 将centerPanel添加到窗体的中部
		container.add(centerPanel, BorderLayout.CENTER);
		// 将southPanel添加到窗体的南部
		container.add(southPanel, BorderLayout.SOUTH);
		setSize(500, 300);
		// 将窗体位于屏幕的中央
		setLocationRelativeTo(null);
		setVisible(true);
	}
 
	private JPanel getNorthPanel1() {
		JPanel panel = new JPanel();
		label = new JLabel("输入");
		inputText = new JTextField(10);
		boldBx = new JCheckBox("粗体");
		colorBtn = new JButton("颜色");
		italicBx = new JCheckBox("斜体"); 
		name = new JButton("202202373 张俊杰");
		//注册监视器
		inputText.addActionListener(this);
		colorBtn.addActionListener(this);
		
		boldBx.addItemListener(this);
		italicBx.addItemListener(this);
		
		panel.add(label);
		panel.add(inputText);
		panel.add(boldBx);
		panel.add(colorBtn);
		panel.add(italicBx);
		panel.add(name);
		return panel;
	}
 
	private JPanel getCenterPanel1() {
		JPanel panel = new JPanel();
	    txtArea = new JTextArea(10,10);
	    panel.setLayout(new BorderLayout());
	    panel.add(new JScrollPane(txtArea),BorderLayout.CENTER);
	    return panel;
	}
	private JPanel getSouthPanel1() {
		JPanel panel = new JPanel();	
		//获得系统默认字体
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		fontNames = ge.getAvailableFontFamilyNames();
		fontType = new JComboBox(fontNames);
		//设置字体大小
		fontSizes = new String[63];
		for (int i = 0; i < fontSizes.length; i++) {
			fontSizes[i] = Integer.toString(i+10);
		}
		fontSize = new JComboBox(fontSizes);
    windowStyle = new JComboBox(style) ;
    
    fontSize.addItemListener(this);
    windowStyle.addItemListener(this);
    panel.add(fontType);
    panel.add(fontSize);
    panel.add(windowStyle);
		return panel;
	}
 
 
	public static void main(String args[]) {
		ArtFont artFont = new ArtFont();
		artFont.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
 
 @Override
 public void actionPerformed(ActionEvent e) {
	if(e.getSource()== inputText) {
		txtArea.setText(inputText.getText());
	}else if(e.getSource()== colorBtn) {
		colorStyle=JColorChooser.showDialog(this, "请选择一种颜色",colorStyle );
		colorBtn.setForeground(colorStyle);
		txtArea.setForeground(colorStyle);
	}
	
  }
 
@Override
public void itemStateChanged(ItemEvent e) {
	if (e.getSource() == boldBx) {
	   if(boldBx.isSelected()) {
		   boldStyle = font.BOLD;
	   }else {
		   boldStyle = font.PLAIN;
	   }       
	} else if (e.getSource() == italicBx) {
		if(italicBx.isSelected()) {
			italicStyle = font.ITALIC;
		}else {
			italicStyle = font.PLAIN;
		}
	} else if (e.getSource() == fontType) {
		fontSizeStyle = Integer.parseInt((String) e.getItem());                                                            
	} else if (e.getSource() == fontSize) {
		fontSizeStyle =  Integer.parseInt((String) e.getItem());                                                            
	} else if (e.getSource() == windowStyle) {
		String s = (String) e.getItem();
		String className = "";
		if (s.equals("Windows显示效果"))
			className = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
		else if (s.equals("Unix显示效果"))
			className = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
		else if (s.equals("默认显示效果"))
			className = UIManager.getCrossPlatformLookAndFeelClassName();
		try {
			UIManager.setLookAndFeel(className);
			SwingUtilities.updateComponentTreeUI(this);
		} catch (Exception de) {
			System.out.println("Exception happened!");
		}
	}
	font = new Font(fontNameStyle, boldStyle + italicStyle, fontSizeStyle);
	txtArea.setFont(font);
}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值