java 记事本全选_java 编写的记事本程序怎么实现复制 黏贴 剪切 全选的功能 ?...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

import javax.swing.*;import java.awt.event.*;import java.io.*;import java.awt.*;public class zuoye10_4_5{ public static void main(String args[]){ FirstWindow win=new FirstWindow(""); }}class FirstWindow extends JFrame implements ActionListener{ JMenuBar menubar; JMenu menu,menu1,menu2; JMenuItem dakai,baocun,quanxuan,tuichu,itemCopy,itemCut,itemPaste,zidonghuanhang,Backcolor,Forecolor; JTextArea text; JFileChooser fileChooser; FirstWindow(String s){ setSize(800,500); setLocation(120,120); setVisible(true); fileChooser=new JFileChooser("c:/"); //buttonFile=new JButton("打开文件"); menubar=new JMenuBar(); menu=new JMenu("文件"); dakai=new JMenuItem("打开"); baocun=new JMenuItem("保存"); quanxuan=new JMenuItem("全选"); tuichu=new JMenuItem("退出"); menu1=new JMenu("编辑"); itemCopy=new JMenuItem("复制"); itemCut=new JMenuItem("剪切"); itemPaste=new JMenuItem("粘贴"); menu2=new JMenu("格式"); zidonghuanhang=new JMenuItem("自动换行"); Backcolor=new JMenuItem("背景颜色"); Forecolor=new JMenuItem("字体颜色"); dakai.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK)); baocun.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK)); quanxuan.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK)); itemCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK)); itemCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK)); itemPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK)); text=new JTextArea(10,10); menu.add(dakai); menu.addSeparator(); menu.add(baocun); menu.addSeparator(); menu.add(quanxuan); menu.addSeparator(); menu.add(tuichu); menubar.add(menu); menu1.add(itemCopy); menu1.addSeparator(); menu1.add(itemCut); menu1.addSeparator(); menu1.add(itemPaste); menubar.add(menu1); menu2.add(zidonghuanhang); menu2.addSeparator(); menu2.add(Backcolor); menu2.addSeparator(); menu2.add(Forecolor); menubar.add(menu2); add(text); //add(buttonFile,BorderLayout.NORTH); add(new JScrollPane(text),BorderLayout.CENTER); setJMenuBar(menubar); dakai.addActionListener(this); baocun.addActionListener(this); quanxuan.addActionListener(this); tuichu.addActionListener(this); itemCopy.addActionListener(this); itemCut.addActionListener(this); itemPaste.addActionListener(this); zidonghuanhang.addActionListener(this); Backcolor.addActionListener(this); Forecolor.addActionListener(this); //buttonFile.addActionListener(this); validate(); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } public void actionPerformed(ActionEvent e){ try{ if(e.getSource()==dakai){ text.setText(null); int n=fileChooser.showOpenDialog(null); if(n==JFileChooser.APPROVE_OPTION){ File file=fileChooser.getSelectedFile(); FileReader rfile=new FileReader(file); BufferedReader inTwo=new BufferedReader(rfile); String s=null; while((s=inTwo.readLine())!=null) //text.setText(s); text.append(s+'\n'); } } else if(e.getSource()==baocun){ //File file=new File("hello.text"); //FileWriter tofile=new FileWriter("hello.txt"); //char b[]=text.getText().toCharArray();//获取文本域中的内容并且存在数组B中; //tofile.write(b,0,b.length); //tofile.close(); int n=fileChooser.showSaveDialog(null); if(n==JFileChooser.APPROVE_OPTION){ File file=fileChooser.getSelectedFile(); FileWriter wFile=new FileWriter(file); wFile.write(text.getText()); wFile.close(); } } else if(e.getSource()==quanxuan) text.selectAll(); else if(e.getSource()==tuichu) System.exit(0); else if(e.getSource()==itemCopy) text.copy(); else if(e.getSource()==itemCut) text.cut(); else if(e.getSource()==itemPaste) text.paste(); else if(e.getSource()==zidonghuanhang) text.setLineWrap(true); else if(e.getSource()==Backcolor){ Color newColor=JColorChooser.showDialog(this,"调色板",Backcolor.getBackground()); if(newColor!=null) text.setBackground(newColor); } else if(e.getSource()==Forecolor){ Color newColor=JColorChooser.showDialog(this,"调色板",Forecolor.getBackground()); if(newColor!=null) text.setForeground(newColor); } } catch(IOException ee){} }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值