Memory Drive

반응형
버튼을 생성하여 컨테이너 애플릿에 붙인뒤 이벤트 처리
  1. import java.awt.*;
  2. import java.applet.*;
  3. import java.awt.event.*;
  4. public class ButtonExam extends Applet implements ActionListener
  5. {
  6.   Label myLabel;
  7.   Button myButton1, myButton2, myButton3;
  8.   public void init()
  9.   {
  10.     myButton1 = new Button();
  11.     myButton1.setLabel("C언어");
  12.     myButton1.addActionListener(this);
  13.     add(myButton1);
  14.     myButton2 = new Button("C++언어");
  15.     myButton2.addActionListener(this);
  16.     add(myButton2);
  17.    
  18.     myButton3 = new Button("자바");
  19.     myButton3.addActionListener(this);
  20.     add(myButton3);
  21.    
  22.     myLabel = new Label();
  23.     myLabel.setText("버튼을 눌러주세요!");
  24.     myLabel.setAlignment(Label.CENTER);
  25.     myLabel.setBackground(Color.yellow);
  26.     add(myLabel);
  27.   }
  28.   public void actionPerformed(ActionEvent e)
  29.   {
  30.     if(e.getSource() == myButton1) {
  31.       myLabel.setText("선택 : C언어");
  32.     } else if(e.getSource()==myButton2) {
  33.       myLabel.setText("선택 : C++언어");
  34.     } else if(e.getSource()==myButton3) {
  35.       myLabel.setText("선택 : 자바");
  36.     }
  37.   }
  38. }

반응형