| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 788 人关注过本帖
标题:编记事本过程遇到的问题
只看楼主 加入收藏
暂不存在
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2013-5-30
结帖率:75%
收藏
已结贴  问题点数:20 回复次数:5 
编记事本过程遇到的问题
刚学java,试编了下记事本,出了很多问题,麻烦前辈们眼力好的花点时间看下,很奇怪为什么stateBar有没有显示在下面,然后事件的监听很不正常,键盘的快捷键也实现不了,望大神可以多提一些建议,不怕挨批只求进步!!


import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import
import
import
import
import
import
import java.util.Enumeration;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
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;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;



public class Ex227 extends JFrame{
    private JMenuBar cd;
    private JMenu cd1,cd2,cd3,cd4,cd5;
    private JMenuItem cd11,cd12,cd13,cd14,cd15,cd16,cd17;
    private JMenuItem cd21,cd22,cd23,cd24,cd25,cd26,cd27,cd28,cd29,cd210,cd211;
    private JMenuItem cd31,cd32;
    private JMenuItem cd41;
    private JMenuItem cd51,cd52;
    private JTextArea wby;
    private JScrollPane gdt;
    private JLabel stateBar;
    private JPopupMenu popUpMenu;
    private JFileChooser fileChooser;
    public Ex227() {
        super("新建文本文档");
        setUpUIComponent();
        setUpEventListener();
        
        ImageIcon image=new ImageIcon("u=2739352216,4239401708&fm=23&gp=0.jpg");
        this.setIconImage(image.getImage());
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    private void setUpUIComponent(){
        this.setSize(500,500);
        //菜单栏
        cd=new JMenuBar();
        // 设置「文件」菜单
        cd1=new JMenu("文件(F)");
        cd11=new JMenuItem("新建(N)                   Ctrl+N");
        cd12=new JMenuItem("打开(O)...                Ctrl+O");
        cd12.setAccelerator(
                KeyStroke.getKeyStroke(
                        KeyEvent.VK_O,
                        InputEvent.CTRL_MASK));
        cd13=new JMenuItem("保存(S)                   Ctrl+S");
        cd13.setAccelerator(
                KeyStroke.getKeyStroke(
                        KeyEvent.VK_S,
                        InputEvent.CTRL_MASK));
        cd14=new JMenuItem("另存为(A)...");
        cd15=new JMenuItem("页面设置(U)");
        cd16=new JMenuItem("打印(P)                   Ctrl+P");
        cd17=new JMenuItem("退出(X)");
        cd17.setAccelerator(
                KeyStroke.getKeyStroke(
                        'X'));
        cd1.add(cd11);
        cd1.add(cd12);
        cd1.add(cd13);
        cd1.add(cd14);
        cd1.addSeparator();
        cd1.add(cd15);
        cd1.add(cd16);
        cd1.addSeparator();
        cd1.add(cd17);

        // 设置「编辑」菜单  
        cd2=new JMenu("编辑(E)");
        cd21=new JMenuItem("撤销(Z)                   Ctrl+Z");
        cd22=new JMenuItem("剪切(T)                   Ctrl+X");
        cd22.setAccelerator(
                KeyStroke.getKeyStroke(KeyEvent.VK_X,
                        InputEvent.CTRL_MASK));
        cd23=new JMenuItem("复制(C)                   Ctrl+C");
        cd23.setAccelerator(
                KeyStroke.getKeyStroke(KeyEvent.VK_C,
                        InputEvent.CTRL_MASK));
        cd24=new JMenuItem("粘贴(V)                   Ctrl+V");
        cd24.setAccelerator(
                KeyStroke.getKeyStroke(KeyEvent.VK_V,
                        InputEvent.CTRL_MASK));
        cd25=new JMenuItem("删除(L)                   Del");
        cd26=new JMenuItem("查找(F)...");
        cd27=new JMenuItem("查找下一个(N)                   F3");
        cd28=new JMenuItem("替换(R)                   Ctrl+H");
        cd29=new JMenuItem("转到(G)                   Ctrl+G");
        cd210=new JMenuItem("全选(A)                   Ctrl+A");
        cd211=new JMenuItem("时间/日期(D)                   F5");
        cd2.add(cd21);
        cd2.addSeparator();
        cd2.add(cd22);
        cd2.add(cd23);
        cd2.add(cd24);
        cd2.add(cd25);
        cd2.addSeparator();
        cd2.add(cd26);
        cd2.add(cd27);
        cd2.add(cd28);
        cd2.add(cd29);
        cd2.add(cd210);
        cd2.addSeparator();
        cd2.add(cd211);


        //设置[格式]菜单
        cd3=new JMenu("格式(0)");
        cd31=new JMenuItem("自动换行(W)         ");
        cd32=new JMenuItem("字体(F)         ");
        cd3.add(cd31);
        cd3.add(cd32);


        //设置[查看]菜单
        cd4=new JMenu("查看(V)");
        cd41=new JMenuItem("状态栏(S)         ");
        cd4.add(cd41);


        //设置[帮助]菜单
        cd5=new JMenu("帮助(H)");
        cd51=new JMenuItem("查看帮助(H)         ");
        cd52=new JMenuItem("关于笔记本(A)         ");
        cd5.add(cd51);
        cd5.addSeparator();
        cd5.add(cd52);


        cd.add(cd1);
        cd1.setMnemonic('F');
        cd.add(cd2);
        cd2.setMnemonic('E');
        cd.add(cd3);
        cd3.setMnemonic('O');
        cd.add(cd4);
        cd4.setMnemonic('V');
        cd.add(cd5);
        cd5.setMnemonic('H');

        this.setJMenuBar(cd);
        //设置文本域

        wby=new JTextArea();
        wby.setFont(new Font("宋体", Font.PLAIN, 16));
        wby.setLineWrap(true);//设置文本区的换行策略。
        JScrollPane panel = new JScrollPane(wby,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        Container contentPane = getContentPane();
        contentPane.add(panel, BorderLayout.CENTER);  

        // 状态栏
        stateBar = new JLabel("未修改");
        stateBar.setHorizontalAlignment(SwingConstants.LEFT); //设置标签内容沿 X 轴的对齐方式。
        stateBar.setBorder(BorderFactory.createEtchedBorder());
        contentPane.add(stateBar, BorderLayout.SOUTH);      
        
        popUpMenu = cd2.getPopupMenu();
        fileChooser = new JFileChooser();



    }
    private void setUpEventListener(){
        // 按下窗口关闭钮事件处理
        this.addWindowListener(
                new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                        closeFile();
                    }
                }
                );
        // 菜单 - 打开
        cd12.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        openFile();
                    }
                }
                );

        // 菜单 - 保存
        cd13.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        saveFile();
                    }
                }
                );

        // 菜单 - 另存为
        cd14.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        saveFileAs();
                    }
                }
                );

        // 菜单 - 关闭文件
        cd17.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        closeFile();
                    }
                }
                );

        // 菜单 - 剪切
        cd22.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        cut();
                    }
                }
                );

        // 菜单 - 复制
        cd23.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        copy();
                    }
                }
                );

        // 菜单 - 粘贴
        cd24.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        paste();
                    }
                }
                );
        // 编辑区键盘事件
        wby.addKeyListener(
                new KeyAdapter() {
                    public void keyTyped(KeyEvent e) {
                        processTextArea();
                    }
                }
                );

        // 编辑区鼠标事件
        wby.addMouseListener(
                new MouseAdapter() {
                    public void mouseReleased(MouseEvent e) {
                        if(e.getButton() == MouseEvent.BUTTON3)
                            popUpMenu.show(cd2, e.getX(), e.getY());
                    }

                    public void mouseClicked(MouseEvent e) {
                        if(e.getButton() == MouseEvent.BUTTON1)
                            popUpMenu.setVisible(false);
                    }
                }
                );
    }
    private void openFile() {
        if(isCurrentFileSaved()) { // 文件是否为保存状态
            open(); // 打开
        }
        else {
            // 显示对话框
            int option = JOptionPane.showConfirmDialog(
                    null, "文件已修改,是否保存?",
                    "保存文件?", JOptionPane.YES_NO_OPTION,
                    JOptionPane.WARNING_MESSAGE, null);
            switch(option) {
            // 确认文件保存
            case JOptionPane.YES_OPTION:
                saveFile(); // 保存文件
                break;
                // 放弃文件保存
            case JOptionPane.NO_OPTION:
                open();
                break;
            }
        }
    }
    private boolean isCurrentFileSaved() {
        if(stateBar.getText().equals("未修改")) {
            return false;
        }
        else {
            return true;
        }
    }
    private void open() {
        // fileChooser 是 JFileChooser 的实例
        // 显示文件选取的对话框
        int option = fileChooser.showOpenDialog(null);
        // 使用者按下确认键
        if(option == JFileChooser.APPROVE_OPTION) {
            try {
                // 开启选取的文件
                BufferedReader buf =
                        new BufferedReader(
                                new FileReader(
                                        fileChooser.getSelectedFile()));

                // 设定文件标题
                setTitle(fileChooser.getSelectedFile().toString());
                // 清除前一次文件
                wby.setText("");
                // 设定状态栏
                stateBar.setText("未修改");
                // 取得系统相依的换行字符
                String lineSeparator = System.getProperty("line.separator");
                // 读取文件并附加至文字编辑区
                String text;
                while((text = buf.readLine()) != null) {
                    wby.append(text);
                    wby.append(lineSeparator);
                }

                buf.close();
            }   
            catch(IOException e) {
                JOptionPane.showMessageDialog(null, e.toString(),
                        "开启文件失败", JOptionPane.ERROR_MESSAGE);
            }
        }        
    }
    private void saveFile() {
        // 从标题栏取得文件名称
        File file = new File(getTitle());

        // 若指定的文件不存在
        if(!file.exists()) {
            // 执行另存为
            saveFileAs();
        }
        else {
            try {
                // 开启指定的文件
                BufferedWriter buf =
                        new BufferedWriter(
                                new FileWriter(file));
                // 将文字编辑区的文字写入文件
                buf.write(wby.getText());
                buf.close();
                // 设定状态栏为未修改
                stateBar.setText("未修改");
            }
            catch(IOException e) {
                JOptionPane.showMessageDialog(null, e.toString(),
                        "写入文件失败", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
    private void saveFileAs(){
        //显示文件选择器
        int option=fileChooser.showSaveDialog(null);
        //取得选择文件
        if(option==fileChooser.APPROVE_OPTION){
            //取得所得文件
            File file=fileChooser.getSelectedFile();
            //设置文件标题
            this.setTitle(file.toString());
            try{
                //建立新文件
                file.createNewFile();
                //保存文件
                saveFile();
            }
            catch(IOException e){
                JOptionPane.showMessageDialog(null, e.toString(),
                        "无法建立新文件", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
    private void closeFile() {
        // 是否已保存文件
        if(isCurrentFileSaved()) {
            // 释放窗口资源,而后关闭程序
            dispose();
        }
        else {
            int option = JOptionPane.showConfirmDialog(
                    null, "文件已修改,是否保存?",
                    "保存文件?", JOptionPane.YES_NO_OPTION,
                    JOptionPane.WARNING_MESSAGE, null);

            switch(option) {
            case JOptionPane.YES_OPTION:
                saveFile();
                break;
            case JOptionPane.NO_OPTION:
                dispose();
            }
        }
    }

    private void cut(){
        wby.cut();
        stateBar.setText("已修改");
        popUpMenu.setVisible(false);
    }

    private void copy(){
        wby.copy();
        popUpMenu.setVisible(false);   
    }

    private void paste(){
        wby.paste();
        stateBar.setText("已修改");
        popUpMenu.setVisible(false);
    }

    private void processTextArea(){
        stateBar.setText("已修改");
    }
    private static void InitGlobalFont(Font font) {
        FontUIResource fontRes = new FontUIResource(font);
        for (Enumeration<Object> keys = UIManager.getDefaults().keys();
                keys.hasMoreElements(); ) {
            Object key = keys.nextElement();
            Object value = UIManager.get(key);
            if (value instanceof FontUIResource) {
                UIManager.put(key, fontRes);
            }
        }
    }
    public static void main(String[] args){
        InitGlobalFont(new Font("alias", Font.PLAIN, 13));
        new Note();
    }

}
搜索更多相关主题的帖子: 记事本 import 快捷键 学java 键盘 
2013-12-07 21:36
韶志
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:斗气大陆
等 级:贵宾
威 望:44
帖 子:2223
专家分:13592
注 册:2013-3-22
收藏
得分:14 
so long ~

三十年河东,三十年河西,莫欺少年穷!
2013-12-08 14:05
暂不存在
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2013-5-30
收藏
得分:0 
求大神搭救啊啊啊啊
2013-12-08 19:14
谢跃锋
Rank: 2
等 级:论坛游民
威 望:2
帖 子:50
专家分:77
注 册:2013-4-14
收藏
得分:0 
very very long/我看到眼睛好痛。。。
2014-02-25 21:49
wowuhaole999
Rank: 1
等 级:新手上路
帖 子:7
专家分:5
注 册:2014-2-25
收藏
得分:0 
我也遇到过这种问题
2014-02-26 12:17
雪碧透心凉
Rank: 3Rank: 3
等 级:论坛游侠
威 望:2
帖 子:40
专家分:131
注 册:2014-2-26
收藏
得分:0 
long long

  assume nothing,do more, need less,smile often and realize how fortunate you are right now.
2014-02-26 15:36
快速回复:编记事本过程遇到的问题
数据加载中...
 
   



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

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