JFrame ActionListener

keb*_*ang 1 java swing actionlistener

JFrame myframe = new JFrame("My Sample Frame");
  JButton mybutton = new JButton("Okay");
Run Code Online (Sandbox Code Playgroud)

有人可以向我解释这些部分.

 mybutton.addActionListener(new ActionListener(){

  public void actionPerformed(ActionEvent evt){

  //Assuming that the content here will do something.

  }
Run Code Online (Sandbox Code Playgroud)

Jes*_*per 5

你究竟对这段代码有什么不了解?

代码为按钮添加了一个动作侦听器.actionPerformed单击按钮时将调用动作侦听器的方法.

请注意,此处使用匿名内部类.


Suh*_*pta 5

这里使用匿名内部类。

您已经在技术上实现了 ActionListener。当您调用 addActionListener 时:

mybutton.addActionListener(new ActionListener(){

 public void actionPerformed(ActionEvent evt){

    //Assuming that the content here will do something.

 }
Run Code Online (Sandbox Code Playgroud)

你创建了一个匿名类的实例,或者一个实现了 ActionListener 的没有名字的类。

对于相同的,请访问此链接。