• 第十三周课程总结


    1.菜单组件

    JMenu与JMenuBar

    若在Java中实现菜单,则可以使用JMenu组件,不过,如果要使用JMenu则首先要了解JMenuBar组件。JMenuBar组件的功能是用来摆放JMenu组件,当建立完许多的JMenu组件之后,需要通过JMenuBar组件来将JMenu组件加入到窗口中

    JMenu的常用方法

    JMenu的常用方法

    JMenuItem

    JMenuItem:为菜单加入菜单项

    JMenuItem的常用方法

    2.JFileChooser

    JFileChooser的常用方法

    记事本

    代码

    package demo;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.InputEvent;
    import java.awt.event.KeyEvent;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import javax.swing.ImageIcon;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.KeyStroke;
    
    public class TextPad implements ActionListener{
    	JFrame frame;
    	JMenuBar bar;
    	JMenu fileMenu,editMenu,formatMenu,lookMenu;
    	JMenuItem newItem,openItem,saveItem,saveAnotherItem,closeItem;
    	ImageIcon newIcon,openIcon,saveIcon,saveAnotherIcon,closeIcon;
    	JMenuItem revocationItem,cutItem,copyItem,stickItem,deleteItem,allItem,timeDateItem;
    	JMenuItem lineItem,typeFaceItem,zoomItem,statusItem;
    	JScrollPane scroll;
    	JTextArea area;
    	JFileChooser chooser;             //文件选择框
    	File file;
    	
    	public TextPad() {
    		frame = new JFrame("记事本");
    		bar=new JMenuBar();
    		fileMenu=new JMenu("文件");
    		editMenu=new JMenu("编辑");
    		formatMenu=new JMenu("格式");
    		lookMenu=new JMenu("查看");
    		newIcon=new ImageIcon("e:"+File.separator+"Test"+File.separator+"小图标"+File.separator+"newIcon.png");
    		openIcon=new ImageIcon("e:"+File.separator+"Test"+File.separator+"小图标"+File.separator+"openIcon.png");
    		saveIcon=new ImageIcon("e:"+File.separator+"Test"+File.separator+"小图标"+File.separator+"saveIcon.png");
    		saveAnotherIcon=new ImageIcon("e:"+File.separator+"Test"+File.separator+"小图标"+File.separator+"saveAnotherIcon.png");
    		closeIcon=new ImageIcon("e:"+File.separator+"Test"+File.separator+"小图标"+File.separator+"closeIcon.png");	
    		area=new JTextArea();
    		scroll=new JScrollPane(area);
    
    		
    		newItem=new JMenuItem("新建(N)",newIcon);
    		openItem=new JMenuItem("打开(O)",openIcon);
    		saveItem=new JMenuItem("保存(S)",saveIcon);
    		saveAnotherItem=new JMenuItem("另存为(D)",saveAnotherIcon);
    		closeItem=new JMenuItem("关闭(X)",closeIcon);
    		
    		revocationItem=new JMenuItem("撤销(Z)");
    		cutItem=new JMenuItem("剪切(T)");
    		copyItem=new JMenuItem("复制( C)");
    		stickItem=new JMenuItem("粘贴(V)");
    	    deleteItem=new JMenuItem("删除(P)");
    		allItem=new JMenuItem("全选(A)");
    		timeDateItem=new JMenuItem("时间日期(Q)");
    		
    		lineItem=new JMenuItem("自动换行(W)");
    		typeFaceItem=new JMenuItem("字体(F)");
    		zoomItem=new JMenuItem("缩放(R)");
    		statusItem=new JMenuItem("状态栏(S)");
    		//加上监视器
    		newItem.addActionListener(this);
    		openItem.addActionListener(this);
    		saveItem.addActionListener(this);
    		saveAnotherItem.addActionListener(this);
    		revocationItem.addActionListener(this);
    		cutItem.addActionListener(this);
    		copyItem.addActionListener(this);		
    		stickItem.addActionListener(this);
    		deleteItem.addActionListener(this);
    		allItem.addActionListener(this);
    		timeDateItem.addActionListener(this);
    		lineItem.addActionListener(this);
    		typeFaceItem.addActionListener(this);
    		zoomItem.addActionListener(this);
    		statusItem.addActionListener(this);
    		
    		
    		fileMenu.add(newItem);
    		fileMenu.add(openItem);
    		fileMenu.add(saveItem);
    		fileMenu.add(saveAnotherItem);
    		fileMenu.addSeparator();
    		fileMenu.add(closeItem);
    		
    		editMenu.add(revocationItem);
    		editMenu.add(cutItem);
    		editMenu.add(copyItem);
    		editMenu.add(stickItem);
    		editMenu.add(deleteItem);
    		editMenu.add(allItem);
    		editMenu.add(timeDateItem);
    		
    		formatMenu.add(lineItem);
    		formatMenu.add(typeFaceItem);
    		lookMenu.add(zoomItem);
    		lookMenu.add(statusItem);
    		
    		newItem.setMnemonic(KeyEvent.VK_N);
    		openItem.setMnemonic(KeyEvent.VK_O);
    		saveItem.setMnemonic(KeyEvent.VK_S);
    		saveAnotherItem.setMnemonic(KeyEvent.VK_D);
    		closeItem.setMnemonic(KeyEvent.VK_X);
    		revocationItem.setMnemonic(KeyEvent.VK_Z);
    		cutItem.setMnemonic(KeyEvent.VK_T);
    		copyItem.setMnemonic(KeyEvent.VK_C);
    		stickItem.setMnemonic(KeyEvent.VK_V);
    		deleteItem.setMnemonic(KeyEvent.VK_P);
    		allItem.setMnemonic(KeyEvent.VK_A);
    		timeDateItem.setMnemonic(KeyEvent.VK_Q);
    		
    		//快捷键
    		newItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_N,InputEvent.CTRL_DOWN_MASK));
    		openItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_O,InputEvent.CTRL_DOWN_MASK));
    		saveItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_S,InputEvent.CTRL_DOWN_MASK));
    		saveAnotherItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_D,InputEvent.CTRL_DOWN_MASK));
    		closeItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_X,InputEvent.CTRL_DOWN_MASK));
    		revocationItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_Z,InputEvent.CTRL_DOWN_MASK));
    		cutItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_T,InputEvent.CTRL_DOWN_MASK));
    		copyItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_C,InputEvent.CTRL_DOWN_MASK));	
    		stickItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_V,InputEvent.CTRL_DOWN_MASK));
    		deleteItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_P,InputEvent.CTRL_DOWN_MASK));
    		allItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_A,InputEvent.CTRL_DOWN_MASK));
    		timeDateItem.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_Q,InputEvent.CTRL_DOWN_MASK));
    
    		
    		bar.add(fileMenu);
    		bar.add(editMenu);
    		bar.add(formatMenu);
    		bar.add(lookMenu);
    		
    		frame.setJMenuBar(bar);
    		frame.add(scroll);
    		frame.setSize(600,400);
    		frame.setLocation(200, 100);
    		frame.setVisible(true);
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent event) {
    		Object obj=event.getSource();
    		if(obj instanceof JMenuItem) {
    			JMenuItem item = (JMenuItem) obj;
    			if(item==newItem) {
    				new TextPad();
    			}else if(item==openItem) {
    				chooser=new JFileChooser();
    				chooser.showOpenDialog(null);
    				file=chooser.getSelectedFile();
    				try {
    					FileInputStream fis=new FileInputStream(file);
    					byte[] b = new byte[fis.available()];
    					fis.read(b);
    					area.append(new String(b));
    					fis.close();
    					
    				} catch (FileNotFoundException e) {
    					e.printStackTrace();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			}else if(item==saveItem) {
    				chooser=new JFileChooser();
    				chooser.showSaveDialog(null);
    				FileOutputStream fos;
    				try {
    					fos = new FileOutputStream(file);
    					byte[] b=area.getText().getBytes();
    					fos.write(b);
    					fos.close();
    				} catch (FileNotFoundException e) {
    					e.printStackTrace();
    				} catch (IOException e) {				
    				}			
    			}else if(item==saveAnotherItem) {
    				chooser=new JFileChooser();
    				chooser.showSaveDialog(null);
    				file=chooser.getSelectedFile();
    				try {
    					if(!file.exists()) {
    						file.createNewFile();
    					}
    					FileOutputStream fos=new FileOutputStream(file);
    					byte[] b=area.getText().getBytes();
    					fos.write(b);
    					fos.close();
    				} catch (FileNotFoundException e) {					
    					e.printStackTrace();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    				
    			}else if(item==closeItem) {
    				System.exit(1);
    			}
    		}
    		
    	}
    }
    
    
    
    package demo;
    
    public class Test {
    
    	public static void main(String[] args) {
    		new TextPad();
    
    	}
    
    }
    
    

    运行截图

  • 相关阅读:
    hdu2302(枚举,大数取模)
    hdu2108(判断凸多边形)
    Codeforces Round #324 (Div. 2) C (二分)
    hdu1798(圆的位置关系)
    hdu1722(gcd)
    alias命令(使用命令别名)
    关于Linux环境变量
    poj1988(并查集)
    Linux基本命令
    Pandas数据规整
  • 原文地址:https://www.cnblogs.com/cxnkkk/p/11920429.html
Copyright © 2020-2023  润新知