📄 menu1.java
字号:
package Application;
import java.awt.*;
import java.io.*;
import java.awt.datatransfer.*;
public class Menu1 extends Frame {
static int count = 0;
String name = null;
String path = null;
String txt ="";
Menu f = new Menu( "文件(F)" );
Menu e = new Menu( "编辑(E)" );
Menu g = new Menu( "格式(G)" );
Menu v = new Menu( "查看(V)" );
Menu h = new Menu( "帮助(H)" );
TextArea ta= new TextArea( txt , 100 , 3000);
MenuBar mb = new MenuBar();
Clipboard clipbd = getToolkit().getSystemClipboard();
MenuItem[] mi_f = { new MenuItem("新建(N) Ctrl+N"),
new MenuItem("打开(O) Ctrl+O"),
new MenuItem("保存(S) Ctrl+S"),
new MenuItem("另存为(I)..."),
new MenuItem("页面设置(U)"),
new MenuItem("打印(P)... Ctrl+p"),
new MenuItem("退出(X)")
};
MenuItem[] mi_e ={ new MenuItem("撤销(U) Ctrl+Z"),
new MenuItem("剪切(T) Ctrl+X"),
new MenuItem("复制(C) Ctrl+C"),
new MenuItem("粘贴(P) Ctrl+V"),
new MenuItem("删除(L) Del"),
new MenuItem("查找(F) Ctrl+F"),
new MenuItem("查找下一个 F3"),
new MenuItem("替换(R) Ctrl+H"),
new MenuItem("转到(G) Ctrl+G"),
new MenuItem("全选(A) Ctrl+A"),
new MenuItem("时间/日期(D) F5"),
};
MenuItem[] mi_g = { new MenuItem("自动换行(W)"),
new MenuItem("字体(F)...")
};
MenuItem[] mi_g1 = { new MenuItem("---自动换行(W)"),
new MenuItem("字体(F)...")
};
MenuItem[] mi_v = { new MenuItem("状态栏(S)")};
MenuItem[] mi_h = { new MenuItem("帮助主题(H)"),
new MenuItem("关于记事本(A)")
};
public boolean Save( Menu1 f, String name , String path )throws IOException {
PrintWriter pw = new PrintWriter(new FileWriter(path+name, false));
String input=f.ta.getText();
pw.print(input);
pw.flush();
pw.close();
return true;
}
public boolean Save( Menu1 f ){
FileDialog d = new FileDialog(f,
"保存文件",FileDialog.SAVE);
d.setFile("*.*");
d.setDirectory(".");
d.show();
String saveFile;
if((saveFile = d.getFile()) != null) {
f.name = d.getFile();
f.path = d.getDirectory();
try{
Save( f , f.name , f.path );
} catch ( IOException ie ){
ie.printStackTrace();
}
}
return true;
}
Menu1(String n,int a,int b,String p,String t){
this.name = n;
this.path = p;
this.txt = t;
count++;
setTitle(this.name+" - 记事本");
setLayout( new BorderLayout());
for( int x=0 ; x<mi_f.length ; x++ ) f.add( mi_f[x] );
for(int x=0 ; x <mi_e.length ; x++ ) e.add( mi_e[x] );
for(int x =0; x<mi_g.length ; x++ ) g.add( mi_g[x] );
for(int x=0;x<mi_v.length; x++) v.add( mi_v[x] );
for(int x=0;x<mi_h.length; x++) h.add( mi_h[x] );
mi_f[4].disable();mi_f[5].disable();
mi_v[0].disable();
mi_e[0].disable();
mi_e[8].disable();
//设置可用性!
mb.add(f);
mb.add(e);
mb.add(g);
mb.add(v);
mb.add(h);
ta.setText(this.txt);
add(ta);
setFont(new Font("宋体",Font.PLAIN,15));
setMenuBar(mb); // getMenuBar()
if(a<0) a=-a;
if(b<0) b=-b;
setLocation(0, 0);
setSize(a,b); //设置大小可见性,默认无大小。。不可见!
setVisible(true); //也可以在下面使用Frame类的静态函数设置
}
Menu1(String name,String path){
new Menu1(name ,800,600,path,"");
}
Menu1(String name , String path ,String txt){
new Menu1(name,800,600,path,txt);
}
Menu1(String name){
new Menu1(name , 800 , 600 ,null,"" );
}
Menu1(){
new Menu1("无标题",800,600,null,"");
}
public boolean handleEvent( Event evt ){
if( evt.id == Event.WINDOW_DESTROY ) {
if (count == 1)System.exit(0);
else {
count--;
dispose();
}
//上面其实和下面等效!但是从效率而言,上面更高
//dispose();
//dispose() 和System.exit(0)是不同的
//exit()是退出虚拟机
//dispose()是释放当前窗口资源
}
else return super.handleEvent( evt );
return true ;
}
public boolean action( Event evt , Object obj) {
if(evt.target instanceof MenuItem) {
if( obj.equals("自动换行(W)" ) == true ) {
g.removeAll();
for(int x =0; x<mi_g1.length ; x++ ) g.add( mi_g1[x] );
}
else if( obj.equals("---自动换行(W)")==true ){
g.removeAll();
for(int x =0; x<mi_g.length ; x++ ) g.add( mi_g[x] );
}
else if( obj.equals("新建(N) Ctrl+N")==true) {
Menu1 x=new Menu1();
}
else if( obj.equals("打开(O) Ctrl+O")) {
FileDialog d = new FileDialog(this,
"选择文件打开");
d.setFile("*.*"); // Filename filter
d.setDirectory("."); // Current directory
d.show();
String temp;
if( ( temp = d.getFile()) != null ){
name = d.getFile();
path = d.getDirectory();
setTitle(this.name+" - 记事本---"+path);
temp = path+name;
try{
BufferedReader br = new BufferedReader(new FileReader(temp));
temp = br.readLine();
String result ="";
while(temp!=null){
result=result+temp;
result=result+"\n";
temp=br.readLine();
}
txt = result;
ta.setText( txt );
} catch ( IOException e ) {
e.printStackTrace();
}
}
}
else if( obj.equals("保存(S) Ctrl+S")){
Frame f= null;
if( path == null )
Save( this );
else
try{
Save(this,this.name,this.path );
}catch ( IOException ie) {
ie.printStackTrace();
}
}
else if(obj.equals("另存为(I)...")){
Save( this );
}
else if( obj.equals("页面设置(U)")){
}
else if( obj.equals("打印(P)... Ctrl+p")){
}
else if( obj.equals("退出(X)")){
if (count == 1)System.exit(0);
else {
count--;
dispose();
}
}
else if( obj.equals("剪切(T) Ctrl+X")) {
String selection = ta.getSelectedText();
StringSelection clipString = new StringSelection( selection );
clipbd.setContents( clipString , clipString );
ta.replaceRange( "" ,
ta.getSelectionStart(),
ta.getSelectionEnd() );
}
else if( obj.equals("复制(C) Ctrl+C")) {
String selection = ta.getSelectedText();
StringSelection clipString = new StringSelection( selection );
clipbd.setContents( clipString , clipString );
}
else if( obj.equals("粘贴(P) Ctrl+V")) {
Transferable clipData = clipbd.getContents(Menu1.this);
try {
String clipString =
(String)clipData.
getTransferData(
DataFlavor.stringFlavor);
ta.replaceRange(clipString,
ta.getSelectionStart(),
ta.getSelectionEnd());
} catch(Exception ex) {
System.out.println("not String flavor");
}
}
else if( obj.equals("删除(L) Del")) {
ta.replaceRange( "" ,
ta.getSelectionStart(),
ta.getSelectionEnd() );
}
else return super.action(evt , obj);
}
else return super.action(evt, obj);
return true;
}
public static void main( String[] args ) {
Menu1 f= new Menu1();
//f.setForeground(Color.green);
//设置控件颜色,当然这需要在设置可见性之前才能起作用!
//f.setSize(600,300);
//f.show();
//Menu1函数已经设置过可见性了
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -