📄 mymemo1.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class MyMemo1 extends JFrame implements ActionListener{
TextArea ta = new TextArea();
MyMenuBar mb = new MyMenuBar(this);
MyFile mf = new MyFile(this);
public MyMemo1(String title){
super(title);
this.setMenuBar(mb);
this.add(ta);
this.setBounds(400,200,300,300);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand()=="新建"){
ta.setText("");
}
else if(e.getActionCommand()=="打开"){
String s = mf.getData();
ta.setText(s);
}
else if(e.getActionCommand()=="保存"){
String s = ta.getText();
mf.saveData(s);
}
else if(e.getActionCommand()=="退出"){
this.dispose();
}
}
public static void main(String[] args){
new MyMemo1("记事本");
}
}
class MyMenuBar extends MenuBar {
String[] menus = {"文件","编辑","查找","帮助"};
String[][] menuItems = {{"新建","打开","保存",null,"退出"},
{"剪贴","复制","粘贴","清除",null,"全选"},
{"查找",null,"查找替换"},
{"信息"}};
//MyMemo1 jf;
public MyMenuBar(MyMemo1 jf){
//this.jf = jf;
for(int i=0;i<menus.length ;i++){
this.add(new Menu(menus[i]));
}
for(int i=0;i<this.getMenuCount();i++){
for(int j=0;j<menuItems[i].length;j++){
if(menuItems[i][j]==null){
this.getMenu(i).addSeparator();
}
else{
this.getMenu(i).add(new MenuItem(menuItems[i][j]));
this.getMenu(i).getItem(j).addActionListener(jf);
}
}
}
}
}
class MyFile extends FileDialog{
public MyFile(MyMemo1 parent){
super(parent);
}
public String getPath(){
return this.getDirectory()+"\\"+this.getFile();
}
//打开 获取所选文件的内容,以String 返回
public String getData(){
String aline;
StringBuffer sb = new StringBuffer();
this.setTitle("打开");
this.setMode(FileDialog.LOAD);
this.setVisible(true);
try{
BufferedReader br = new BufferedReader(
new FileReader(this.getPath()));
while( (aline = br.readLine()) !=null){
sb.append(aline+"\n");
}
br.close();
}catch(IOException e){
e.printStackTrace();
}
return sb.toString();
}
//保存 将ta的内容保存到选定的文件中
public void saveData(String s){
this.setTitle("保存");
this.setMode(FileDialog.SAVE);
this.setVisible(true);
try{
BufferedWriter bw = new BufferedWriter(
new FileWriter(this.getPath()));
bw.write(s);
bw.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -