展开全部
哥们,我的号被封了,还有两天才能解封。我又申请了个号。怎么联系你啊?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
希望下面写的代码对你有用。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Notepad extends JFrame implements ActionListener {
JMenuItem miOpen = new JMenuItem("Open");
JMenuItem miSave = new JMenuItem("Save");
JMenuItem miExit = new JMenuItem("Exit");
JTextArea taMain = new JTextArea();
JFileChooser fileChooser = new JFileChooser();
File file;
public Notepad() {
this.setSize(600, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu("File");
menuBar.add(menuFile);
menuFile.add(miOpen);
menuFile.add(miSave);
menuFile.addSeparator();
menuFile.add(miExit);
this.getContentPane().add(menuBar, BorderLayout.NORTH);
this.getContentPane().add(new JScrollPane(taMain));
miOpen.addActionListener(this);
miSave.addActionListener(this);
miExit.addActionListener(this);
this.setVisible(true);
}
public static void main(String[] args) {
new Notepad();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == miOpen) {
doOpen();
} else if (e.getSource() == miSave) {
doSave();
} else if (e.getSource() == miExit) {
System.exit(0);
}
}
private void doOpen() {
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
taMain.setText("");
file = fileChooser.getSelectedFile();
char[] buffer = new char[1024];
int count;
try {
FileReader reader = new FileReader(file);
while ((count = reader.read(buffer)) > 0) {
taMain.append(String.valueOf(buffer, 0, count));
}
reader.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "open file error!");
}
}
}
private void doSave() {
if (file == null) {
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile();
} else {
return;
}
}
try {
if (!file.exists()) {
file.createNewFile();
}
FileWriter writer = new FileWriter(file);
writer.write(taMain.getText());
writer.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "save file error!");
}
}
}
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Notepad extends JFrame implements ActionListener {
JMenuItem miOpen = new JMenuItem("Open");
JMenuItem miSave = new JMenuItem("Save");
JMenuItem miExit = new JMenuItem("Exit");
JTextArea taMain = new JTextArea();
JFileChooser fileChooser = new JFileChooser();
File file;
public Notepad() {
this.setSize(600, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu("File");
menuBar.add(menuFile);
menuFile.add(miOpen);
menuFile.add(miSave);
menuFile.addSeparator();
menuFile.add(miExit);
this.getContentPane().add(menuBar, BorderLayout.NORTH);
this.getContentPane().add(new JScrollPane(taMain));
miOpen.addActionListener(this);
miSave.addActionListener(this);
miExit.addActionListener(this);
this.setVisible(true);
}
public static void main(String[] args) {
new Notepad();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == miOpen) {
doOpen();
} else if (e.getSource() == miSave) {
doSave();
} else if (e.getSource() == miExit) {
System.exit(0);
}
}
private void doOpen() {
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
taMain.setText("");
file = fileChooser.getSelectedFile();
char[] buffer = new char[1024];
int count;
try {
FileReader reader = new FileReader(file);
while ((count = reader.read(buffer)) > 0) {
taMain.append(String.valueOf(buffer, 0, count));
}
reader.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "open file error!");
}
}
}
private void doSave() {
if (file == null) {
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile();
} else {
return;
}
}
try {
if (!file.exists()) {
file.createNewFile();
}
FileWriter writer = new FileWriter(file);
writer.write(taMain.getText());
writer.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "save file error!");
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package 记事本;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.awt.event.ActionListener;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;
import java.awt.event.*;
public class Notepad extends JFrame{
private static final long serialVersionUID=1l;
private JPanel contentpane;
private JMenuBar bar=new JMenuBar();
private JMenu file=new JMenu();
private JMenuItem newfile=new JMenuItem();
private JMenuItem openfile=new JMenuItem();
private JMenuItem savefile=new JMenuItem();
private JMenuItem saveasfile=new JMenuItem();
private JMenuItem quitfile=new JMenuItem();
private JMenu edit=new JMenu();
private JMenuItem cutedit=new JMenuItem();
private JMenuItem copyedit=new JMenuItem();
private JMenuItem pastedit=new JMenuItem();
private JMenuItem selectalledit=new JMenuItem();
private JMenuItem deletedit=new JMenuItem();
private JMenu color=new JMenu();
private JMenuItem selectcolor=new JMenuItem();
private JMenu help=new JMenu();
private JMenuItem about=new JMenuItem();
private JPopupMenu popupmenu=new JPopupMenu();
private JMenuItem popupcut=new JMenuItem();
private JMenuItem popupcopy=new JMenuItem();
private JMenuItem popuppaste=new JMenuItem();
private JMenuItem popupselectall=new JMenuItem();
private JMenuItem popupdelete=new JMenuItem();
private JTextArea textarea=new JTextArea();
private JScrollPane scroller=new JScrollPane();
private Clipboard clipboard=Toolkit.getDefaultToolkit().getSystemClipboard();
public Notepad(){
initcontainer();
initcomponent();
initlistener();
}
private void initcontainer(){
contentpane=(JPanel)this.getContentPane();
contentpane.setLayout(new BorderLayout());
contentpane.add(textarea,BorderLayout.CENTER);
contentpane.add("Center",scroller);
this.setBounds(100,100,500,500);
this.setFont(new Font("宋体",Font.PLAIN,12));
this.setTitle("无标题-记事本");
this.setJMenuBar(bar);
}
private void initcomponent(){
buildmenubar();
buildpopupmenu();
buildtextarea();
}
private void buildmenubar(){
initmenubar();
initmenufile();
initmenuedit();
initcolorhelp();
}
private void initmenubar(){
bar.add(file);
bar.add(edit);
bar.add(color);
bar.add(help);
}
private void initmenufile(){
file.setText("文件");
file.add(newfile);
file.add(openfile);
file.add(savefile);
file.add(saveasfile);
file.addSeparator();
file.add(quitfile);
newfile.setText("新建(N)");
newfile.setMnemonic(KeyEvent.VK_N);
newfile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,Event.CTRL_MASK));
openfile.setText("打开(O)");
openfile.setMnemonic(KeyEvent.VK_O);
openfile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.CTRL_MASK));
savefile.setText("保存(S)");
savefile.setMnemonic(KeyEvent.VK_S);
savefile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, Event.CTRL_MASK));
saveasfile.setText("另存为…(A)");
saveasfile.setMnemonic(KeyEvent.VK_A);
quitfile.setText("退出");
}
private void initmenuedit(){
edit.setText("编辑");
edit.add(cutedit);
edit.add(copyedit);
edit.add(pastedit);
edit.addSeparator();
edit.add(selectalledit);
edit.add(deletedit);
cutedit.setText("剪切(X)");
cutedit.setMnemonic(KeyEvent.VK_X);
cutedit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK));
copyedit.setText("复制(C)");
copyedit.setMnemonic(KeyEvent.VK_C);
copyedit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK));
pastedit.setText("粘贴(V)");
pastedit.setMnemonic(KeyEvent.VK_V);
pastedit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK));
selectalledit.setText("全选(A)");
selectalledit.setMnemonic(KeyEvent.VK_A);
selectalledit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, Event.CTRL_MASK));
deletedit.setText("删除(D)");
deletedit.setMnemonic(KeyEvent.VK_D);
deletedit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, Event.CTRL_MASK));
}
private void initcolorhelp(){
color.setText("颜色");
color.add(selectcolor);
selectcolor.setText("选择颜色……");
help.setText("帮助");
help.add(about);
about.setText("关于……");
}
private void buildpopupmenu(){
popupmenu.add(popupcut);
popupmenu.add(popupcopy);
popupmenu.add(popuppaste);
popupmenu.addSeparator();
popupmenu.add(popupdelete);
popupmenu.add(popupselectall);
popupcut.setText("剪切(X)");
popupcopy.setText("复制(C)");
popuppaste.setText("粘贴(V)");
popupdelete.setText("删除(D)");
popupselectall.setText("全选(A");
}
private void buildtextarea(){
textarea.setRows(20);
textarea.setColumns(20);
textarea.setDoubleBuffered(false);
textarea.setToolTipText("<--翔鹰工作室提供-->");
textarea.setVerifyInputWhenFocusTarget(true);
textarea.setText("<--翔鹰工作室提供-->");
textarea.add(popupmenu);
scroller.getViewport().add(textarea);
}
private void initlistener(){
Actlistener listener=new Actlistener();
newfile.addActionListener(listener);
savefile.addActionListener(listener);
saveasfile.addActionListener(listener);
openfile.addActionListener(listener);
quitfile.addActionListener(listener);
cutedit.addActionListener(listener);
copyedit.addActionListener(listener);
pastedit.addActionListener(listener);
deletedit.addActionListener(listener);
selectalledit.addActionListener(listener);
selectcolor.addActionListener(listener);
about.addActionListener(listener);
textarea.addMouseListener(new Mouselistener());
this.addWindowListener(new Windowlistener());
popupcut.addActionListener(listener);
popupcopy.addActionListener(listener);
popuppaste.addActionListener(listener);
popupdelete.addActionListener(listener);
popupselectall.addActionListener(listener);
}
class Actlistener implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource()==newfile){
newfile();
}else if(e.getSource()==openfile){
openfile();
}else if(e.getSource()==savefile){
savefile();
}else if(e.getSource()==saveasfile){
saveasfile();
}else if(e.getSource()==quitfile){
quitfile();
}else if(e.getSource()==cutedit){
cutedit();
}else if(e.getSource()==copyedit){
copyedit();
}else if(e.getSource()==pastedit){
pastedit();
}else if(e.getSource()==deletedit){
deletedit();
}else if(e.getSource()==selectalledit){
selectalledit();
}else if(e.getSource()==selectcolor){
selectcolor();
}else if(e.getSource()==about){
about();
}
}
}
class Mouselistener extends MouseAdapter{
public void mouseRelease(MouseEvent e){
if(e.isPopupTrigger()){
popupmenu.show((JTextArea)e.getSource(),e.getX(),e.getY());
}
}
}
class Windowlistener extends WindowAdapter{
public void windowClosing(WindowEvent arg0){
quitfile();
}
}
String filename=null;
boolean isSaved=false;
private void newfile(){
if(isSaved){
this.textarea.setText("");
this.textarea.setFocusable(true);
this.setTitle("无标题-记事本");
}else{
int result =JOptionPane.showConfirmDialog(this,"想保存文件么?","记事本",JOptionPane.YES_NO_CANCEL_OPTION);
if(JOptionPane.OK_OPTION==result){
saveasfile();
}else if(JOptionPane.NO_OPTION==result){
this.textarea.setText("");
this.textarea.setFocusable(true);
this.setTitle("无标题-记事本");
}else{
}
}
}
private void openfile(){
String openfilename="";
JFileChooser filechooser=new JFileChooser();
if(isSaved){
try{
if(JFileChooser.APPROVE_OPTION==filechooser.showOpenDialog(this)){
openfilename=filechooser.getSelectedFile().getPath();
File file=new File(openfilename);
int length=(int)file.length();
int num=0;
FileReader reader=new FileReader(file);
char data[]=new char[length];
while(reader.ready()){
num+=reader.read(data,num,length-num);
}
reader.close();
textarea.setText(new String(data,0,num));
filename=openfilename;
this.setTitle(filename.substring(filename.lastIndexOf("\\")+1));
this.repaint();
isSaved=false;
}
}catch(Exception e){
JOptionPane.showMessageDialog(this, "打开文件时出错!","错误",JOptionPane.ERROR_MESSAGE);
}
}else{
int result=JOptionPane.showConfirmDialog(this,"想保存文件么?","记事本",JOptionPane.YES_NO_CANCEL_OPTION);
if(JOptionPane.OK_OPTION==result){
savefile();
openfile();
}else if(JOptionPane.NO_OPTION==result){
isSaved=true;
openfile();
}else{
}
}
}
private void savefile(){
if(filename==null){
saveasfile();
}else{
if(!isSaved){
if(filename.length()!=0){
try{
File file=new File(filename);
FileWriter filewriter=new FileWriter(file);
filewriter.write(textarea.getText());
filewriter.close();
isSaved=true;
this.setTitle(filename.substring(filename.lastIndexOf("\\")+1));
this.repaint();
}catch (Exception e){
JOptionPane.showMessageDialog(this, "保存文件时出错!","错误",JOptionPane.ERROR_MESSAGE);
}
}else{
saveasfile();
}
}
}
}
private void saveasfile(){
JFileChooser filechooser=new JFileChooser();
if(JFileChooser.APPROVE_OPTION==filechooser.showSaveDialog(this)){
filename=filechooser.getSelectedFile().getPath();
savefile();
}
}
private void quitfile(){
if(!isSaved){
int result=JOptionPane.showConfirmDialog(this,"想保存文件么?","记事本",JOptionPane.YES_NO_CANCEL_OPTION);
if(JOptionPane.OK_OPTION==result){
savefile();
System.exit(0);
}else if(JOptionPane.NO_OPTION==result){
System.exit(0);
}else{
}
}else{
System.exit(0);
}
}
private void cutedit(){
try{
String str=this.textarea.getSelectedText();
if(str.length()!=0){
StringSelection s=new StringSelection(str);
clipboard.setContents(s,s);
this.textarea.replaceRange("",this.textarea.getSelectionStart(),this.textarea.getSelectionEnd());
isSaved=false;
}
}catch (Exception e){
JOptionPane.showMessageDialog(this,"剪切出错!","错误",JOptionPane.ERROR_MESSAGE);
}
}
private void copyedit(){
try{
String str=this.textarea.getSelectedText();
if(str.length()!=0){
StringSelection s=new StringSelection(str);
clipboard.setContents(s,s);
}
}catch(Exception e){
JOptionPane.showMessageDialog(this,"复制出错!","错误",JOptionPane.ERROR_MESSAGE);
}
}
private void pastedit(){
try{
Transferable tr=clipboard.getContents(this);
if(tr!=null){
String s=(String)tr.getTransferData(DataFlavor.stringFlavor);
if(s!=null);{
textarea.replaceRange(s,textarea.getSelectionStart(),textarea.getSelectionEnd());
}
isSaved=false;
}
}catch(Exception e){
JOptionPane.showMessageDialog(this,"粘贴时出错!","错误",JOptionPane.ERROR_MESSAGE);
}
}
private void deletedit(){
textarea.replaceRange("",textarea.getSelectionStart(),textarea.getSelectionEnd());
isSaved=false;
}
private void selectalledit(){
textarea.setSelectionStart(0);
textarea.setSelectionEnd(this.textarea.getText().length());
}
private void selectcolor(){
Color color=textarea.getForeground();
JColorChooser colorchooser=new JColorChooser();
colorchooser.setColor(color);
textarea.setForeground(JColorChooser.showDialog(textarea,"选择颜色",color));
}
private void about(){
JOptionPane.showMessageDialog(this,"记事本由翔鹰工作室提供","Sinner",JOptionPane.INFORMATION_MESSAGE);
}
/**
* @param args
*/
public static void main(String[] args) {
Notepad demo=new Notepad();
demo.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.awt.event.ActionListener;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;
import java.awt.event.*;
public class Notepad extends JFrame{
private static final long serialVersionUID=1l;
private JPanel contentpane;
private JMenuBar bar=new JMenuBar();
private JMenu file=new JMenu();
private JMenuItem newfile=new JMenuItem();
private JMenuItem openfile=new JMenuItem();
private JMenuItem savefile=new JMenuItem();
private JMenuItem saveasfile=new JMenuItem();
private JMenuItem quitfile=new JMenuItem();
private JMenu edit=new JMenu();
private JMenuItem cutedit=new JMenuItem();
private JMenuItem copyedit=new JMenuItem();
private JMenuItem pastedit=new JMenuItem();
private JMenuItem selectalledit=new JMenuItem();
private JMenuItem deletedit=new JMenuItem();
private JMenu color=new JMenu();
private JMenuItem selectcolor=new JMenuItem();
private JMenu help=new JMenu();
private JMenuItem about=new JMenuItem();
private JPopupMenu popupmenu=new JPopupMenu();
private JMenuItem popupcut=new JMenuItem();
private JMenuItem popupcopy=new JMenuItem();
private JMenuItem popuppaste=new JMenuItem();
private JMenuItem popupselectall=new JMenuItem();
private JMenuItem popupdelete=new JMenuItem();
private JTextArea textarea=new JTextArea();
private JScrollPane scroller=new JScrollPane();
private Clipboard clipboard=Toolkit.getDefaultToolkit().getSystemClipboard();
public Notepad(){
initcontainer();
initcomponent();
initlistener();
}
private void initcontainer(){
contentpane=(JPanel)this.getContentPane();
contentpane.setLayout(new BorderLayout());
contentpane.add(textarea,BorderLayout.CENTER);
contentpane.add("Center",scroller);
this.setBounds(100,100,500,500);
this.setFont(new Font("宋体",Font.PLAIN,12));
this.setTitle("无标题-记事本");
this.setJMenuBar(bar);
}
private void initcomponent(){
buildmenubar();
buildpopupmenu();
buildtextarea();
}
private void buildmenubar(){
initmenubar();
initmenufile();
initmenuedit();
initcolorhelp();
}
private void initmenubar(){
bar.add(file);
bar.add(edit);
bar.add(color);
bar.add(help);
}
private void initmenufile(){
file.setText("文件");
file.add(newfile);
file.add(openfile);
file.add(savefile);
file.add(saveasfile);
file.addSeparator();
file.add(quitfile);
newfile.setText("新建(N)");
newfile.setMnemonic(KeyEvent.VK_N);
newfile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,Event.CTRL_MASK));
openfile.setText("打开(O)");
openfile.setMnemonic(KeyEvent.VK_O);
openfile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.CTRL_MASK));
savefile.setText("保存(S)");
savefile.setMnemonic(KeyEvent.VK_S);
savefile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, Event.CTRL_MASK));
saveasfile.setText("另存为…(A)");
saveasfile.setMnemonic(KeyEvent.VK_A);
quitfile.setText("退出");
}
private void initmenuedit(){
edit.setText("编辑");
edit.add(cutedit);
edit.add(copyedit);
edit.add(pastedit);
edit.addSeparator();
edit.add(selectalledit);
edit.add(deletedit);
cutedit.setText("剪切(X)");
cutedit.setMnemonic(KeyEvent.VK_X);
cutedit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK));
copyedit.setText("复制(C)");
copyedit.setMnemonic(KeyEvent.VK_C);
copyedit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK));
pastedit.setText("粘贴(V)");
pastedit.setMnemonic(KeyEvent.VK_V);
pastedit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK));
selectalledit.setText("全选(A)");
selectalledit.setMnemonic(KeyEvent.VK_A);
selectalledit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, Event.CTRL_MASK));
deletedit.setText("删除(D)");
deletedit.setMnemonic(KeyEvent.VK_D);
deletedit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, Event.CTRL_MASK));
}
private void initcolorhelp(){
color.setText("颜色");
color.add(selectcolor);
selectcolor.setText("选择颜色……");
help.setText("帮助");
help.add(about);
about.setText("关于……");
}
private void buildpopupmenu(){
popupmenu.add(popupcut);
popupmenu.add(popupcopy);
popupmenu.add(popuppaste);
popupmenu.addSeparator();
popupmenu.add(popupdelete);
popupmenu.add(popupselectall);
popupcut.setText("剪切(X)");
popupcopy.setText("复制(C)");
popuppaste.setText("粘贴(V)");
popupdelete.setText("删除(D)");
popupselectall.setText("全选(A");
}
private void buildtextarea(){
textarea.setRows(20);
textarea.setColumns(20);
textarea.setDoubleBuffered(false);
textarea.setToolTipText("<--翔鹰工作室提供-->");
textarea.setVerifyInputWhenFocusTarget(true);
textarea.setText("<--翔鹰工作室提供-->");
textarea.add(popupmenu);
scroller.getViewport().add(textarea);
}
private void initlistener(){
Actlistener listener=new Actlistener();
newfile.addActionListener(listener);
savefile.addActionListener(listener);
saveasfile.addActionListener(listener);
openfile.addActionListener(listener);
quitfile.addActionListener(listener);
cutedit.addActionListener(listener);
copyedit.addActionListener(listener);
pastedit.addActionListener(listener);
deletedit.addActionListener(listener);
selectalledit.addActionListener(listener);
selectcolor.addActionListener(listener);
about.addActionListener(listener);
textarea.addMouseListener(new Mouselistener());
this.addWindowListener(new Windowlistener());
popupcut.addActionListener(listener);
popupcopy.addActionListener(listener);
popuppaste.addActionListener(listener);
popupdelete.addActionListener(listener);
popupselectall.addActionListener(listener);
}
class Actlistener implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource()==newfile){
newfile();
}else if(e.getSource()==openfile){
openfile();
}else if(e.getSource()==savefile){
savefile();
}else if(e.getSource()==saveasfile){
saveasfile();
}else if(e.getSource()==quitfile){
quitfile();
}else if(e.getSource()==cutedit){
cutedit();
}else if(e.getSource()==copyedit){
copyedit();
}else if(e.getSource()==pastedit){
pastedit();
}else if(e.getSource()==deletedit){
deletedit();
}else if(e.getSource()==selectalledit){
selectalledit();
}else if(e.getSource()==selectcolor){
selectcolor();
}else if(e.getSource()==about){
about();
}
}
}
class Mouselistener extends MouseAdapter{
public void mouseRelease(MouseEvent e){
if(e.isPopupTrigger()){
popupmenu.show((JTextArea)e.getSource(),e.getX(),e.getY());
}
}
}
class Windowlistener extends WindowAdapter{
public void windowClosing(WindowEvent arg0){
quitfile();
}
}
String filename=null;
boolean isSaved=false;
private void newfile(){
if(isSaved){
this.textarea.setText("");
this.textarea.setFocusable(true);
this.setTitle("无标题-记事本");
}else{
int result =JOptionPane.showConfirmDialog(this,"想保存文件么?","记事本",JOptionPane.YES_NO_CANCEL_OPTION);
if(JOptionPane.OK_OPTION==result){
saveasfile();
}else if(JOptionPane.NO_OPTION==result){
this.textarea.setText("");
this.textarea.setFocusable(true);
this.setTitle("无标题-记事本");
}else{
}
}
}
private void openfile(){
String openfilename="";
JFileChooser filechooser=new JFileChooser();
if(isSaved){
try{
if(JFileChooser.APPROVE_OPTION==filechooser.showOpenDialog(this)){
openfilename=filechooser.getSelectedFile().getPath();
File file=new File(openfilename);
int length=(int)file.length();
int num=0;
FileReader reader=new FileReader(file);
char data[]=new char[length];
while(reader.ready()){
num+=reader.read(data,num,length-num);
}
reader.close();
textarea.setText(new String(data,0,num));
filename=openfilename;
this.setTitle(filename.substring(filename.lastIndexOf("\\")+1));
this.repaint();
isSaved=false;
}
}catch(Exception e){
JOptionPane.showMessageDialog(this, "打开文件时出错!","错误",JOptionPane.ERROR_MESSAGE);
}
}else{
int result=JOptionPane.showConfirmDialog(this,"想保存文件么?","记事本",JOptionPane.YES_NO_CANCEL_OPTION);
if(JOptionPane.OK_OPTION==result){
savefile();
openfile();
}else if(JOptionPane.NO_OPTION==result){
isSaved=true;
openfile();
}else{
}
}
}
private void savefile(){
if(filename==null){
saveasfile();
}else{
if(!isSaved){
if(filename.length()!=0){
try{
File file=new File(filename);
FileWriter filewriter=new FileWriter(file);
filewriter.write(textarea.getText());
filewriter.close();
isSaved=true;
this.setTitle(filename.substring(filename.lastIndexOf("\\")+1));
this.repaint();
}catch (Exception e){
JOptionPane.showMessageDialog(this, "保存文件时出错!","错误",JOptionPane.ERROR_MESSAGE);
}
}else{
saveasfile();
}
}
}
}
private void saveasfile(){
JFileChooser filechooser=new JFileChooser();
if(JFileChooser.APPROVE_OPTION==filechooser.showSaveDialog(this)){
filename=filechooser.getSelectedFile().getPath();
savefile();
}
}
private void quitfile(){
if(!isSaved){
int result=JOptionPane.showConfirmDialog(this,"想保存文件么?","记事本",JOptionPane.YES_NO_CANCEL_OPTION);
if(JOptionPane.OK_OPTION==result){
savefile();
System.exit(0);
}else if(JOptionPane.NO_OPTION==result){
System.exit(0);
}else{
}
}else{
System.exit(0);
}
}
private void cutedit(){
try{
String str=this.textarea.getSelectedText();
if(str.length()!=0){
StringSelection s=new StringSelection(str);
clipboard.setContents(s,s);
this.textarea.replaceRange("",this.textarea.getSelectionStart(),this.textarea.getSelectionEnd());
isSaved=false;
}
}catch (Exception e){
JOptionPane.showMessageDialog(this,"剪切出错!","错误",JOptionPane.ERROR_MESSAGE);
}
}
private void copyedit(){
try{
String str=this.textarea.getSelectedText();
if(str.length()!=0){
StringSelection s=new StringSelection(str);
clipboard.setContents(s,s);
}
}catch(Exception e){
JOptionPane.showMessageDialog(this,"复制出错!","错误",JOptionPane.ERROR_MESSAGE);
}
}
private void pastedit(){
try{
Transferable tr=clipboard.getContents(this);
if(tr!=null){
String s=(String)tr.getTransferData(DataFlavor.stringFlavor);
if(s!=null);{
textarea.replaceRange(s,textarea.getSelectionStart(),textarea.getSelectionEnd());
}
isSaved=false;
}
}catch(Exception e){
JOptionPane.showMessageDialog(this,"粘贴时出错!","错误",JOptionPane.ERROR_MESSAGE);
}
}
private void deletedit(){
textarea.replaceRange("",textarea.getSelectionStart(),textarea.getSelectionEnd());
isSaved=false;
}
private void selectalledit(){
textarea.setSelectionStart(0);
textarea.setSelectionEnd(this.textarea.getText().length());
}
private void selectcolor(){
Color color=textarea.getForeground();
JColorChooser colorchooser=new JColorChooser();
colorchooser.setColor(color);
textarea.setForeground(JColorChooser.showDialog(textarea,"选择颜色",color));
}
private void about(){
JOptionPane.showMessageDialog(this,"记事本由翔鹰工作室提供","Sinner",JOptionPane.INFORMATION_MESSAGE);
}
/**
* @param args
*/
public static void main(String[] args) {
Notepad demo=new Notepad();
demo.setVisible(true);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询