写了一个记事本程序,程序运行效果如下。

31.jpg

采用了第 3 方感官皮肤( xp )。总结该程序代码无非就是一个 JFrame 设置 JMenuBar —— >JMenu( 字符串作参数 ) —— >JMenuItem action 作为参数)。文本面板 JScrollPane+JTextPane ,由于 JTextPane 本身就有复制、粘贴、剪切方法,所以写这个程序也显得非常简单。功能设计得也很简单,主要是练习。

部分重要代码,菜单部分:

JMenuBar menubar = new JMenuBar ();                 // 实例化菜单栏

        JMenu menuFile = new JMenu (" 文件 ");                // 实例化菜单

        JMenu menuEdit = new JMenu (" 编辑 ");

        JMenu menuAbout = new JMenu (" 帮助 ");

        menuFile.add (new JMenuItem (actions[0]));          // 增加新菜单项

        menuFile.add (new JMenuItem (actions[1]));

        menuFile.add (new JMenuItem (actions[2]));

        menuFile.add (new JMenuItem (actions[7]));

        menuEdit.add (new JMenuItem (actions[3]));

        menuEdit.add (new JMenuItem (actions[4]));

        menuEdit.add (new JMenuItem (actions[5]));

        menuAbout.add (new JMenuItem (actions[6]));

        menubar.add (menuFile);                             // 增加菜单

        menubar.add (menuEdit);

        menubar.add (menuAbout);

事件的一部分代码:

class SaveAction extends AbstractAction

    {  // 保存命令

        public SaveAction ()

        {

            super (" 保存 ");

        }

        public void actionPerformed (ActionEvent e)

        {

            int i = filechooser.showSaveDialog (EditorDemo.this); // 显示保存文件对话框

            if (i == JFileChooser.APPROVE_OPTION)

            {  // 点击对话框中保存按钮

                File f = filechooser.getSelectedFile (); // 得到选择的文件

                try

                {

                    FileOutputStream out = new FileOutputStream (f);  // 得到文件输出流

                    out.write (textPane.getText ().getBytes ()); // 写出文件

                }

                catch (Exception ex)

                {

                    ex.printStackTrace (); // 输出出错信息

                }

            }

        }

    }

posted on 2007-03-08 12:09 -274°C 阅读(2221) 评论(0)  编辑  收藏 所属分类: JAVA

只有注册用户登录后才能发表评论。


网站导航:
 

常用链接

留言簿(21)

随笔分类(265)

随笔档案(242)

相册

JAVA网站

关注的Blog

搜索

  •  

积分与排名

  • 积分 - 908978
  • 排名 - 40

最新评论