动作听众不起作用

时间:2015-12-02 06:18:54

标签: java swing

不幸的是,当我想要将动作监听器设置为获取textArea内容的按钮之一并将其发送到另一个类时它不能以我期望的方式工作,而不是它当我改变文本区域的内容时,我不知道发生了什么事?

我命名为Button的第一个按钮,当我在Button的动作监听器名为Clac中使用另一个动作监听器时发生同样的问题

这是我的代码:

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class FFT_Main_Frame extends JFrame {

    JLabel label;
    JButton button;
    TextPanel txtPanel;
    JButton Button;
    JLabel label1;
    // JTextArea[] BoxArray;
    makePolynomial mp;
    JButton Calc;
    Complex[] Nums;
    Complex[] Result;
    int input;
    FFT_Main fft_main;
    ShowResult shr;

    public FFT_Main_Frame() {

        Button.addActionListener(new ActionListener() {
            // Test t;

            // Integer content;
            @Override
            public void actionPerformed(ActionEvent arg0) {

                try {
                    Integer content = new Integer(Integer.parseInt(txtPanel.getTextArea()));
                    input = content;
                    System.out.println(input);
                    // inputt=input;
                    mp = new makePolynomial(content);
                    setLayout(new FlowLayout());
                    add(mp);
                    // setLayout(new BorderLayout());
                    add(Calc);

                    Nums = new Complex[input + 1];
                    Calc.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                            for (int i = input; i >= 0; i--) {
                                Nums[i] = new Complex(Double.parseDouble(mp.BoxArray[2 * (i + 1) - 1].getText()),
                                        Double.parseDouble(mp.BoxArray[2 * i].getText()));
                            }

                            for (int i = 0; i <= input; i++) {
                                System.out.println(Nums[i]);
                            }
                            fft_main = new FFT_Main();
                            Result = new Complex[input];

                            Result = fft_main.Recursive_FFT(Nums);

                            shr = new ShowResult(Result);

                            setLayout(new BorderLayout());
                            add(shr, BorderLayout.CENTER);

                            System.out.println("Result\n\n");
                            for (int i = 0; i <= input; i++) {
                                System.out.println(Result[i]);
                            }

                        }
                    });

                } catch (NumberFormatException exception) {
                    JOptionPane.showMessageDialog(null, "Please Enter Just Numbers! ", "Wrong Value",
                            JOptionPane.ERROR_MESSAGE);

                }
            }
        });
    }
}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

你正在使用动作监听器错误的方法这里是一个如何使用它的例子

public FFT_Main_Frame() {

  JButton Button = new JButton("Button");
  Button.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent arg0) {

       try {

         // Code Here

       }catch(Exception e) {
          // Code Here 

      }
  });

  JButton Calc = new JButton("Calc");
  Calc.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent arg0) {

           // Code Here          

     }
     });

   }  
 }

有关如何使用动作侦听器的一些教程

https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

http://www.tutorialspoint.com/swing/swing_action_listener.htm