美文网首页
AWT的FileDialog

AWT的FileDialog

作者: 开心的小哈 | 来源:发表于2021-11-07 22:13 被阅读0次
    package com.main;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class MassageDialogDeom {
        public void init(){
    
            Frame jFrame=new Frame("test Menu");
            //菜单条
            MenuBar menuBar=new MenuBar();
            //菜单项组件
            Menu file=new Menu("file");
    
            Menu edit =new Menu("edit");
            Menu save =new Menu("save");
            FileDialog openFile=new FileDialog(jFrame,"choose open file",FileDialog.LOAD);
            FileDialog saveFile=new FileDialog(jFrame,"choose save file",FileDialog.SAVE);
    
    
            MenuItem auto=new MenuItem("auto Row");
            MenuItem copy=new MenuItem("copy");
    
            MenuItem paste=new MenuItem("paste");
            MenuItem paste2=new MenuItem("-");//MenuItem添加分割线的方式
    
    //        paste.setLabel("-");
    
            Menu formatMenu=new Menu("format");
    //        formatMenu.addSeparator();//菜单之间添加分割线
            MenuItem annotation=new MenuItem("annotation",new MenuShortcut(KeyEvent.VK_Q,true));//关联快捷键ctrl+shift+Q
            MenuItem cancelAnnotation =new MenuItem("cancelAnnotation");
    
            TextArea content=new TextArea(6,10);
    
            copy.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("------");
                    openFile.setVisible(true);
    
                    String directory = openFile.getDirectory();
                    String file1 = openFile.getFile();
                    System.out.println(directory+file1);
                }
            });
            paste.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    saveFile.setVisible(true);
    
                    String directory = saveFile.getDirectory();
                    String file1 = saveFile.getFile();
                    System.out.println(directory+file1);
                }
            });
            annotation.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                content.append("click annotation"+e.getActionCommand());
            }
        });
            formatMenu.add(annotation);
            formatMenu.add(cancelAnnotation);
    
            edit.add(auto);
            edit.add(copy);
            edit.add(paste);
            edit.add(paste2);
            edit.add(formatMenu);
            menuBar.add(file);
            menuBar.add(edit);
            menuBar.add(save);
            jFrame.setMenuBar(menuBar);
            jFrame.add(content);
    
            jFrame.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                    super.windowClosing(e);
                }
            });
            jFrame.pack();
            jFrame.setVisible(true);
        }
    
        public static void main(String[] args) {
            new MassageDialogDeom().init();
    
        }
    }
    
    

    相关文章

      网友评论

          本文标题:AWT的FileDialog

          本文链接:https://www.haomeiwen.com/subject/mwjyzltx.html