ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [자바프로그래밍] 이벤트 처리
    공부 2023. 6. 2. 13:31

    ActionEventTest3.java

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class MyFrame extends JFrame implements ActionListener {
       private JButton button1;
       private JButton button2;
       private JButton button3;
       private JLabel label;
       private JTextField text;
       
       MyFrame() {
          Toolkit kit = Toolkit.getDefaultToolkit();
          Dimension screensize = kit.getScreenSize();
          setSize(400, 200);
          setLocation(screensize.width / 2, screensize.height / 2);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setTitle("MyFrame");
          
          Image img = kit.getImage("icon.jpg");
          setIconImage(img);
          setLayout( new FlowLayout() );
          button1 = new JButton("버튼1");
          button2 = new JButton("버튼2");
          button3 = new JButton("버튼3");
          label = new JLabel("눌려진 버튼이 출력됩니다.");
          text = new JTextField(20); // 20칸
          button1.addActionListener(this);
          button2.addActionListener(this);
          button3.addActionListener(this);
          
          this.add(button1);
          this.add(button2);
          this.add(button3);
          this.add(label);
          this.add(text);
          setVisible(true);
       }
       
       public void actionPerformed(ActionEvent e) {
          if(e.getSource() == button1) {
             label.setText("Button - 1 is pressed");
             text.setText("Button - 1 is pressed");
             text.setEditable(false);
          } else if(e.getSource() == button2) {
             label.setText("Button - 2 is pressed");
             text.setText("Button - 2 is pressed");
             text.setEditable(true);
          } else {
             label.setText("Button - 3 is pressed");
             text.setText("Button - 3 is pressed");
          }
       }
    }

     

    Key 이벤트

    KeyListener 인터페이스 구현

    메소드

     

    키 이벤트 예제

     

    '공부' 카테고리의 다른 글

    [JSP] CheckBox  (0) 2023.09.12
    [JSP] Login  (0) 2023.09.12
    [자바프로그래밍] 인터페이스, 람다식, 패키지 // 미작성  (0) 2023.06.09
    [자바프로그래밍] 그래픽 사용자 인터페이스  (0) 2023.06.02
    [웹 프로그래밍] DOM  (0) 2023.06.01
Designed by Tistory.