收藏 分销(赏)

java专业课程设计带注册功能的记事本.doc

上传人:天**** 文档编号:2494273 上传时间:2024-05-30 格式:DOC 页数:17 大小:43.04KB
下载 相关 举报
java专业课程设计带注册功能的记事本.doc_第1页
第1页 / 共17页
java专业课程设计带注册功能的记事本.doc_第2页
第2页 / 共17页
java专业课程设计带注册功能的记事本.doc_第3页
第3页 / 共17页
java专业课程设计带注册功能的记事本.doc_第4页
第4页 / 共17页
java专业课程设计带注册功能的记事本.doc_第5页
第5页 / 共17页
点击查看更多>>
资源描述

1、import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;im

2、port java.io.IOException;import java.io.PrintWriter;import java.io.RandomAccessFile;import javax.swing.*;SuppressWarnings(serial)public class Notepad extends JFrame Notepad notepad; static int size=12; int i,j,k,m,n; JTextArea textarea; String file = ; FileDialog fileload = new FileDialog(this, , Fi

3、leDialog.LOAD); FileDialog filesave = new FileDialog(this, , FileDialog.SAVE); Denglu denglu; public Notepad() super(); denglu=new Denglu(this,true); denglu.setVisible(true); setSize(400, 400); try JMenuBar menuBar=new JMenuBar(); setJMenuBar(menuBar);/ /* / JMenu menu1=new JMenu(); final JMenuItem

4、menuItem1= new JMenuItem(),new JMenuItem(), new JMenuItem(),new JMenuItem() ; for(i=0;imenuItem1.length;i+) menu1.add(menuItem1i); menuItem1i.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) JMenuItem mi1=(JMenuItem)e.getSource(); if(mi1.getActionCommand().equals() o

5、penFile(); else if(mi1.getActionCommand().equals() saveFile(); else if(mi1.getActionCommand().equals() saveAs(); else if(mi1.getActionCommand().equals() System.exit(0); ); / JMenu menu2=new JMenu(); final JMenuItem menuItem2=new JMenuItem(),new JMenuItem(),new JMenuItem() ; for(j=0;jmenuItem2.length

6、;j+) menu2.add(menuItem2j); menuItem2j.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) JMenuItem mi2=(JMenuItem)e.getSource(); if(mi2.getActionCommand().equals() textarea.getSelectedText(); textarea.copy(); else if(mi2.getActionCommand().equals() textarea.getSelecte

7、dText(); textarea.cut(); else if(mi2.getActionCommand().equals() textarea.paste(); ); / JMenu menu3=new JMenu(); JMenu menu3_1=new JMenu(); final JMenuItem menuItem3_1=new JMenuItem(),new JMenuItem() ; for(k=0;kmenuItem3_1.length;k+) menu3_1.add(menuItem3_1k); menuItem3_1k.addActionListener(new Acti

8、onListener() public void actionPerformed(ActionEvent e) JMenuItem mi3_1=(JMenuItem)e.getSource(); if(mi3_1.getActionCommand().equals() textarea.setLineWrap(true); else if(mi3_1.getActionCommand().equals() textarea.setLineWrap(false); ); / JMenu menu3_2=new JMenu(); final JMenuItem menuItem3_2=new JM

9、enuItem(10),new JMenuItem(15),new JMenuItem(20), new JMenuItem(25),new JMenuItem(30),new JMenuItem(40), new JMenuItem(),new JMenuItem(),new JMenuItem() ; for(m=0;mmenuItem3_2.length;m+) menu3_2.add(menuItem3_2m); menuItem3_2m.addActionListener(new ActionListener() public void actionPerformed(ActionE

10、vent e) JMenuItem mi3_2=(JMenuItem)e.getSource(); if(mi3_2.getActionCommand().equals(10) size=10; textarea.setFont(new Font(Default,Font.PLAIN,size); else if(mi3_2.getActionCommand().equals(15) size=15; textarea.setFont(new Font(Default,Font.PLAIN,size); else if(mi3_2.getActionCommand().equals(20) s

11、ize=20; textarea.setFont(new Font(Default,Font.PLAIN,size); else if(mi3_2.getActionCommand().equals(25) size=25; textarea.setFont(new Font(Default,Font.PLAIN,size); else if(mi3_2.getActionCommand().equals(30) size=30; textarea.setFont(new Font(Default,Font.PLAIN,size); else if(mi3_2.getActionCommand

12、().equals(40) size=40; textarea.setFont(new Font(Default,Font.PLAIN,size); else if(mi3_2.getActionCommand().equals() size=size+1; textarea.setFont(new Font(Default,Font.PLAIN,size); else if(mi3_2.getActionCommand().equals() size=size-1; textarea.setFont(new Font(Default,Font.PLAIN,size); else if(mi3

13、_2.getActionCommand().equals() size=12; textarea.setFont(new Font(Default,Font.PLAIN,size); ); / JMenu menu3_3=new JMenu(); final JMenuItem menuItem3_3=new JMenuItem(red), new JMenuItem(black),new JMenuItem(blue), new JMenuItem(orange),new JMenuItem(cyan), new JMenuItem(green) ; for(n=0;nmenuItem3_3

14、.length;n+) menu3_3.add(menuItem3_3n); menuItem3_3n.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) JMenuItem mi3_3=(JMenuItem)e.getSource(); if(mi3_3.getActionCommand().equals(red) textarea.setForeground(Color.red); else if(mi3_3.getActionCommand().equals(black) te

15、xtarea.setForeground(Color.black); else if(mi3_3.getActionCommand().equals(blue) textarea.setForeground(Color.blue); else if(mi3_3.getActionCommand().equals(orange) textarea.setForeground(Color.orange); else if(mi3_3.getActionCommand().equals(cyan) textarea.setForeground(Color.cyan); else if(mi3_3.g

16、etActionCommand().equals(green) textarea.setForeground(Color.green); ); menu3.add(menu3_1); menu3.add(menu3_2); menu3.add(menu3_3); / JMenu menu4=new JMenu(); final JMenuItem menuItem4 =new JMenuItem(); menuItem4.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) JDial

17、og dialog = new JDialog(notepad, , true); dialog.setLayout(new GridLayout(2,1); dialog.setSize(300, 200); dialog.setLocation(40+getX(), 50+getY(); dialog.setTitle(jdk); ImageIcon imageIcon=new ImageIcon(ma.gif); JLabel label=new JLabel(jdk1.6.0_21,imageIcon,JLabel.CENTER); label.setHorizontalTextPos

18、ition(JLabel.CENTER); label.setVerticalTextPosition(JLabel.BOTTOM); label.setToolTipText(); dialog.add(label); dialog.setVisible(true); dialog.setResizable(false); ); menu4.add(menuItem4); /* textarea = new JTextArea(); JScrollPane scrollPane=new JScrollPane(textarea); this.add(scrollPane); / menuBa

19、r.add(menu1); menuBar.add(menu2); menuBar.add(menu3); menuBar.add(menu4); this.setVisible(true); catch (Exception e1) e1.printStackTrace(); public void openFile()/ fileload.setVisible(true); String direct = fileload.getDirectory(); String filename = fileload.getFile(); if (direct != null) & (filenam

20、e != null) file = direct + filename; loadFile(); public void loadFile() / textarea.setText(); super.setTitle(file + -); try BufferedReader br = new BufferedReader(new FileReader(file); String reader = br.readLine(); while (reader != null) textarea.append(reader + n); reader = br.readLine(); br.close

21、(); catch (FileNotFoundException e) System.out.println( + file + ); catch (IOException e) e.printStackTrace(); public void saveFile() / String dangqianwenjian = textarea.getText(); try if (file.isEmpty() filesave.setVisible(true); String direct = filesave.getDirectory(); String filename = filesave.g

22、etFile(); if (direct != null) & (filename != null) file = direct + filename; PrintWriter pw = new PrintWriter(new FileWriter(file); pw.println(dangqianwenjian); pw.flush(); pw.close(); super.setTitle(file + -); else PrintWriter pw = new PrintWriter(new FileWriter(file); pw.println(dangqianwenjian);

23、pw.flush(); pw.close(); catch (IOException e) e.printStackTrace(); public void saveAs() / filesave.setVisible(true); String direct = filesave.getDirectory(); String filename = filesave.getFile(); if (direct != null) & (filename != null) file = direct + filename; saveFile(); super.setTitle(file + -);

24、 public static void main(String args) Notepad notepad=new Notepad(); notepad.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); notepad.setResizable(true); class Denglu extends Dialog implements ActionListener /* * */private static final long serialVersionUID = 1L;Label Lname ,Lpassword; / TextField Tn

25、ame; / JPasswordField Tpassword; /JPasswordField* Panel panel1=new Panel(); / Button Load,login; / boolean b=true; File file1=new File(); File file2=new File(); / Denglu(Frame f,String s,boolean b) super(f,s,b); Lname=new Label ();Lname.setBackground(Color.lightGray);/ Lpassword=new Label ();Lpasswo

26、rd.setBackground(Color.lightGray);/ Tname=new TextField (10); Tpassword=new JPasswordField (10); Load=new Button();Load.setBackground(Color.lightGray); login=new Button();login.setBackground(Color.lightGray); add(panel1,BorderLayout.CENTER); setLayout(new CardLayout(3,2); panel1.add(Lname); panel1.a

27、dd(Tname); panel1.add(Lpassword); panel1.add(Tpassword); panel1.add(Load);Load.addActionListener(this); panel1.add(login);login.addActionListener(this); setSize(200,200); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); public void actionPerformed(Act

28、ionEvent e) / if (e.getSource() = Load) String name1 = Tname.getText(); String password1 = new String(Tpassword.getPassword(); boolean flag = false; String s1=new String30;String s2=new String30;int j=0; int index=0; tryRandomAccessFile in3=new RandomAccessFile(file1,rw); String s=null;int x=0;in3.s

29、eek(0); while(s=in3.readLine()!=null)s1x=s;x=x+1; j=x;in3.close();RandomAccessFile in4=new RandomAccessFile(file2,rw);int y=0;in4.seek(0); while(s=in4.readLine()!=null)s2y=s;y=y+1; in4.close();catch(Exception a)System.out.println(error2);for(int i=0;ij+1;i+)if(name1.equals(s1i)flag=true;index=i;brea

30、k;if(flag) if(password1.equals(s2index)JOptionPane.showMessageDialog(null, !, , JOptionPane.INFORMATION_MESSAGE); setVisible(false); else JOptionPane.showMessageDialog(null, !, ,JOptionPane.ERROR_MESSAGE); else JOptionPane.showMessageDialog(null, !, ,JOptionPane.ERROR_MESSAGE); / if (e.getSource() =

31、 login) String s1=new String30; int j=0; boolean flag = false; String NCmp = Tname.getText(); String PCmp = new String(Tpassword.getPassword(); tryRandomAccessFile in=new RandomAccessFile(file1,rw); String s=null; while(s=in.readLine()!=null)s1j=s;j=j+1; in.close(); catch(Exception a)System.out.prin

32、tln(error); for(int i=0;ij+1;i+)if(NCmp.equals(s1i) flag=true;break; if (flag) JOptionPane.showMessageDialog(null, !, , JOptionPane.ERROR_MESSAGE); else JOptionPane.showMessageDialog(null, !, , JOptionPane.INFORMATION_MESSAGE); tryRandomAccessFile in1=new RandomAccessFile(file1,rwd); if(file1.exists()long length=file1.length();in1.seek(length); in1.writeBytes(NCmp+rn);in1.close(); RandomAccessFile in2=new RandomAccessFile(file2,rwd); if(file2.exists()long length2=file2.length();in2.seek(length2);in2.writeBytes(PCmp+rn);in2.close(); catch(Exception b)System.out.println(error1);

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 考试专区 > 中考

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服