码迷,mamicode.com
首页 > 编程语言 > 详细

java之 ------ 图型界面(一)

时间:2015-05-12 11:37:15      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;


public class EditorJFrame extends JFrame implements ActionListener,MouseListener{
	private JTextArea text;//文本区
	private JComboBox comboxName,comboxSize;//组合框
	private JCheckBox chkBold,chkItalic;//字形复选框
	private JRadioButton[] radioColors;//颜色单选按钮数组
	private JPopupMenu popMenu;//快捷菜单
	private boolean isTrue=true;
	private Color color;
	private boolean bool=true;
	
	public EditorJFrame(){
		super("文本编译器");
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		//Dimension dim=this.getToolkit().getScreenSize();//获取屏幕分辨率
		//this.setBounds(dim.width/4, dim.height/4, dim.width/2, dim.height/2);
		this.setSize(800, 600);//设置窗口大小
		this.setLocationRelativeTo(null);//设置窗口位置居中
		text = new JTextArea("welcome....");
		this.getContentPane().add(new JScrollPane(text));
		
		JToolBar toolBar=new JToolBar();//工具栏,默认水平方向
		this.getContentPane().add(toolBar,"North");//工具栏添加到框架内容窗格北边
		GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
		String[] fontsName=ge.getAvailableFontFamilyNames();//获取系统字体
		comboxName =new JComboBox(fontsName);//在组合框显示系统字体
		comboxName.addActionListener(this);//对组合框注册动作事件监听器
		toolBar.add(comboxName);
		
		String[] sizeStr={"20","30","40","50","60"};
		comboxSize=new JComboBox(sizeStr);//字号组合框
		comboxSize.setEditable(true);//设置组合框为可编辑
		comboxSize.addActionListener(this);//组合框注册动作事件监听器
		toolBar.add(comboxSize);
		
		chkBold =new JCheckBox("粗体");chkBold.addActionListener(this);toolBar.add(chkBold);
		chkItalic=new JCheckBox("斜体");chkItalic.addActionListener(this);toolBar.add(chkItalic);
		
		String[] colorStrs={"红","绿","蓝"};
		radioColors=new JRadioButton[colorStrs.length];
		ButtonGroup bgroup=new ButtonGroup();
		for(int i=0;i<radioColors.length;i++){
			radioColors[i]=new JRadioButton(colorStrs[i]);//颜色单选按钮
			radioColors[i].addActionListener(this);
			bgroup.add(radioColors[i]);
			toolBar.add(radioColors[i]);
		}
		
		addMyMenu();
		text.addMouseListener(this);
		this.setVisible(true);
	}
	
	private void addMyMenu() {
		JMenuBar menuBar=new JMenuBar();
		this.setJMenuBar(menuBar);//添加菜单栏
		String[] menuStrs={"文件", "编辑", "帮助"};
		JMenu[] menu=new JMenu[menuStrs.length];
		for(int i=0;i<menu.length;i++){
			menu[i]=new JMenu(menuStrs[i]);//菜单
			menuBar.add(menu[i]);
		}
		menu[0].add(new JMenuItem("打开"));menu[0].add(new JMenuItem("保存"));menu[0].addSeparator();//添加菜单项 并与exit之间用线分隔
		JMenuItem menuItemExit=new JMenuItem("退出");menuItemExit.setActionCommand("exit");menuItemExit.addActionListener(this);
		menu[0].add(menuItemExit);
		
		JMenuItem menuColor=new JMenuItem("颜色");
		menu[1].add(menuColor);menuColor.addActionListener(this);
		
		popMenu=new JPopupMenu();//快捷菜单
		String[] menuItemStrs={ "剪切", "复制", "粘贴" };
		JMenuItem[] popMenuItems=new JMenuItem[menuItemStrs.length];
		for(int i=0;i<popMenuItems.length;i++){
			popMenuItems[i]=new JMenuItem(menuItemStrs[i]);
			popMenu.add(popMenuItems[i]);
			popMenuItems[i].addActionListener(this);
		}
		text.add(popMenu);
	}

	public static void main(String[] args) {
		new EditorJFrame();
	}

	public void mouseClicked(MouseEvent e) {
		if(e.getModifiers()==MouseEvent.BUTTON3_MASK){//显示快捷菜单
			popMenu.show(text, e.getX(), e.getY());
		}
		
	}
	public void mousePressed(MouseEvent e) {
		
	}
	public void mouseReleased(MouseEvent e) {
		
	}
	public void mouseEntered(MouseEvent e) {
	}
	public void mouseExited(MouseEvent e) {
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() instanceof JComboBox || e.getSource() instanceof JCheckBox){
			//控制输入字体大小为有效数字
			try{
				bool=!bool;//防止弹出两次异常
				if(bool){
					return ;
				}
				String fontName=(String) comboxName.getSelectedItem();
				int size=Integer.parseInt((String) comboxSize.getSelectedItem());
				if(size<4 || size>120){
					isTrue=false;
					throw new Exception("SizeException");
				}
				Font font=text.getFont();
				int style=font.getStyle();
				if(e.getSource()==chkBold){//字形整数的第0为表示粗体,第一位表示斜体
					style=style^1;
				}
				if(e.getSource()==chkItalic){
					style=style^2;
				}
				text.setFont(new Font(fontName,style,size));
				bool=true;
			}catch(NumberFormatException e1){
				JOptionPane.showMessageDialog(this, "\""+(String)comboxSize.getSelectedItem()+"\"不能转换成整数,请重新输入!");//  \" 表示引号
			}catch(Exception e2){
				if(e2.getMessage().equalsIgnoreCase("SizeException")){
					JOptionPane.showMessageDialog(this, "字体不合适,请重新输入!");
				}
			}
		}
		if(e.getSource() instanceof JRadioButton){//对单选按钮按下做出反应
			if(e.getSource()==radioColors[0]){
				color=new Color(255,0,0);
			}
			if(e.getSource()==radioColors[1]){
				color=new Color(0,255,0);
			}
			if(e.getSource()==radioColors[2]){
				color=new Color(0,0,255);
			}
			text.setForeground(color);
		}
		if(e.getSource()==comboxSize){
			if(!isTrue){//如果不符合要求  不要将字体大小放进字体大小的组合框
				isTrue=true;
				return ;
			}
			String size=(String)comboxSize.getSelectedItem();
			int i=0,n=comboxSize.getItemCount();
			while(i<n&&Integer.parseInt(size)>=Integer.parseInt((String)comboxSize.getItemAt(i))){
				if(Integer.parseInt(size)==Integer.parseInt((String)comboxSize.getItemAt(i))){
					return ;
				}
				i++;
			}
			comboxSize.insertItemAt(size, i);
		}
	}

}



java之 ------ 图型界面(一)

标签:

原文地址:http://blog.csdn.net/u011479875/article/details/45665165

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!