2015-12-02 201 views
0

我是java swing的新手。動作監聽器不工作

當我想設置一個actionlistener到其中一個按鈕時,它必須獲取textArea內容並將其發送給另一個類。它沒有以我期望的方式工作。

相反,它工作時,我改變文本區域的內容,我不知道發生了什麼?

的第一個按鈕我命名爲按鈕和同樣的問題發生了,當我使用一個名爲CLAC

這裏的按鈕的ActionListener的內另一ActionListener的是我的代碼:

package FFT; 

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() 
{ 
    super ("F F T"); 
    //final int input; 
    label=new JLabel(); 
    txtPanel=new TextPanel(); 
    Button=new JButton("OK"); 

    label.setText("Please Enter your Polynomial Degree"); 
    setLayout(new FlowLayout()); 
    add(label); 
    add(txtPanel); 
    add(Button); 

    Calc=new JButton("Claculate"); 



    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); 

      } 
     } 
    } 
      ); 
    //Integer k=new Integer(Integer.parseInt(txtPanel.getTextArea())); 
    //System.out.println(); 
    //Nums=new Complex[input+1]; 





    setSize(600,500); 
    setVisible(true); 






    } 

} 

makePolynomial類:

package FFT; 

import java.awt.FlowLayout; 

import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextArea; 

public class makePolynomial extends JPanel{ 


    JLabel[] label; 
    public JTextArea[] BoxArray; 
    public static int l; 
    public makePolynomial(int i) 
    { 
     //label=new JLabel(); 
     BoxArray=new JTextArea[2*(i+1)]; 
    label=new JLabel[i]; 
    l=i; 
    setLayout(new FlowLayout()); 

    int b=i; 
    for (int x=2*(i+1)-1;x>=0;x-=2) 
    { 
     BoxArray[x]=new JTextArea(1,3); 
     BoxArray[x-1]=new JTextArea(1,3); 
     add(BoxArray[x]); 
     add(BoxArray[x-1]); 

     if (b >=1) 
     { 
      label[b-1]=new JLabel(); 
      label[b-1].setText("<html>i X<sup>"+b+"</sup></html>"); 
      add(label[b-1]); 
     } 

     if(b==0) 
     { 
      JLabel l1=new JLabel(); 
      l1.setText("i"); 
      add(l1); 
     } 
     b--; 
    } 
} 

} 

ShowResult類:

package FFT; 

import java.awt.FlowLayout; 

import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class ShowResult extends JPanel { 
JLabel[] label; 


public ShowResult(Complex[] c) { 


    label=new JLabel[c.length]; 
    System.out.println("Complex"+c.length); 

    setLayout(new FlowLayout()); 

    for (int i=0;i<c.length;i++) 
    { 
     label[i]=new JLabel(); 
     label[i].setText("y["+i+"]= "+c[i].toString()); 

     add(label[i]); 

    } 




    } 



    } 

感謝您的幫助

+2

_it不以我expect_的方式工作嗎?你能指望什麼? –

+2

請查看[常見問題解答 - 我如何提出一個好問題?](http://stackoverflow.com/help/how-to-ask)如何提出好問題,以便您得到很好的答案。尤其是提供一個[MCVE](http://stackoverflow.com/help/mcve)(專注於** minimal **)來重現問題 - 您的代碼很可能包含了太多不需要證明問題的東西。這也是一種追蹤問題的通用方法:將代碼降至最低(使用git等版本控制系統,以便可以跟蹤更改)。 –

+0

我期望,點擊按鈕後,動作偵聽器完全工作,但相反後點擊按鈕什麼也沒有發生,直到當我想改變textArea內容動作偵聽器的作品....我想知道爲什麼mp = new makePolynomial(content); setLayout(new FlowLayout()); add(mp);不要工作 –

回答

0

與你的代碼的一小部分可運行程序的一個例子,在這裏看到的下面。爲了保持簡短,我用JPanel代替makePolynomial。我已經添加了代碼來隱藏ButtonactionPerformed方法中的三個初始組件,但我確信有更好的方法來執行此操作。

這個程序是否像你期望的那樣工作?

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

public class ActionListenerBug { 
    public static void main(final String[] arguments) { 
     SwingUtilities.invokeLater(() -> new ActionListenerBug().createAndShowGui()); 
    } 

    private void createAndShowGui() { 
     final JFrame frame = new JFrame("Stack Overflow"); 
     frame.setBounds(100, 100, 800, 600); 
     frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 

     final JPanel panel = new JPanel(); 

     JLabel label = new JLabel(); 
     // TextPanel is a class? 
     JTextPane txtPanel = new JTextPane(); 
     JButton Button = new JButton("OK"); 

     label.setText("Please Enter your Polynomial Degree"); 

     panel.setLayout(new FlowLayout()); 
     panel.add(label); 
     panel.add(txtPanel); 
     panel.add(Button); 

     Button.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent arg0) { 
       try { 
        int input = Integer.parseInt(txtPanel.getText()); 
        System.out.println(input); 
        //mp = new makePolynomial(content); 
        JPanel mp = new JPanel(); 
        mp.add(new JLabel("input: " + input)); 

        // Hide other components for now. Showing a different panel would be 
        // better. 
        label.setVisible(false); 
        txtPanel.setVisible(false); 
        Button.setVisible(false); 

        panel.setLayout(new FlowLayout()); 
        panel.add(mp); 
        //setLayout(new BorderLayout()); 
        JButton Calc = new JButton("Claculate"); 
        panel.add(Calc); 
        panel.revalidate(); 
       } catch (NumberFormatException exception) { 
        JOptionPane.showMessageDialog(null, "Please Enter Just Numbers! ", 
                "Wrong Value", 
                JOptionPane.ERROR_MESSAGE); 
       } 
      } 
     }); 

     frame.getContentPane().add(panel); 
     frame.setVisible(true); 
    } 
}