자바 getActionCommand() 메서드

Sheeraz Gul 2024년2월15일
자바 getActionCommand() 메서드

이 튜토리얼은 Java에서 getActionCommand() 메소드를 사용하는 방법을 보여줍니다.

Java에서 getActionCommand() 메서드 사용

ActionListener 클래스의 getActionCommand() 메소드는 버튼을 식별합니다. 버튼이 여러 개 있을 때 getActionCommand()를 사용하면 어떤 버튼이 눌렸는지 쉽게 알 수 있습니다.

getActionCommand() 메서드는 작업 명령에서 문자열 표현을 제공합니다. 값은 setActionCommmand() 메서드를 사용하여 설정할 수 있는 구성 요소별로 다릅니다.

getActionCommand 메서드를 사용하여 예제를 만들어 보겠습니다. 먼저 JFrame을 확장하고 ActionListener를 구현하는 클래스를 만듭니다.

그런 다음 private Button을 초기화하고 생성자 메서드를 사용하여 제목, 크기, setButtons(), setActions() 메서드 등을 설정합니다. setButtons() 메서드를 사용하여 버튼에 이름을 지정하고 추가하고 사용합니다. setActions() 메서드는 getActionCommand가 호출될 때 호출될 작업 명령을 설정합니다.

이제 actionPerformed를 사용하여 위 명령에 작업을 할당합니다. 이러한 명령은 getActionCommand()를 사용하여 호출됩니다.

마지막으로 main 메서드로 또 다른 클래스를 생성하고 위 클래스의 인스턴스를 생성하여 애플리케이션을 실행합니다.

완전한 소스 코드:

package delftstack;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

class DemoFrame extends JFrame implements ActionListener {
  private JButton J_Button1, J_Button2, J_Button3;

  DemoFrame() {
    setTitle("getActionCommand");
    setLayout(new FlowLayout());
    setButtons();
    setAction();
    setSize(700, 200);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  private void setButtons() {
    J_Button1 = new JButton("JButton One");
    J_Button2 = new JButton("JButton Two");
    J_Button3 = new JButton("JButton Three");
    add(J_Button1);
    add(J_Button2);
    add(J_Button3);
  }

  private void setAction() {
    J_Button1.addActionListener(this);
    J_Button1.setActionCommand("J_Button1");
    J_Button2.addActionListener(this);
    J_Button2.setActionCommand("J_Button2");
    J_Button3.addActionListener(this);
    J_Button3.setActionCommand("J_Button3");
  }

  public void actionPerformed(ActionEvent event) {
    if (event.getActionCommand() == "J_Button1")
      JOptionPane.showMessageDialog(rootPane, "The JButton One is Pressed");
    else if (event.getActionCommand() == "J_Button2")
      JOptionPane.showMessageDialog(rootPane, "The JButton Two is Pressed");
    else if (event.getActionCommand() == "J_Button3")
      JOptionPane.showMessageDialog(rootPane, "The JButton Three is Pressed");
  }
}

public class Example {
  public static void main(String[] args) {
    DemoFrame Demo_Frame = new DemoFrame();
  }
}

위의 코드는 세 개의 버튼을 생성하고 이에 대한 동작 명령을 설정합니다. 버튼을 누르면 getActionCommand()가 호출되고 showMessageDialog() 메서드가 해당 메시지를 표시합니다.

출력:

Java에서 getActionCommand() 메서드 사용

작가: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook