import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class TextEditorFrame extends JFrame
{
File file = null;
TextEditorFrame()
{
initTextPane();
initAboutDialog();
initToolBar();
initMenu();
}
void initTextPane()
{
getContentPane().add(new JScrollPane(text));
}
JTextPane text = new JTextPane();
JFileChooser filechooser = new JFileChooser();
JDialog about = new JDialog(this);
JMenuBar menubar = new JMenuBar();
JMenu[] menus = new JMenu[]
{ new JMenu("File"), new JMenu("Edit"),
new JMenu("Help")
};
JMenuItem menuitems[][] = new JMenuItem[][]
{
{ new JMenuItem("New"), new JMenuItem("Open"), new JMenuItem("Save"), new JMenuItem("Exit") },
{new JMenuItem("Copy"), new JMenuItem("Cut"), new JMenuItem("Paste") }, {new JMenuItem("Help") }
};
void initMenu()
{
for (int i = 0; i < menus.length; i++)
{
menubar.add(menus[i]);
for (int j = 0; j < menuitems[i].length; j++)
{
menus[i].add(menuitems[i][j]);
menuitems[i][j].addActionListener(action);
}
}
this.setJMenuBar(menubar);
}
ActionListener action = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JMenuItem mi = (JMenuItem) e.getSource();
String id = mi.getText();
if (id.equals("New"))
{
text.setText("");
file = null;
} else if (id.equals("Open"))
{
if (file != null)
filechooser.setSelectedFile(file);
int returnVal = filechooser
.showOpenDialog(TextEditorFrame.this);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
file = filechooser.getSelectedFile();
openFile();
}
} else if (id.equals("Save"))
{
if (file != null)
filechooser.setSelectedFile(file);
int returnVal = filechooser
.showSaveDialog(TextEditorFrame.this);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
file = filechooser.getSelectedFile();
saveFile();
}
} else if (id.equals("Exit"))
{
TextEditorFrame f = new TextEditorFrame();
int s = JOptionPane.showConfirmDialog(f, "Really want to exit?", "Exit",
JOptionPane.YES_NO_CANCEL_OPTION);
if (s == JOptionPane.YES_OPTION)
System.exit(0);
} else if (id.equals("Cut"))
{
text.cut();
} else if (id.equals("Copy"))
{
text.copy();
} else if (id.equals("Paste"))
{
text.paste();
} else if (id.equals("Help"))
{
about.setSize(200,150);
about.setTitle("Help");
about.show();
}
}
};
void saveFile()
{
try
{
FileWriter fw = new FileWriter(file);
fw.write(text.getText());
fw.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
void openFile()
{
try
{
FileReader fr = new FileReader(file);
int len = (int) file.length();
char[] buffer = new char[len];
fr.read(buffer, 0, len);
fr.close();
text.setText(new String(buffer));
} catch (Exception e)
{
e.printStackTrace();
}
}
void initAboutDialog()
{
about.getContentPane().add(new JLabel("This is a TextEditor programme"));
about.setModal(true);
}
JToolBar toolbar = new JToolBar();
JButton[] buttons = new JButton[]
{
new JButton("", new ImageIcon("fuzhi.jpg")),
new JButton("", new ImageIcon("jianqie.jpg")),
new JButton("", new ImageIcon("zhantie.jpg"))
};
void initToolBar()
{
for (int i = 0; i < buttons.length; i++)
toolbar.add(buttons[i]);
buttons[0].setToolTipText("Copy");
buttons[0].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
text.copy();
}
});
buttons[1].setToolTipText("Cut");
buttons[1].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
text.cut();
}
});
buttons[2].setToolTipText("Paste");
buttons[2].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
text.paste();
}
});
this.getContentPane().add(toolbar, BorderLayout.NORTH);
}
public static void main(String args[])
{
TextEditorFrame f = new TextEditorFrame();
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
TextEditorFrame f = new TextEditorFrame();
int s = JOptionPane.showConfirmDialog(f, "Really want to exit?", "Exit",
JOptionPane.YES_NO_OPTION);
if (s == JOptionPane.YES_OPTION)
System.exit(0);
}
});
f.setTitle("TextEditor");
f.setSize(750, 550);
f.setVisible(true);
}
}