| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1057 人关注过本帖
标题:自己写的程序 有一个错误 期待高手解决!!
只看楼主 加入收藏
elan1986
Rank: 6Rank: 6
等 级:贵宾
威 望:18
帖 子:458
专家分:407
注 册:2007-12-17
结帖率:100%
收藏
 问题点数:0 回复次数:1 
自己写的程序 有一个错误 期待高手解决!!
package lch;

import java.sql.*;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FileDialog;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import
import
import
import
import

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;

public  class MenuDemo implements ActionListener,ItemListener
{
    public static JTextArea output;
    JScrollPane scrollpane;
    String newline="\n";
    public static JMenuItem exitItem;
    public static JMenuItem query0;
    public static JMenuItem query1;
    public static JMenuItem about0;
    public static JMenuItem about1;
    public static JMenuItem denglu;
    public static JMenuItem tianjiaduzhe;
    public static JMenuItem saveItem;
    public static JFileChooser filechooser = new JFileChooser();
    public JMenuBar createMenubar(){
        JMenuBar menubar;
        JMenu menu,submenu;
        JMenuItem menuItem;
        //JRadioButtonMenuItem rbMenuItem;
        
        menubar=new JMenuBar(); //定义菜单条
        
        //定义第一个菜单
        menu=new JMenu("登陆(A)");
        menu.setMnemonic(KeyEvent.VK_A);
        menubar.add(menu);
        
        //定义第一个菜单选项
        denglu=new JMenuItem("管理员登陆(0)",KeyEvent.VK_0);
        //设置快捷建
        denglu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1,ActionEvent.ALT_MASK));
        //添加监听
        denglu.addActionListener(this);
        menu.add(denglu);
        
        //定义第二个菜单选项
        menuItem=new JMenuItem("新建管理员(B)");
        menuItem.setMnemonic(KeyEvent.VK_B);
        menuItem.addActionListener(this);
        menu.add(menuItem);
        
        menu.addSeparator();//添加分割符
        
        /*JMenuItem exitItem=new JMenuItem("退出(X)");
        exitItem.setMnemonic(KeyEvent.VK_X);
        exitItem.addActionListener(this);
        menu.add(exitItem);*/
        exitItem=new JMenuItem("退出(X)");
        exitItem.setMnemonic(KeyEvent.VK_X);
        exitItem.addActionListener(this);
        menu.add(exitItem);

        
        menu=new JMenu("功能(N)");
        menu.setMnemonic(KeyEvent.VK_N);
        menubar.add(menu);
        
        
        /*submenu=new JMenu("查询");
        //定义第一个子菜单
        menuItem=new JMenuItem("读者信息");
        menuItem.addActionListener(this);
        submenu.add(menuItem);
        menu.add(submenu);*/
        
        submenu=new JMenu("查询");
        //定义第一个子菜单
        query0=new JMenuItem("读者信息");
        query0.addActionListener(this);
        submenu.add(query0);
        menu.add(submenu);
    
        query1=new JMenuItem("图书信息");
        query1.addActionListener(this);
        submenu.add(query1);
        menu.add(submenu);
        
        menu.addSeparator();
        

        submenu=new JMenu("添加");
        tianjiaduzhe=new JMenuItem("添加读者信息");
        tianjiaduzhe.addActionListener(this);
        submenu.add(tianjiaduzhe);
        menu.add(submenu);
        menuItem=new JMenuItem("添加图书信息");
        menuItem.addActionListener(this);
        submenu.add(menuItem);
        menu.add(submenu);
        
        menu.addSeparator();
        
        /*menuItem=new JMenuItem("删除读者");
        menuItem.addActionListener(this);
        menu.add(menuItem);*/
        
        submenu=new JMenu("删除");
        menuItem=new JMenuItem("删除读者信息");
        menuItem.addActionListener(this);
        submenu.add(menuItem);
        menu.add(submenu);
        menuItem=new JMenuItem("删除图书信息");
        menuItem.addActionListener(this);
        submenu.add(menuItem);
        menu.add(submenu);
        menu.addSeparator();
        
        /*menuItem=new JMenuItem("更新读者");
        menuItem.addActionListener(this);
        menu.add(menuItem);*/
        
        submenu=new JMenu("更新");
        menuItem=new JMenuItem("更新读者信息");
        menuItem.addActionListener(this);
        submenu.add(menuItem);
        menu.add(submenu);
        menuItem=new JMenuItem("更新图书信息");
        menuItem.addActionListener(this);
        submenu.add(menuItem);
        menu.add(submenu);
        
        
        menu=new JMenu("工具(T)");
        menu.setMnemonic(KeyEvent.VK_T);
        menubar.add(menu);
        menuItem=new JMenuItem("借书");
        menuItem.addActionListener(this);
        menu.add(menuItem);        
        menu.addSeparator();
        menuItem=new JMenuItem("还书");
        menuItem.addActionListener(this);
        menu.add(menuItem);
        menu.addSeparator();
        menuItem=new JMenuItem("罚款");
        menuItem.addActionListener(this);
        menu.add(menuItem);
        
        
        menu=new JMenu("读者留言(Q)");
        submenu.setMnemonic(KeyEvent.VK_Q);
        menubar.add(menu);
        menuItem=new JMenuItem("读者留言");
        menuItem.addActionListener(this);
        menu.add(menuItem);
        
        menu=new JMenu("关于(H)");
        submenu.setMnemonic(KeyEvent.VK_H);
        menubar.add(menu);    
        about0=new JMenuItem("联系作者");
        about0.addActionListener(this);
        menu.add(about0);
        menu.addSeparator();
        about1=new JMenuItem("关于图书管理系统");
        about1.addActionListener(this);
        menu.add(about1);
        return menubar;
        
    }
    
    //构建面板
    public Container createContentPane(){
        JPanel contentPane=new JPanel(new BorderLayout());
        contentPane.setOpaque(true);
        //定义一个文本域
        //output=new JTextArea(5,30);
        output=new JTextArea(5,30);
        output.setEditable(false);
        scrollpane=new JScrollPane(output);

        //将文本域添加到面板中
        contentPane.add(scrollpane,BorderLayout.CENTER);
        
        return contentPane;
    }
    //构建弹出菜单
    public void createPopupMent(){
        JMenuItem menuItem;
        
        JPopupMenu popup=new JPopupMenu();
          ImageIcon openicon = createImageIcon("images/Open16.gif");
        menuItem=new JMenuItem("复制文件",openicon);
        menuItem.addActionListener(this);
        popup.add(menuItem);
        /*ImageIcon saveicon = createImageIcon("images/Save16.gif");
        menuItem=new JMenuItem("保存文件",saveicon);
        menuItem=new JMenuItem("保存文件");
        menuItem.addActionListener(this);
        popup.add(menuItem);*/
        saveItem=new JMenuItem("保存文件");
        saveItem.addActionListener(this);
        popup.add(saveItem);
        
        //添加一个监听给文本域 响应点击右键时响应
        MouseListener popupListener=new PopupListener(popup);
        output.addMouseListener(popupListener);
    }
    


    private ImageIcon createImageIcon(String string) {
        // TODO Auto-generated method stub
        return null;
    }

    //监听普通菜单事件
    
        @SuppressWarnings("deprecation")
        public void actionPerformed(ActionEvent e){
        JMenuItem source=(JMenuItem)(e.getSource());

        /*String s="监测事件"+newline
        +"事件源:"+source.getText()
        +"(选择对象"+getClassName(source)+")";
        output.append(s+newline);*/
        if(source==denglu)
            {
             new yonghudenglu();
            }
        else if(source==saveItem)
        {
            saveItem();
        }
        else if(source==exitItem)
            System.exit(0);
        else if(source==query0)//查看读者信息
        {
            Connection conn=ConnectACS.getConnect("jdbc:odbc:library");
            
    
        ResultSet rs=ConnectACS.getResultSet(conn,"select * from duzheziliao");
        try
            {
            this.output.append("读者信息:");
            while(rs.next())
                {
                    this.output.append(newline+rs.getString(1)+"  "+rs.getString(2)+"  "+rs.getString(3)+"  "+rs.getString(4)+"  "+rs.getString(5)
                            +"  "+rs.getString(6)+"  "+rs.getString(7)+"  "+rs.getString(8)+newline);
                   
                }
                
            }
        catch(SQLException sqle)
                {
                    sqle.printStackTrace();
                }
            }
        else if(source==query1)//查看图书信息
        {
            Connection conn=ConnectACS.getConnect("jdbc:odbc:library");
            
    
        ResultSet rs=ConnectACS.getResultSet(conn,"select * from tushuziliao");
        try
            {
            this.output.append("图书信息:");
            while(rs.next())
                {
                    this.output.append(newline+rs.getString(1)+"  "+rs.getString(2)+"  "+rs.getString(3)+"  "+rs.getString(4)+"  "+rs.getString(5)
                            +"  "+rs.getString(6)+"  "+"$"+rs.getString(7)+newline);
                   
                }
                
            }
        catch(SQLException sqle)
                {
                    sqle.printStackTrace();
                }
            }
        else if(source==tianjiaduzhe)
            {
            tianjiayonghu cu=new tianjiayonghu();
            cu.show();
            }
        else if(source==about0)
            {
                JOptionPane.showMessageDialog(null,"作者:ellan(软件0602)");
            }
        else if(source==about1)
            {
                JOptionPane.showMessageDialog(null,"图书管理测试版本(版本号:1.0.0)");
            }
    
    }
    
    

    

        
        private void saveItem() {
        // TODO Auto-generated method stub
            //int i = filechooser.showSaveDialog(createMenubar.this); //显示保存文件对话框
            
                File f = filechooser.getSelectedFile(); //得到选择的文件
                try {
                    FileOutputStream out = new FileOutputStream(f);  //得到文件输出流
                    out.write(output.getText().getBytes()); //写出文件                
                } catch (Exception ex) {
                    ex.printStackTrace(); //输出出错信息
                }
            }
        
    

        // TODO Auto-generated method stub
        


    private String getClassName(JMenuItem source) {
        // TODO Auto-generated method stub
        return null;
    }


    public static void main(String[] args){
        JFrame.setDefaultLookAndFeelDecorated(true);
        
        //创建一个窗体
         JFrame frame = new JFrame("MenuDemo");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
         //创建菜单 并添加到面板中
         MenuDemo demo=new MenuDemo();
         frame.setJMenuBar(demo.createMenubar());
         frame.setContentPane(demo.createContentPane());
        
         //生成弹出菜单
         demo.createPopupMent();
        
         //显示窗体
         frame.setSize(450,260);
         frame.setVisible(true);

    }

    private static void showFrame(String string) {
        // TODO Auto-generated method stub
        
    }

    public void itemStateChanged(ItemEvent e) {
        // TODO Auto-generated method stub
        
    }
         }
    class PopupListener extends MouseAdapter{
        JPopupMenu popup;
        
        PopupListener(JPopupMenu popupMenu){
            popup=popupMenu;
        }
        
        public void mousePressed(MouseEvent e){
            maybeShowPopup(e);
            
        }
        


        public void mouseReleased(MouseEvent e){
            maybeShowPopup(e);
        }
         private void maybeShowPopup(MouseEvent e) {
          if (e.isPopupTrigger())
            {
                  popup.show(e.getComponent(),e.getX(), e.getY());
            }
         }
    }
    
    
    
我想问的是  当我点击右键时候,选择保存文件的时候 总是出现一个错误
谁能帮帮忙 帮我看看 我都看了好几遍了 都没找出问题所在!
搜索更多相关主题的帖子: import awt java event 
2008-05-21 13:15
xiaoyu2006
Rank: 1
等 级:新手上路
帖 子:64
专家分:0
注 册:2007-12-14
收藏
得分:0 
有错误提示么?打出来看看!
2008-05-21 13:25
快速回复:自己写的程序 有一个错误 期待高手解决!!
数据加载中...
 
   



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

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