动作事件没有解雇? [英] action event not fired ?

查看:77
本文介绍了动作事件没有解雇?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个框架显示一些文本字段和几个按钮,经典的确定和重置按钮,它们应该确认文本字段的值(关闭框架)并分别重置它们。

我认为我完全遵循委托方法程序来管理事件,即创建按钮对象,定义一个监听器类(实现ActionListener接口),并使用将侦听器类添加到按钮 addActionListener方法。但是没有办法让整个事情发挥作用。

没有例外。只有当我点击按钮时,才会忽略监听器的actionPerformed方法,就好像它不存在一样。

有什么问题?



以下是代码:



I have a frame showing some textfields and a couple of buttons, the classical "Ok" and "Reset" buttons, which are supposed to confirm the values of the textfields (closing the frame) and reset them respectively.
I think I thoroughly followed the "delegation method procedure" for managing event, that is , create the button object, define a listener class (implementing the ActionListener interface) , and add the listener class to the button using the "addActionListener" method . But there's no way to make the whole thing works.
No exception. Only when I click the button the "actionPerformed" method of the listener is simply ignored, as if it didn't exist.
What is the problem ?

Here is the code :

public FrameViewAddEstrazione (String ExtrPath)
        {
            try
            {
                FilePath = ExtrPath;
                
                F = new JFrame ();
                PP = new JPanel(); 
                PD = new JPanel(); // pannello data
                PE = new JPanel(); // pannello estrazioni
                PX = new JPanel(); // pannello pulsanti
                
                F.setLayout (new BoxLayout(F.getContentPane(), BoxLayout.Y_AXIS));
                F.add(PP); F.add(PD); F.add(PE); F.add(PX);
                
                // Path del file estrazioni
                JLabel LP = new JLabel ();
                TP = new JTextField (ExtrPath);
                TP.setColumns(40);
                PP.add (LP); PP.add(TP);

                // Data
                JLabel LYear = new JLabel ("Year"); TYear = new JTextField("",4) ;
                JLabel LMonth = new JLabel ("Month"); TMonth = new JTextField("",2) ;
                JLabel LDay = new JLabel ("Day"); TDay = new JTextField("",2) ;
                PD.add(LYear);  PD.add(TYear);  PD.add(LMonth); PD.add(TMonth); PD.add(LDay); PD.add(TDay);

                // Estrazione
                JLabel EL = new JLabel("Estrazioni");
                E1 = new JTextField ("",2);
                E2 = new JTextField ("",2);    
                E3 = new JTextField ("",2);
                E4 = new JTextField ("",2);
                E5 = new JTextField ("",2); 
                E6 = new JTextField ("",2); 
                PE.add(EL); PE.add (E1); PE.add(E2); PE.add(E3); PE.add(E4); PE.add(E5); PE.add(E6);

                // Pulsanti di conferma / reset
                BOK     = new JButton ("Ok");
                //BOK.setActionCommand("OK");
                BReset  = new JButton ("Reset");
                //BReset.setActionCommand("Reset");
                PX.add (BOK); PX.add(BReset);
                BOK.addActionListener   (this);
                BReset.addActionListener(this);
                
                F.setSize(400, 200);
                F.setVisible(true);
               
            }
            catch (Exception E)
            {
                E = E;
            }
        }
        
        public void actionPerformed(ActionEvent e)
        {
            try
            {
                
                String Cmd = e.getActionCommand();
                String File ="", chunk;    
                BufferedReader S = new BufferedReader (new FileReader (FilePath));
                chunk = S.readLine();
                while (chunk != "")
                {
                    File += chunk;
                    chunk = S.readLine();
                }
                
                if (Cmd == "OK")
                {



                        F.setVisible(false);
                }
                if (Cmd =="Reset")// reset all values
                {
                        TP.setText(FilePath);    
                        TYear.setText(""); TMonth.setText(""); TDay.setText(""); 
                        E1.setText("");
                        E2.setText("");
                        E3.setText("");
                        E4.setText("");
                        E5.setText("");
                        E6.setText("");
                }
            }
            catch (Exception E)
            {
                E=E ;
            }
            
        }

推荐答案

请使用IDE并调试编码。 Eclipse是免费的,可以轻松下载。它会改善你的编码。



你很快就会发现什么是错的。



此外,你会发现很多编程问题,比如那些带有大写字母的变量。更好地使用它们的说话名称,e0到e ......会在事情变得复杂时变得混乱。名为name或street的变量说明了自己并描述了内容。
Please use an IDE and debug your coding. Eclipse is free and can be downloaded easily. It will improve your coding.

You will soon find out what is wrong.

Also you will find lots of programming problems like those variables with Capital letters. Better use speaking names for them, "e0" to "e..." will get confusing when things get complicated. a variable named "name" or "street" speaks for itself and describes the content.


这篇关于动作事件没有解雇?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆