import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import javax.swing.*;
import java.awt.event.*;
public class CursorClass implements ActionListener{
JButton btn;
JFrame frame;
public static void main(String[] args) {
CursorClass cc = new CursorClass();
cc.launchButton();
}
public void launchButton(){
frame = new JFrame("Main Window");
frame.setSize(400, 400);
btn = new JButton("Close Main Window");
btn.setSize(100,100);
btn.addActionListener(this); // <--------
JPanel pnl = new JPanel();
pnl.add(btn);
frame.add(pnl);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event){
System.out.print("Exiting..");
System.exit(0);
}
}
正如您在launchButton
方法中看到的......btn.addActionListener(this)
功能......
是什么this
?指的是?
是指整体CursorClass
吗?我只是有点困惑。我无法理解我传递给按钮对象的内容。我知道我向其中添加了 ActionListener .. 现在按钮(这是事件源)将在单击按钮时响应按钮,但添加this
该方法.. 我无法看到是什么this
.. 我知道最常见的答案是this
指当前对象等。但我需要更具体的内容。我写的这个类是否被认为是this
?