[java]代码库package myframe;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import java.io.*;
public class notepad extends JFrame implements ActionListener {
JMenuBar menu;
JMenu menu1, menu2, menu3, menu4, menu5;
JMenuItem mi2, mi3, mi4, mi5, mi6;
JMenu ej;
JMenuItem ej1, ej2;
JToolBar toolbar;
JButton button1, button2, button3, button4, button5;
JTextArea textarea;
JScrollPane gdt;
public notepad() {
toolbar = new JToolBar();
button1 = new JButton("新建");
button2 = new JButton("打开");
button3 = new JButton("剪切");
button4 = new JButton("复制");
button5 = new JButton("粘贴");
menu = new JMenuBar();
menu1 = new JMenu("文件 ");
// menu1.setFont(null);
//menu1.setMnemonic('f');
menu2 = new JMenu("编辑(E)");
menu2.setMnemonic('e');
menu3 = new JMenu("格式(O)");
menu3.setMnemonic('O');
menu4 = new JMenu("查看(V)");
menu4.setMnemonic('V');
menu5 = new JMenu("帮助(H)");
menu5.setMnemonic('H');
ej = new JMenu("新建");
ej1 = new JMenuItem("文件");
ej2 = new JMenuItem("模板");
mi2 = new JMenuItem("打开");
mi2.addActionListener(this);
mi2.setActionCommand("open");
mi3 = new JMenuItem("保存(S)");
mi3.setMnemonic('s');
mi3.addActionListener(this);
mi3.setActionCommand("save");
mi4 = new JMenuItem("另存为");
mi5 = new JMenuItem("页面布局");
mi6 = new JMenuItem("退出");
textarea = new JTextArea();
gdt = new JScrollPane(textarea);
toolbar.add(button1);
toolbar.add(button2);
toolbar.add(button3);
toolbar.add(button4);
toolbar.add(button5);
ej.add(ej1);
ej.add(ej2);
menu.add(ej);
menu1.add(ej);
menu1.add(mi2);
menu1.addSeparator();
menu1.add(mi3);
menu1.add(mi4);
// menu1.addSeparator();
menu1.add(mi5);
menu1.add(mi6);
menu.add(menu1);
menu.add(menu2);
menu.add(menu3);
menu.add(menu4);
menu.add(menu5);
menu.add(menu5);
this.setTitle("记事本");
this.setSize(800, 700);
this.setJMenuBar(menu);
this.add(toolbar, BorderLayout.SOUTH);
this.add(gdt);
this.setDefaultCloseOperation(3);
this.setVisible(true);
}
public static void main(String[] args) {
JFrame notepad = new notepad();
}
@Override
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();
if (s.equals("open")) {
JFileChooser choose = new JFileChooser();
choose.setDialogTitle("打开文件");
choose.showOpenDialog(null);
choose.setVisible(true);
String path = choose.getSelectedFile().getAbsolutePath();//GET the whole path of the file
FileReader read = null;
BufferedReader bufferread = null;
try {
read = new FileReader(path);
bufferread = new BufferedReader(read);
String f = "", c = "";
while ((f = bufferread.readLine()) != null) {
c = c + (f + "\n");
}
textarea.setText(c);
} catch (Exception e1) {
} finally {
try {
read.close();
bufferread.close();
} catch (Exception e1) {
}
}
} else if (s.equals("save")) {
JFileChooser ch= new JFileChooser();
ch.setDialogTitle("另存为");
ch.showSaveDialog(null);
ch.setVisible(true);
String path= ch.getSelectedFile().getAbsolutePath();
try{
PrintStream p1 = new PrintStream(path);
System.setOut(p1);
System.out.println(this.textarea.getText());
}catch(Exception e2){
}
}
}
}
[代码运行效果截图]