java课程设计----记事本
要求用jdk作一个记事本。有最基本的打开,保存,跟windows自带的功能差不多,简单一些就可以了是很帅,和我之前下载的一摸一样,我只要简单一些的。有打开,保存,退出,剪...
要求用 jdk作一个记事本。有最基本的 打开,保存,
跟windows自带的功能差不多,简单一些就可以了
是很帅,和我之前下载的一摸一样,我只要简单一些的。
有打开,保存,退出,剪贴,复制,粘贴的就可以了 。
麻烦家了。
如果最后没有答案,那就一楼得分了 展开
跟windows自带的功能差不多,简单一些就可以了
是很帅,和我之前下载的一摸一样,我只要简单一些的。
有打开,保存,退出,剪贴,复制,粘贴的就可以了 。
麻烦家了。
如果最后没有答案,那就一楼得分了 展开
3个回答
展开全部
//package net.src.net;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.color.*;
import java.awt.font.*;
import javax.swing.undo.*;
public class Note extends JFrame
{
boolean isChange=false;
boolean wasChange=false;
JMenuBar menuBar=new JMenuBar();
JMenu menuFile=new JMenu("File");
JMenuItem menuFileOpen=new JMenuItem("Open..");
JMenuItem menuFileSave=new JMenuItem("Save..");
JMenuItem menuFileExit=new JMenuItem("Exit");
JMenu menuEdit=new JMenu("Edit");
JMenuItem menuFileCut=new JMenuItem("Cut");
JMenuItem font=new JMenuItem("Font");
JMenuItem menuFilePaste=new JMenuItem("Paste");
JTextArea fileArea=new JTextArea();
public Note()
{
this.setTitle("记事本");
Toolkit tool=this.getToolkit();//窗口图标!
Image myimage=tool.getImage("戒指.jpg");
this.setIconImage(myimage);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
exit();
}
});
menuFileOpen.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
fileOpen_actionPerformed(e);
}
});
menuFileSave.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
fileSave_actionPerformed(e);
}
});
menuFileExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
fileExit_actionPerformed(e);
}
});
menuFileCut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
fileCut_actionPerformed(e);
}
});
menuFilePaste.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
filePaste_actionPerformed(e);
}
});
font.addActionListener(new ActionListener ()
{
public void actionPerformed(ActionEvent e)
{
fileColor_actionPerformed(e);
}
});
fileArea.getDocument().addDocumentListener(new DocumentListener()
{
public void insertUpdate(DocumentEvent e)
{
wasChange=isChange;
isChange=true;
}
public void removeUpdate(DocumentEvent e)
{
wasChange=isChange;
isChange=true;
}
public void changedUpdate(DocumentEvent e)
{
wasChange=isChange;
isChange=true;
}
});
setSize(500,450);
menuBar.add(menuFile);
menuFile.setMnemonic('F');
menuBar.add(menuEdit);
menuEdit.setMnemonic('E');
menuFile.add(menuFileOpen);
menuFileOpen.setMnemonic('O');//访问健;
menuFileOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));//快捷健;
menuFile.add(menuFileSave);
menuFileSave.setMnemonic('S');
menuFileSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
menuFile.addSeparator();
menuFile.add(menuFileExit);
menuFileExit.setMnemonic('E');
menuEdit.add(menuFileCut);
menuFileCut.setMnemonic('C');
menuFileCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
menuEdit.add(menuFilePaste);
menuFilePaste.setMnemonic('P');
menuFilePaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK));
menuEdit.add(font);
font.setMnemonic('N');
font.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));
this.getContentPane().add(new JScrollPane(fileArea));
setJMenuBar(menuBar);
fileArea.setLineWrap(true);
}
public void fileExit_actionPerformed(ActionEvent e)
{
System.exit(0);
}
public void fileOpen_actionPerformed(ActionEvent e)
{
//以下是filter;
JFileChooser fileChooser=new JFileChooser();
//fileChooser.addChoosableFileFilter(new myFilter("*.txt","Files(*.txt)"));
fileChooser.addChoosableFileFilter(fileChooser.getFileFilter());
if(fileChooser.APPROVE_OPTION!=fileChooser.showOpenDialog(this))return;
//以下是文件读
BufferedReader dataIn=null;
try
{
dataIn=new BufferedReader(new FileReader(fileChooser.getSelectedFile().getPath()));
String c=null;
do
{
c=dataIn.readLine();
if(c!=null)
fileArea.append(c+"\n");
}
while(c!=null);
}
catch(Exception ex)
{
System.out.println("Catch exception:"+ex.toString());
}
}
public void exit()
{
if(isChange==false)
System.exit(1);
else
{
int decision=JOptionPane.showConfirmDialog(this,"The File has Change.\n"+"Do you want to save the change?",
"Notepad",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
if (decision == JOptionPane.YES_OPTION)
{
//以下是将文件写入计算机!
try {
JFileChooser fileSave = new JFileChooser();
fileSave.setDialogTitle("保存文件");
//fileSave.addChoosableFileFilter(new myFilter("*.txt","Files(*.txt)"));
fileSave.addChoosableFileFilter(fileSave.getFileFilter());
if (fileSave.APPROVE_OPTION != fileSave.showSaveDialog(this))
return;
BufferedWriter dataOut = new BufferedWriter(new BufferedWriter(new
FileWriter(fileSave.getSelectedFile())));
String c = null;
do {
String str = fileArea.getText();
dataOut.write(str);
dataOut.close();
}
while (c != null);
}
catch (Exception e2) {
System.out.println("Catch exception:有错误!" + e2.toString());
}
}
else if (decision == JOptionPane.NO_OPTION)
System.exit(1);
else if (decision == JOptionPane.CANCEL_OPTION);
;
}
//innerclass.fileSave_actionPerformed();
}
public void fileSave_actionPerformed(ActionEvent e1)
{
//以下是将文件写入计算机!
try
{
JFileChooser fileSave=new JFileChooser();
fileSave.setDialogTitle("保存文件" );
//fileSave.addChoosableFileFilter(new myFilter("*.txt","Files(*.txt)"));
fileSave.addChoosableFileFilter(fileSave.getFileFilter());
if(fileSave.APPROVE_OPTION!=fileSave.showSaveDialog(this))return;
BufferedWriter dataOut=new BufferedWriter(new BufferedWriter(new FileWriter(fileSave.getSelectedFile()+".txt")));
//RandomAccessFile dataOut=new RandomAccessFile(fileSave.getSelectedFile(),"rw");
String c=null;
do
{
String str=fileArea.getText();
dataOut.write(str);
dataOut.close();
}
while(c!=null);
}
catch(Exception e2)
{
System.out.println("Catch exception:有错误!"+e2.toString());
}
}
public static void main(String arg[])
{
Note nt=new Note();
nt.show();
}
public void fileCut_actionPerformed(ActionEvent e)
{
fileArea.cut();
}
public void filePaste_actionPerformed(ActionEvent e)
{
fileArea.paste();
}
public void fileColor_actionPerformed(ActionEvent e)
{
JColorChooser fileColor=new JColorChooser();
//fileArea.setForeground(fileColor.showDialog(this,"颜色",Color.red));
//fileArea.setSelectionColor(fileColor.showDialog(this,"颜色",Color.red));
fileArea.setSelectionColor(fileColor.showDialog(this,"颜色",Color.black));
}
}
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.color.*;
import java.awt.font.*;
import javax.swing.undo.*;
public class Note extends JFrame
{
boolean isChange=false;
boolean wasChange=false;
JMenuBar menuBar=new JMenuBar();
JMenu menuFile=new JMenu("File");
JMenuItem menuFileOpen=new JMenuItem("Open..");
JMenuItem menuFileSave=new JMenuItem("Save..");
JMenuItem menuFileExit=new JMenuItem("Exit");
JMenu menuEdit=new JMenu("Edit");
JMenuItem menuFileCut=new JMenuItem("Cut");
JMenuItem font=new JMenuItem("Font");
JMenuItem menuFilePaste=new JMenuItem("Paste");
JTextArea fileArea=new JTextArea();
public Note()
{
this.setTitle("记事本");
Toolkit tool=this.getToolkit();//窗口图标!
Image myimage=tool.getImage("戒指.jpg");
this.setIconImage(myimage);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
exit();
}
});
menuFileOpen.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
fileOpen_actionPerformed(e);
}
});
menuFileSave.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
fileSave_actionPerformed(e);
}
});
menuFileExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
fileExit_actionPerformed(e);
}
});
menuFileCut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
fileCut_actionPerformed(e);
}
});
menuFilePaste.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
filePaste_actionPerformed(e);
}
});
font.addActionListener(new ActionListener ()
{
public void actionPerformed(ActionEvent e)
{
fileColor_actionPerformed(e);
}
});
fileArea.getDocument().addDocumentListener(new DocumentListener()
{
public void insertUpdate(DocumentEvent e)
{
wasChange=isChange;
isChange=true;
}
public void removeUpdate(DocumentEvent e)
{
wasChange=isChange;
isChange=true;
}
public void changedUpdate(DocumentEvent e)
{
wasChange=isChange;
isChange=true;
}
});
setSize(500,450);
menuBar.add(menuFile);
menuFile.setMnemonic('F');
menuBar.add(menuEdit);
menuEdit.setMnemonic('E');
menuFile.add(menuFileOpen);
menuFileOpen.setMnemonic('O');//访问健;
menuFileOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));//快捷健;
menuFile.add(menuFileSave);
menuFileSave.setMnemonic('S');
menuFileSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
menuFile.addSeparator();
menuFile.add(menuFileExit);
menuFileExit.setMnemonic('E');
menuEdit.add(menuFileCut);
menuFileCut.setMnemonic('C');
menuFileCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
menuEdit.add(menuFilePaste);
menuFilePaste.setMnemonic('P');
menuFilePaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK));
menuEdit.add(font);
font.setMnemonic('N');
font.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));
this.getContentPane().add(new JScrollPane(fileArea));
setJMenuBar(menuBar);
fileArea.setLineWrap(true);
}
public void fileExit_actionPerformed(ActionEvent e)
{
System.exit(0);
}
public void fileOpen_actionPerformed(ActionEvent e)
{
//以下是filter;
JFileChooser fileChooser=new JFileChooser();
//fileChooser.addChoosableFileFilter(new myFilter("*.txt","Files(*.txt)"));
fileChooser.addChoosableFileFilter(fileChooser.getFileFilter());
if(fileChooser.APPROVE_OPTION!=fileChooser.showOpenDialog(this))return;
//以下是文件读
BufferedReader dataIn=null;
try
{
dataIn=new BufferedReader(new FileReader(fileChooser.getSelectedFile().getPath()));
String c=null;
do
{
c=dataIn.readLine();
if(c!=null)
fileArea.append(c+"\n");
}
while(c!=null);
}
catch(Exception ex)
{
System.out.println("Catch exception:"+ex.toString());
}
}
public void exit()
{
if(isChange==false)
System.exit(1);
else
{
int decision=JOptionPane.showConfirmDialog(this,"The File has Change.\n"+"Do you want to save the change?",
"Notepad",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
if (decision == JOptionPane.YES_OPTION)
{
//以下是将文件写入计算机!
try {
JFileChooser fileSave = new JFileChooser();
fileSave.setDialogTitle("保存文件");
//fileSave.addChoosableFileFilter(new myFilter("*.txt","Files(*.txt)"));
fileSave.addChoosableFileFilter(fileSave.getFileFilter());
if (fileSave.APPROVE_OPTION != fileSave.showSaveDialog(this))
return;
BufferedWriter dataOut = new BufferedWriter(new BufferedWriter(new
FileWriter(fileSave.getSelectedFile())));
String c = null;
do {
String str = fileArea.getText();
dataOut.write(str);
dataOut.close();
}
while (c != null);
}
catch (Exception e2) {
System.out.println("Catch exception:有错误!" + e2.toString());
}
}
else if (decision == JOptionPane.NO_OPTION)
System.exit(1);
else if (decision == JOptionPane.CANCEL_OPTION);
;
}
//innerclass.fileSave_actionPerformed();
}
public void fileSave_actionPerformed(ActionEvent e1)
{
//以下是将文件写入计算机!
try
{
JFileChooser fileSave=new JFileChooser();
fileSave.setDialogTitle("保存文件" );
//fileSave.addChoosableFileFilter(new myFilter("*.txt","Files(*.txt)"));
fileSave.addChoosableFileFilter(fileSave.getFileFilter());
if(fileSave.APPROVE_OPTION!=fileSave.showSaveDialog(this))return;
BufferedWriter dataOut=new BufferedWriter(new BufferedWriter(new FileWriter(fileSave.getSelectedFile()+".txt")));
//RandomAccessFile dataOut=new RandomAccessFile(fileSave.getSelectedFile(),"rw");
String c=null;
do
{
String str=fileArea.getText();
dataOut.write(str);
dataOut.close();
}
while(c!=null);
}
catch(Exception e2)
{
System.out.println("Catch exception:有错误!"+e2.toString());
}
}
public static void main(String arg[])
{
Note nt=new Note();
nt.show();
}
public void fileCut_actionPerformed(ActionEvent e)
{
fileArea.cut();
}
public void filePaste_actionPerformed(ActionEvent e)
{
fileArea.paste();
}
public void fileColor_actionPerformed(ActionEvent e)
{
JColorChooser fileColor=new JColorChooser();
//fileArea.setForeground(fileColor.showDialog(this,"颜色",Color.red));
//fileArea.setSelectionColor(fileColor.showDialog(this,"颜色",Color.red));
fileArea.setSelectionColor(fileColor.showDialog(this,"颜色",Color.black));
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |