简体   繁体   English

理解java ActionListener时遇到的问题-Performed(ActionEvent e)

[英]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. 我必须为Fibonacci程序编写代码,该程序用两个文本框和一个按钮来构建GUI。 A user inputs a number in text box 1 and clicks the button which then places the Fibonacci of that number. 用户在文本框1中输入一个数字,然后单击按钮,然后放置该数字的斐波那契。 I am having problems understanding the actionPerformed part of java and would appreciate any help. 我在理解java的actionPerformed部分时遇到问题,将不胜感激。 Here is my code: There are 3 files. 这是我的代码:有3个文件。

Fibonacci.java 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 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 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 ). 基本上,当您添加一个按钮时,它知道如何在将其单击到名为事件分派器线程(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 ) 在这里,您声明一个实现ActionListener接口的OneActionListener类,以告知他可以处理事件的按钮(使用actionPerformed方法)

I hope this clear out a bit how this works. 我希望这一点可以弄清楚它是如何工作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM