# 내가 게시하고있는 문제에서, 1 행과 2 행에 언급 된 코드를 이해할 수 없습니다. 내가 알고있는 것에 대해 그들이 설정하는 데 사용된다는 것입니다. 버튼에 대한 액션 리스너이지만, 가장 혼란스러운 것은 "JB1.addActionListener (this)"와 같이 라인 1과 라인 2에있는 구문에서 "this"의 역할은 무엇입니까? 이 기본 문법이 어떻게 작동하는지 자세히 설명해주세요. #스윙 : 액션 리스너의 역할
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class frametest1_1 implements ActionListener
{
JLabel JL;
public frametest1_1()
{
//create a JFrame container
JFrame JF=new JFrame("A BUTTON");
//Frame Layout This is contained in Java .awt.*; "ON USING THIS OPTION OUR BUTTON AND OTHER COMPONENT ARE ADJUSTED IN THE FRAME AUTOMATICALLY"
JF.setLayout(new FlowLayout());
//set the size of the container
JF.setSize(200, 200);
//set visible
JF.setVisible(true);
//make button
JButton JB1=new JButton("FIRST");
JButton JB2=new JButton("SECOND");
//add button to the JFrame container
JF.add(JB1);
JF.add(JB2);
//Create and add Label to the JFrame container
JL=new JLabel("PRESS A BUTTON");
JF.add(JL);
//set action command :now this will help in determining that which button is presses, is it FIRST or SECOND
JB1.setActionCommand("one");
JB2.setActionCommand("two");
//The action responded is added to the actionlistener
JB1.addActionListener((ActionListener) this); // line 1
JB2.addActionListener((ActionListener) this); // line 2
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("one"))
JL.setText("First Button Pressed"); // to set text on the label
else
JL.setText("Second button Pressed"); // to set the text on the label
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new frametest1_1();
}
});
}
}
자바 명명 규칙을 배우고 그들에 충실하십시오 : 동일한 기능의
약간 더 나은 구현은 다음과 같은 것이다. – kleopatra