import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.swing;
public class Jishiben extends Frame{
private TextArea content;
private String filePath;
public Jishiben(){
this.init();
}
private void init(){
this.setTitle("记事本");
this.setBounds(200,200,450,500);
this.createMenu();
this.createMainPanel();
this.createState();
this.removeWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent arg0){
System.exit(0);
}
});
this.setVisible(true);
}
//创建菜单
private void createMenu() {
// TODO Auto-generated method stub
MenuBar bar = new MenuBar();
this.setMenuBar(bar);
Menu fileMenu = new Menu("文件(F)");
Menu editMenu = new Menu("编辑(E)");
Menu formatMenu = new Menu("格式(O)");
Menu viewMenu = new Menu("查看(V)");
Menu helpMenu = new Menu("帮助(H)");
bar.add(fileMenu);
bar.add(editMenu);
bar.add(formatMenu);
bar.add(viewMenu);
bar.add(helpMenu);
MenuItem newItem = new MenuItem("新建(N)");
newItem.setShortcut(new MenuShortcut(KeyEvent.VK_N,false));
fileMenu.add(newItem);
MenuItem openItem = new MenuItem("打开(O)");
openItem.setShortcut(new MenuShortcut(KeyEvent.VK_O,false));
openItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
FileDialog dialog = new FileDialog(new Frame(),"打开.....",FileDialog.LOAD);
dialog.setVisible(true);
filePath = dialog.getDirectory() + dialog.getFile();
File file = new File(filePath);
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
try{
br = new BufferedReader (new FileReader(file));
String str = null;
while((str = br.readline()) != null){
sb.append(str).append("\n");
}
content.setText(sb.toString());
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}finally{
if (br != null){
try{
br.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
});
fileMenu.add(openItem);
MenuItem saveItem = new MenuItem("保存(S)");
saveItem.setShortcut(newMenuShortcut (KeyEvent.VK_S,false));
fileMenu.add(saveItem);
MenuItem saveAsItem = new MenuItem("另存为..(S)");
fileMenu.add(saveAsItem);
//添加一个分符
fileMenu.addSeparator();
MenuItem exitItem =new MenuItem("退出(E)");
exitItem.setShortcut(new MenuShortcut(KeyEvent.VK_E,false));
fileMenu.add(exitItem);
//格式菜单的复选菜单项
CheckboxMenuItem newLineItem = new CheckboxMenuItem("自动换行(W)",ture);
formatMenu.add(newLineItem);
}
//创建组内容面板
private void createMainPanel(){
content new = TextArea("",100,100,TextArea.SCROLLBARS_VERTICAL_ONLY);
//添加右键弹出式菜单
final PopupMenu popup = new PopupMenu();
popup.add(new MenuItem("剪切"));
popup.add(new MenuItem("复制"));
popup.add(new MenuItem("粘贴"));
content.add(popup);
content.addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent event) {
//单击鼠标右键
if (event.getButton() == MouseEvent.BUTTON3){
popup.show(content,event.getX(), event.getY());
}
}
});
this.add(content);
}
//创建状态栏
private void createState(){ }
public static void main(String[] args){
new Jishiben();
}
}
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.swing;
public class Jishiben extends Frame{
private TextArea content;
private String filePath;
public Jishiben(){
this.init();
}
private void init(){
this.setTitle("记事本");
this.setBounds(200,200,450,500);
this.createMenu();
this.createMainPanel();
this.createState();
this.removeWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent arg0){
System.exit(0);
}
});
this.setVisible(true);
}
//创建菜单
private void createMenu() {
// TODO Auto-generated method stub
MenuBar bar = new MenuBar();
this.setMenuBar(bar);
Menu fileMenu = new Menu("文件(F)");
Menu editMenu = new Menu("编辑(E)");
Menu formatMenu = new Menu("格式(O)");
Menu viewMenu = new Menu("查看(V)");
Menu helpMenu = new Menu("帮助(H)");
bar.add(fileMenu);
bar.add(editMenu);
bar.add(formatMenu);
bar.add(viewMenu);
bar.add(helpMenu);
MenuItem newItem = new MenuItem("新建(N)");
newItem.setShortcut(new MenuShortcut(KeyEvent.VK_N,false));
fileMenu.add(newItem);
MenuItem openItem = new MenuItem("打开(O)");
openItem.setShortcut(new MenuShortcut(KeyEvent.VK_O,false));
openItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
FileDialog dialog = new FileDialog(new Frame(),"打开.....",FileDialog.LOAD);
dialog.setVisible(true);
filePath = dialog.getDirectory() + dialog.getFile();
File file = new File(filePath);
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
try{
br = new BufferedReader (new FileReader(file));
String str = null;
while((str = br.readline()) != null){
sb.append(str).append("\n");
}
content.setText(sb.toString());
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}finally{
if (br != null){
try{
br.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
});
fileMenu.add(openItem);
MenuItem saveItem = new MenuItem("保存(S)");
saveItem.setShortcut(newMenuShortcut (KeyEvent.VK_S,false));
fileMenu.add(saveItem);
MenuItem saveAsItem = new MenuItem("另存为..(S)");
fileMenu.add(saveAsItem);
//添加一个分符
fileMenu.addSeparator();
MenuItem exitItem =new MenuItem("退出(E)");
exitItem.setShortcut(new MenuShortcut(KeyEvent.VK_E,false));
fileMenu.add(exitItem);
//格式菜单的复选菜单项
CheckboxMenuItem newLineItem = new CheckboxMenuItem("自动换行(W)",ture);
formatMenu.add(newLineItem);
}
//创建组内容面板
private void createMainPanel(){
content new = TextArea("",100,100,TextArea.SCROLLBARS_VERTICAL_ONLY);
//添加右键弹出式菜单
final PopupMenu popup = new PopupMenu();
popup.add(new MenuItem("剪切"));
popup.add(new MenuItem("复制"));
popup.add(new MenuItem("粘贴"));
content.add(popup);
content.addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent event) {
//单击鼠标右键
if (event.getButton() == MouseEvent.BUTTON3){
popup.show(content,event.getX(), event.getY());
}
}
});
this.add(content);
}
//创建状态栏
private void createState(){ }
public static void main(String[] args){
new Jishiben();
}
}