网上看到的一个记事本例子,展示了基本功能,不过如果进入程序后点保存,然后点取消会出现空指针警告。
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.BoxLayout;
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.KeyStroke;
public class TextEditor extends JFrame {
String tempString="";
public static void main(String args[]) {
try {
TextEditor frame = new TextEditor();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public TextEditor() {
super();
getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
setBounds(100, 100, 499, 396);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
final JMenu File_menu = new JMenu();
File_menu.setFont(new Font("", Font.PLAIN, 12));
File_menu.setText("文件");
menuBar.add(File_menu);
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setAutoscrolls(true);
getContentPane().add(scrollPane);
final JTextArea jta=new JTextArea();
jta.setBackground(Color.WHITE);
scrollPane.add(jta);
jta.setLineWrap(true);
scrollPane.setViewportView(jta);
final JMenuItem Open_menu = new JMenuItem();
Open_menu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
Open_menu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser fileChooser=new JFileChooser(".");
fileChooser.setDialogTitle("打开文件");
fileChooser.addChoosableFileFilter(fileChooser.getFileFilter());
fileChooser.showOpenDialog(new javax.swing.JFrame());
File selectFile=fileChooser.getSelectedFile();
try{
BufferedReader filein=new BufferedReader(new FileReader(selectFile.getPath()));
String c=null;
do{
c=filein.readLine();
if(c!=null)
{
jta.append(c+"/n");
}
}
while(c!=null);
filein.close();
}
catch(Exception e){
e.printStackTrace();
}
}
});
Open_menu.setFont(new Font("", Font.PLAIN, 12));
Open_menu.setText("打开 ");
File_menu.add(Open_menu);
//保存文件
final JMenuItem Sava_menu = new JMenuItem();
Sava_menu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser filesaveChooser=new JFileChooser(".");
filesaveChooser.setDialogTitle("保存文件");
filesaveChooser.showOpenDialog(new javax.swing.JFrame());
try{
BufferedWriter fileout=new BufferedWriter(new FileWriter(filesaveChooser.getSelectedFile()));
String c=null;
do{
String context=jta.getText();
fileout.write(context);
fileout.close();
}
while(c!=null);
}
catch(Exception e){
e.printStackTrace();
}
}
});
Sava_menu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
Sava_menu.setFont(new Font("", Font.PLAIN, 12));
Sava_menu.setText("保存 ");
File_menu.add(Sava_menu);
final JMenuItem Quit_menu = new JMenuItem();
Quit_menu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
Quit_menu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, 0));
Quit_menu.setFont(new Font("", Font.PLAIN, 12));
Quit_menu.setText("关闭 ");
File_menu.add(Quit_menu);
final JMenu Edit_menu = new JMenu();
Edit_menu.setFont(new Font("", Font.PLAIN, 12));
Edit_menu.setText("编辑");
menuBar.add(Edit_menu);
final JMenuItem Copy_menu = new JMenuItem();
Copy_menu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try{
String str=jta.getSelectedText();
tempString=str;
}
catch(Exception e){
e.printStackTrace();
}
}
});
Copy_menu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK));
Copy_menu.setFont(new Font("", Font.PLAIN, 12));
Copy_menu.setText("复制 ");
Edit_menu.add(Copy_menu);
final JMenuItem Paste_menu = new JMenuItem();
Paste_menu.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
try{
if(tempString.length()>0){
jta.insert(tempString,jta.getCaretPosition());
}
}
catch(Exception e){
e.printStackTrace();
}
}
});
Paste_menu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK));
Paste_menu.setFont(new Font("", Font.PLAIN, 12));
Paste_menu.setText("粘贴");
Edit_menu.add(Paste_menu);
final JMenu Help_menu = new JMenu();
Help_menu.setMnemonic('H');
Help_menu.setFont(new Font("", Font.PLAIN, 12));
Help_menu.setText("帮助");
menuBar.add(Help_menu);
final JMenuItem About_menu = new JMenuItem();
About_menu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(new JFrame(), "Java实现得记事本小程序");
}
});
About_menu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK));
About_menu.setFont(new Font("", Font.PLAIN, 12));
About_menu.setText("关于记事本 ");
Help_menu.add(About_menu);
}
}