| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1004 人关注过本帖
标题:高手求助,Java运行~·
只看楼主 加入收藏
叮咚卢
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-12-8
收藏
 问题点数:0 回复次数:5 
高手求助,Java运行~·
怎么把他弄成可以双击执行的jar文件啊,急急急``

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import *;
import javax.swing.event.*;
import java.util.*; //Date needed

import

public class MyNotePad extends JFrame {
   
    File file = null;
    Color color = Color.black;
    GraphicsEnvironment getFont = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Font []fonts = getFont.getAllFonts();

    JTextPane text = new JTextPane(); //面板

    JFileChooser filechooser = new JFileChooser(); //文件选择

    JColorChooser colorchooser = new JColorChooser(); //颜色选择

    JDialog about = new JDialog(this); //弹出对话框

    JMenuBar menubar = new JMenuBar(); //菜单

    JToolBar toolbar = new JToolBar(); // 工具栏


    /** Creates a new instance of MyNotePad */
    public MyNotePad() {
        initTextPane();//面板

        initMenu();//菜单

        initAboutDialog();//关于对话框

        initToolBar();//工具栏

    }
   
    //----------------------------------------------

    //面板初始化

    void initTextPane(){
      setFont(new Font("Times New Roman",Font.PLAIN,12));
      getContentPane().add( new JScrollPane(text));
    }

    //------------------------------------------------

    //菜单按钮和下拉菜单定义

 JMenu menus[]= new JMenu[]
    {
        new JMenu("文件"),
        new JMenu("编辑"),
        new JMenu("查看"),
        new JMenu("工具"),
        new JMenu("帮助"),
    };
  
 JMenuItem menuitems [][] =new JMenuItem[][]
    {
        {
            new JMenuItem("新建"),
            new JMenuItem("打开..."),
            new JMenuItem("保存..."),
            new JMenuItem("Exit")
        },
        {
            new JMenuItem("Copy"),
            new JMenuItem("Cut"),
            new JMenuItem("Paste"),
            new JMenuItem("选择全部"),
            new JMenuItem("Color...")
        },
    {
         new JMenuItem("格式"),
         new JMenuItem("字体")

     },
        {
         new JMenuItem("MS 记事本"),
         new JMenuItem("MS 计算器"),
                  new JMenuItem("修改注册表")

     },
        {
                  new JMenuItem("About")
        }
    };
 //---------------------------------------------------

 
//------------------------------------------------------

//菜单初始化

   void initMenu(){
        for(int i=0;i<menus.length;i++)
        {
            menubar.add(menus[i]);
            for(int j=0;j<menuitems[i].length;j++)
            {
                menus[i].add(menuitems[i][j]);
                menuitems[i][j].addActionListener(action);
            }
        }
        this.setJMenuBar(menubar);
    }
 //--------------------------------------------------

 
   //-----------------------------------------------

   //事件处理

    ActionListener action = new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            JMenuItem wf = (JMenuItem)e.getSource();
            String id = wf.getText();
             if(id.equals("新建")){
                Date date=new Date();
                text.setText(date.toString());
                file = null;
             }
             else if(id.equals("打开...")){
                if(file != null)
                  filechooser.setSelectedFile(file);

                int returnVal = filechooser.showOpenDialog(MyNotePad.this);

                if(returnVal == JFileChooser.APPROVE_OPTION){
                    file = filechooser.getSelectedFile();
                    openFile();
                }
            }
             else if(id.equals("保存...")){
                if (file!=null)
                filechooser.setSelectedFile(file);

                int returnVal = filechooser.showSaveDialog(MyNotePad.this);

                if(returnVal == JFileChooser.APPROVE_OPTION){
                    file = filechooser.getSelectedFile();
                    saveFile();
                }
             }
                else if(id.equals("Exit")){
                    System.exit(0);}
            else if(id.equals("Cut")){
                    text.cut();
                }else if(id.equals("Copy")){
                    text.copy();
                }else if(id.equals("Paste")){
                    text.paste();
                }else if(id.equals("选择全部")){
                    text.selectAll();
                }
            else if(id.equals("Color...")){
                    color = JColorChooser.showDialog(MyNotePad.this,"",color);
                    text.setForeground(color);
                }
            else if(id.equals("格式")){
                  
                }
              else if(id.equals("字体")){
                  
                }
            
            else if(id.equals("MS 记事本")){
        try{
         String command = "notepad.exe";
            Process child = Runtime.getRuntime().exec(command);
        }
        catch (IOException ex) {
                         }
                }
            else if(id.equals("MS 计算器")){
                    try{
            String command = "calc.exe";
            Process child = Runtime.getRuntime().exec(command);
            }
            catch (IOException ex){
                          }
                }
            else if(id.equals("修改注册表")){
                try{
                    String command = "regedit.exe";
                    Process child = Runtime.getRuntime().exec(command);
                }
                catch(IOException ex){
            }
            }
            else if(id.equals("About")){
                    about.setSize(250,150);
                    about.setVisible(true);
                }
            
        }
        };

 //-------------------------------------------------------------------------------

//保存文件

    void saveFile()
    {
        try{
            FileWriter fw = new FileWriter(file);
            fw.write(text.getText());
            fw.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
 //---------------------------------------------------------------

 
//打开文件

    void openFile()
    {
        try{
            FileReader fr = new FileReader(file);
            int len = (int) file.length();
            char [] buffer = new char[len];
            fr.read(buffer,0,len);
            fr.close();
            text.setText(new String(buffer));
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
//--------------------------------------------------------------

 //弹出对话框初始化

    void initAboutDialog(){
        
        about.setTitle("关于... ");
        about.getContentPane().setBackground(Color.white );
        about.getContentPane().add(new JLabel(" 简易文字编辑器!作者:了凡"));
        about.setModal(true);

    }
   
    //---------------------------------------------------------------

    //工具栏的定义以及初始化

   
    JButton [] buttons = new JButton[]
    {
        new JButton("",new ImageIcon("copy.jpg")),

        new JButton("",new ImageIcon("cut.jpg")),

        new JButton("",new ImageIcon("paste.jpg")),

        new JButton("",new ImageIcon("MS1.jpg")),

    new JButton("",new ImageIcon("MS2.jpg")),
    };
    //

    void initToolBar(){
          for(int i=0; i<buttons.length;i++)
          toolbar.add(buttons[i]);
        buttons[0].setToolTipText("copy");
        buttons[0].addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                text.copy();
            }
            });
         buttons[1].setToolTipText("cut");
        buttons[1].addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                text.cut();
            }
           });

        buttons[2].setToolTipText("paste");
        buttons[2].addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                text.paste();
            }
           });
            buttons[3].setToolTipText("MS 记事本");
     buttons[3].addActionListener(new ActionListener(){
     public void actionPerformed(ActionEvent e){
     try{
            String command = "notepad.exe";
            Process child = Runtime.getRuntime().exec(command);
            }
        catch (IOException ex){
                         }
     }
           });
           buttons[4].setToolTipText("MS 计算器");
        buttons[4].addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
         try{
                String command = "calc.exe";
                Process child = Runtime.getRuntime().exec(command);
                }
             catch (IOException ex){
                          }
         }
           });
        this.getContentPane().add(toolbar,BorderLayout.NORTH);
    }
    public static void main(String s[]){
        MyNotePad wff= new MyNotePad();
        wff.setTitle("简易文字编辑器");
        wff.setSize(600,400);
        wff.setVisible(true);
        wff.addWindowListener(new WindowAdapter(){ //监听窗体按钮

        public void windowClosing(WindowEvent e){
            System.exit(0);
            }
        });
        
    }
}
搜索更多相关主题的帖子: 运行 Java 
2008-12-08 11:42
elan1986
Rank: 6Rank: 6
等 级:贵宾
威 望:18
帖 子:458
专家分:407
注 册:2007-12-17
收藏
得分:0 
很简单的 选中你的项目 右键---->导出--->选择Java jar文件 点击NEXT 进行相应的操作 特别注意的是 要记得将你的主函数 要设置一下 就是MAIN方法 弄好后 点完成 就OK了

我帮你打了个包 记得将.rar扩展名删除了 保留.jar 这是上传受限的原因

MyNotePad.jar.rar (8.79 KB)
2008-12-08 16:01
xian1
Rank: 2
等 级:论坛游民
威 望:1
帖 子:42
专家分:10
注 册:2008-12-5
收藏
得分:0 
我也学习一下了
2008-12-08 17:21
anyiluo13
Rank: 1
等 级:新手上路
威 望:1
帖 子:65
专家分:0
注 册:2007-6-10
收藏
得分:0 
我看看
2008-12-14 14:22
anyiluo13
Rank: 1
等 级:新手上路
威 望:1
帖 子:65
专家分:0
注 册:2007-6-10
收藏
得分:0 
exe4j
2008-12-14 14:54
快速回复:高手求助,Java运行~·
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020640 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved