Previously written Java notepad

Source: Internet
Author: User

Although it was poorly written, it was still written for a long time. If eclipse is to be cleaned up, back it up:


package com.test.notepad;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
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;
import javax.swing.JViewport;
public class Main {
JFrame frame = null;
JMenuBar jmb = null;
JMenu[] jm = null;
JMenuItem[][] jmi = null;
JScrollPane jsp = null;
JViewport port = null;
JTextArea content = null;
JFileChooser fileChooser = null;
File file = null;
boolean isSaved;
public Main() {
isSaved = true;
Frame = new JFrame ("NOTEPAD");
frame.setLayout(new BorderLayout());
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
confirmExit();
}
};
jmb = new JMenuBar();
jm = new JMenu[2];
JM [0] = new JMenu ("file");
JM [1] = new JMenu ("help");
jmb.add(jm[0]);
jmb.add(jm[1]);
jmi = new JMenuItem[2][];
jmi[0] = new JMenuItem[5];
jmi[1] = new JMenuItem[2];
/ / document
JMI [0] [0] = new jmenuitem ("new");
jmi[0][0].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jmi[0][3].setEnabled(true);
content.setEnabled(true);
isSaved = false;
}
};
JMI [0] [1] = new jmenuitem ("open");
jmi[0][1].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int ret = fileChooser.showOpenDialog(frame);
if (ret != JFileChooser.APPROVE_OPTION) {
Return;
}
file = fileChooser.getSelectedFile();
if (file == null) {
Return;
}
content.setEnabled(true);
FileReader fr = null;
Try {
fr = new FileReader(file);
Int i;
StringBuffer sb = new StringBuffer("");
while ((i = fr.read()) != -1) {
sb.append((char) i);
}
content.setText(sb.toString());
jmi[0][2].setEnabled(true);
jmi[0][3].setEnabled(true);
Fr.close ();
isSaved = false;
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
//					e1.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
//					e2.printStackTrace();
}
}
};
JMI [0] [2] = new jmenuitem ("save");
jmi[0][2].setEnabled(false);
jmi[0][2].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Save ();
}
};
JMI [0] [3] = new jmenuitem ("save as");
jmi[0][3].setEnabled(false);
jmi[0][3].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SaveAs ();
}
};
JMI [0] [4] = new jmenuitem ("exit");
jmi[0][4].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
confirmExit();
}
};
jm[0].add(jmi[0][0]);
jm[0].add(jmi[0][1]);
jm[0].add(jmi[0][2]);
jm[0].add(jmi[0][3]);
jm[0].add(jmi[0][4]);
/ / help
JMI [1] [0] = new jmenuitem ("about");
jm[1].add(jmi[1][0]);
jsp = new JScrollPane();
port = jsp.getViewport();
content = new JTextArea();
content.setEnabled(false);
content.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
CheckSave ();
}
};
port.add(content);
fileChooser = new JFileChooser("e:/");
frame.add(jmb, BorderLayout.NORTH);
frame.add(jsp, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(400, 100, 600, 500);
frame.setVisible(true);
}
protected void confirmExit() {
if (isSaved == false) {
If (joptionpane. Showconfirmdialog (null), "are you sure you want to exit without saving? "", "
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
} else{
if(file == null){
SaveAs ();
}else{
Save ();
}
}
}
System.exit(0);
}
protected void save() {
Try {
FileWriter fw = new FileWriter(file);
String str = content.getText();
System.out.println(str);
fw.write(str);
Fw.close ();
isSaved = true;
CheckSave ();
} catch (IOException e1) {
//			e1.printStackTrace();
}
}
protected void saveAs() {
int ret = fileChooser.showSaveDialog(frame);
if (ret != JFileChooser.APPROVE_OPTION) {
Return;
}
file = fileChooser.getSelectedFile();
Try {
FileWriter fw = new FileWriter(file);
fw.write(content.getText());
Fw.close ();
isSaved = true;
CheckSave ();
} catch (IOException e1) {
//			e1.printStackTrace();
}
}
protected void checkSave() {
//		System.out.println("in func checkSave():" + file);
String title = null;
if(file == null){
title = "untitled";
}else{
title = file.getName();
		}
		frame.setTitle(title);
		if(isSaved != true){
			title += " *";
		} else{
			jmi[0][2].setEnabled(true);
		}
	}

	public static void main(String[] args) {
		new Main();
	}
}

There was no comment at the time, but it should be understandable.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.