日期:2014-05-20  浏览次数:20556 次

为什么编辑窗口下面的撤销不能响应,而帮助下面的查看帮助能响应?求问
Java code


/*Notepad0.1 实现界面
 *Notepad0.2 实现关闭窗口,并完善界面功能
 *Notepad0.3 实现各菜单的文件功能  将menu组件改为JMenu
 *Notepad0.4 实现编辑菜单的各项功能
 */




import java.awt.event.*;
import javax.swing.*;

public class Test{
    
    JFrame frame;
    JTextArea ta;
    JMenuBar menuBar = new JMenuBar();
    JMenu menu[] = new JMenu[5];
    JMenuItem edit[] = new JMenuItem[11];
    JMenuItem help[] = new JMenuItem[2];
    String mLable[] = {"文件(F)","编辑(E)","格式(D)","查看(V)","帮助(H)"};    //    文本
    

    String editLable[] = {"撤销(U)","剪切(T)","复制(C)","粘贴(V)",        //    编辑
                          "删除(L)","查找(F)","查找下一个(N)",
                          "替换(R)","转到(G)","全选(A)","日期(D)"};
    
    String helpLable[] = {"查看帮助(H)","关于记事本(A)"};
    

    EditAction editAction;
    JOptionPane option;

    String context = null;
    
    public static void main(String[] args){
        new Test().lauchFrame();
    }
    
    public void lauchFrame(){
        
        editAction = new EditAction();
        frame = new JFrame("Notepad java");                                    //    窗口
        frame.setBounds(200, 100, 800,600);
        frame.setVisible(true);
        frame.setJMenuBar(menuBar);
        ta = new JTextArea();                                                //    文本域
        ta.setSize(750, 500);
        ta.setEditable(true);
        frame.add(ta);


        for(int i=0;i<5;i++){                    //    文本
            menu[i] = new JMenu(mLable[i]);
            menuBar.add(menu[i]);

        }
        
        for(int j=0;j<11;j++){                    //    编辑
            edit[j] = new JMenuItem(editLable[j]);
            menu[1].add(editLable[j]);
            edit[j].addActionListener(editAction);
        }
        
        for(int i=0;i<2;i++){
            help[i]=new JMenuItem(helpLable[i]);
            menu[4].add(help[i]);
            help[i].addActionListener(editAction);
        }
    }
    

    class EditAction implements ActionListener{
        
        public void actionPerformed(ActionEvent a) {
                if(a.getSource()==edit[0]){                //    撤销
                    ption.showMessageDialog(frame, "为什么不能响应???");
                }

                if(a.getSource()==help[0]){
                    option.showMessageDialog(frame, "为什么不能响应!!!.");
                }
                
            }

     }
}






------解决方案--------------------
明显的错,if(a.getSource()==edit[0]){ 这里的条件不成立。edit[0]应不认得,你可以传个参数给EditAction 来区分。
------解决方案--------------------
Java code

for (int j = 0; j < 11; j++) { // 编辑
            edit[j] = new JMenuItem(editLable[j]);
            menu[1].add(edit[j]);
            edit[j].addActionListener(editAction);
        }



menu[1].add(edit[j]);!!!!!
   ooooooooooption.showMessageDialog(frame, "为什么不能响应???");

------解决方案--------------------
for(int j=0;j<11;j++){ // 编辑
edit[j] = new JMenuItem(editLable[j]);
menu[1].add(editLable[j]);--------------------menu[1].add(edit[j])
edit[j].addActionListener(editAction);
}

for(int i=0;i<2;i++){
help[i]=new JMenuItem(helpLable[i]);
menu[4].add(help[i]);
help[i].addActionListener(editAction);

你打错了,以后要注意看