package com.dream.notepad;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileDescriptor;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class Notepad extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JMenuBar MB = null;
private JMenu M1 = null;
private JMenu M2 = null;
private JMenu M3 = null;
private JMenu M4 = null;
private JMenu M5 = null;
private JMenuItem MI11 = null;
private JMenuItem MI12 = null;
private JMenuItem MI21 = null;
private JMenuItem MI22 = null;
private JMenuItem MI51 = null;
private JTextArea TA = null;
Toolkit toolKit = Toolkit.getDefaultToolkit();
Clipboard clipboard = toolKit.getSystemClipboard(); // @jve:decl-index=0:
/**
* This method initializes MB
*
* @return javax.swing.JMenuBar
*/
private JMenuBar getMB() {
if (MB == null) {
MB = new JMenuBar();
MB.add(getM1());
MB.add(getM2());
MB.add(getM3());
MB.add(getM4());
MB.add(getM5());
}
return MB;
}
/**
* This method initializes M1
*
* @return javax.swing.JMenu
*/
private JMenu getM1() {
if (M1 == null) {
M1 = new JMenu();
M1.setText("文件(F)");
M1.add(getMI11());
M1.add(getMI12());
M1.addActionListener((ActionListener) this);
}
return M1;
}
/**
* This method initializes M2
*
* @return javax.swing.JMenu
*/
private JMenu getM2() {
if (M2 == null) {
M2 = new JMenu();
M2.setText("编辑(E)");
M2.add(getMI21());
M2.add(getMI22());
}
return M2;
}
/**
* This method initializes M3
*
* @return javax.swing.JMenu
*/
private JMenu getM3() {
if (M3 == null) {
M3 = new JMenu();
M3.setText("格式(O)");
}
return M3;
}
/**
* This method initializes M4
*
* @return javax.swing.JMenu
*/
private JMenu getM4() {
if (M4 == null) {
M4 = new JMenu();
M4.setText("查看(V)");
}
return M4;
}
/**
* This method initializes M5
*
* @return javax.swing.JMenu
*/
private JMenu getM5() {
if (M5 == null) {
M5 = new JMenu();
M5.setText("帮助(H)");
M5.add(getMI51());
}
return M5;
}
/**
* This method initializes MI11
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getMI11() {
if (MI11 == null) {
MI11 = new JMenuItem();
MI11.setText("打开(O)");
MI11.addActionListener(this);
}
return MI11;
}
/**
* This method initializes MI12
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getMI12() {
if (MI12 == null) {
MI12 = new JMenuItem();
MI12.setText("保存(S)");
MI12.addActionListener(this);
}
return MI12;
}
/**
* This method initializes MI21
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getMI21() {
if (MI21 == null) {
MI21 = new JMenuItem();
MI21.setText("复制(C)");
MI21.addActionListener(this);
}
return MI21;
}
/**
* This method initializes MI22
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getMI22() {
if (MI22 == null) {
MI22 = new JMenuItem();
MI22.setText("粘贴(P)");
MI22.addActionListener(this);
}
return MI22;
}
/**
* This method initializes MI51
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getMI51() {
if (MI51 == null) {
MI51 = new JMenuItem();
MI51.setText("关于(A)");
MI51.addActionListener(this);
}
return MI51;
}
/**
* This method initializes TA
*
* @return javax.swing.JTextArea
*/
private JTextArea getTA() {
if (TA == null) {
TA = new JTextArea();
}
return TA;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Notepad thisClass = new Notepad();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
/**
* This is the default constructor
*/
public Notepad() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(450, 360);
this.setJMenuBar(getMB());
this.setContentPane(getJContentPane());
this.setTitle("Notepad");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getTA(), BorderLayout.CENTER);
}
return jContentPane;
}
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
if(e.getActionCommand().equals("打开(O)")){
JFileChooser fc=new JFileChooser();
fc.setDialogTitle("打开文件");
fc.showOpenDialog(null);
String filename=fc.getSelectedFile().getAbsolutePath();
FileReader fr=null;
BufferedReader br=null;
try{
fr=new FileReader(filename);
br=new BufferedReader(fr);
String s="";
String all="";
while((s=br.readLine())!=null){
all+=s+"\r\n";
}
TA.setText(all);
}
catch(Exception ex){
ex.printStackTrace();
}
finally{
try{
br.close();
fr.close();
}
catch(Exception exe){
}
}
}
else if(e.getActionCommand().equals("保存(S)")){
JFileChooser fc1=new JFileChooser();
fc1.setDialogTitle("另存为");
fc1.showOpenDialog(null);
String filename=fc1.getSelectedFile().getAbsolutePath();
FileWriter fw=null;
BufferedWriter bw=null;
try{
fw=new FileWriter(filename);
bw=new BufferedWriter(fw);
bw.write(TA.getText());
}
catch(Exception ex){
ex.printStackTrace();
}
finally{
try {
bw.close();
fw.close();
} catch (IOException ex) {
// TODO 自动生成 catch 块
ex.printStackTrace();
}
}
}
else if(e.getActionCommand().equals("关于(A)")){
About aa=new About(this,"ss");
aa.setModal(true);
aa.setVisible(true);
//fc2.showOpenDialog(null);
}
else if(e.getActionCommand().equals("复制(C)")){
String s = TA.getSelectedText();
StringSelection st= new StringSelection(s);
clipboard.setContents(st,null);
}
else if(e.getActionCommand().equals("粘贴(P)")){
Transferable tf = clipboard.getContents(this);
if(tf==null)
return;
String s="";
try{
s = (String)tf.getTransferData(DataFlavor.stringFlavor);
}
catch(Exception ex){
ex.printStackTrace();
}
TA.replaceRange(s,TA.getSelectionStart(),TA.getSelectionEnd());
}
}
}
//创建关于Dialog
public class About extends JDialog {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel LB1 = null;
/**
* @param owner
*/
public About(Frame owner) {
super(owner);
initialize();
}
public About(Notepad notepad, String string) {
// TODO 自动生成构造函数存根
super(notepad,string);
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300, 200);
this.setTitle("关于记事本");
this.setContentPane(getJContentPane());
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
LB1 = new JLabel();
LB1.setText("作者:臧勇峰 作品:记事本 日期:2011/6/3");
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(LB1, BorderLayout.CENTER);
}
return jContentPane;
}
}
其他功能可以参考上面的代码!希望对java初学者有所帮助!
转载于:https://blog.51cto.com/java009/582144