开发者

problems understanding java ActionListener - Performed(ActionEvent e)

I have to write code for a Fibonacci program that builds a GUI with two text box and a button. A user inputs 开发者_开发问答a number in text box 1 and clicks the button which then places the Fibonacci of that number. I am having problems understanding the actionPerformed part of java and would appreciate any help. Here is my code: There are 3 files.

Fibonacci.java

public class Fibonacci{
   int Fib (int n){
      int in1=1,in2=1;
      int sum=0;//initial value
      int index=1;
      while (index<n){
         sum=in1+in2;//sum=the sum of 2 values;
         in1=in2;//in1 gets in2
         in2=sum;//in2 gets sum
         index++; //increment index
      }
   return sum;
   }
}

FibonacciJPanel.java

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


public class FibonacciJPanel extends JPanel implements ActionListener
{  private JTextField inField = new JTextField(15);  //GUI components
   private JTextField resultField = new JTextField(15);
   private JLabel prompt1 = new JLabel("Input Fibonacci>>");
   private JLabel prompt2 = new JLabel("Conversion Result:");
   private JButton FibonacciButton = new JButton("Fibonacci of the input");
   private JPanel panelN = new JPanel();        //Panels
   private JPanel panelC = new JPanel();
   private JPanel panelS = new JPanel();
   private Fibonacci F = new Fibonacci();



   public FibonacciJPanel()                     //Set up user panel
   { setLayout(new BorderLayout());             //User BorderLayout
     panelN.setLayout(new BorderLayout());
     panelC.setLayout(new BorderLayout());
     panelS.setLayout(new BorderLayout());
     panelN.add("North", prompt1);              //Input elements
     panelN.add("South", inField);
     panelC.add("West", FibonacciButton);       //Control button
     panelS.add("North", prompt2);              //Output elements
     panelS.add("South", resultField);
     add("North", panelN);                      //Input at the top
     add("Center", panelC);                     //buttons in the center
     add("South", panelS);                      //Result at the bottom
     FibonacciButton.addActionListener(this);           //Register with listeners
     setSize(175,200);
   } //FibonacciJPanel



public void actionPerformed(ActionEvent e)
    {
String inputStr = inField.getText();        //user input
        int userInput = Integer.parseInt(inputStr); //convert to integer
        boolean result=false;
    if (e.getSource() == FibonacciButton);          //Process and report
    {
            result = fc.sum(userInput);
            resultField.setText("result");

    } //if
     }//actionPerformed()
}

FibonacciJApplet.java

import javax.swing.*;

public class FibonacciJApplet extends JApplet
{   public void init()
    {   getContentPane().add(new FibonacciJPanel());
    } // init()
} // Fibonacci class


You don't mention what is exactly what you don't understand.

Basically when you add a button it knows how to trigger events when you click it to a thread called Event Dispatcher Thread ( EDT ).

Every time you click on a button this event will notify to every "Listener" registered within that button ( They are listening for notifications )

So a simpler example would be:

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

class Click { 
    public static void main( String ... args ) { 
        JButton clickMe = new JButton("Click Me");
        ActionListener aListener = new OneActionListener();
        clickMe.addActionListener( aListener );
        JFrame frame = new JFrame();
        frame.add( clickMe );
        frame.pack();
        frame.setVisible( true );
    }
}
class OneActionListener implements ActionListener { 
    public void actionPerformed( ActionEvent e ) { 
        System.out.printf("Clicked. Thread: %s, action: %s%n", 
            Thread.currentThread().getName(), 
            e.getActionCommand() );
    }
}

Here you declare a class OneActionListener that implements ActionListener interface to let know the button he can handle the event ( with the actionPerformed method )

I hope this clear out a bit how this works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜