Programming/JAVA

awt를 이용한 버튼과 이벤트.

커널제로 2011. 4. 6. 15:45

선생님꺼 파일 업로드(기능 추가 집에서 볼것)

package awt;

import java.awt.*;
import java.awt.event.*;

public class CardTest extends Frame implements ActionListener, ItemListener
{
 
 CardLayout card=new CardLayout();
 Panel p1=new Panel(); //패널 생성
 Panel p2=new Panel(); //패널 생성
 Panel pCenter=new Panel(card);
 
 
 
 TextField tfName; //패널1에 들어가는 내용
 Label lblName;
 
 Checkbox cb; //체크박스 생성, 패널2에 들어가는 내용
 Label lblOut;
 
 Button btn1, btn2; //버튼 생성
 
 public CardTest(String title)
 {
  super(title);
  this.addWindowListener(new WindowAdapter()
  {

   @Override
   public void windowClosing(WindowEvent e) //윈도우 종료
   {
    System.exit(0);
   }
   
  });
  this.setDesign();
  this.setLocation(300,100);
  this.setSize(300, 300);
  this.setVisible(true);
 }
 public void setDesign()
 {
  //card1
  p1.setLayout(null);
  p1.setBackground(Color.orange);
  tfName=new TextField(50);
  lblName=new Label();
  lblName.setBackground(Color.pink);
  tfName.setBounds(50,100,150,30); //set 바운즈 속성 공부하기 API 볼것
  lblName.setBounds(50,100,150,30);
  tfName.addActionListener(this);//엔터를 누르면 액션 발생함
    
  p1.add(tfName);
  p1.add(lblName);
  
  
  //card 2
  p2.setLayout(null);
  p2.setBackground(Color.green);
  
  cb=new Checkbox("운전면허");
  lblOut=new Label();
  
  cb.addItemListener(this);  
  
  cb.setBounds(50,50,100,30);
  lblOut.setBounds(50,100,150,30); //set바운즈 속성 공부하기
  
  p2.add(cb);
  p2.add(lblOut);
  
  //center 에 패널 추가 (카드 레이아웃)

  pCenter.add("name",p1);
  pCenter.add("license",p2);
  
  //card.show(pCenter, "license");
  card.show(pCenter, "name");
  this.add("Center",pCenter);
  
  //South 에 패널 하나 추가해서 버튼 두개 만들고, 화면에 표시
  
  Panel pBottom=new Panel();
  btn1=new Button("card1");
  btn2=new Button("card2");
  
  btn1.addActionListener(this);
  btn2.addActionListener(this);
  
  pBottom.add(btn1);
  pBottom.add(btn2);
  
  this.add("South",pBottom);
  
 }
 //////////////////////////
 @Override
 public void itemStateChanged(ItemEvent e)
 {
  lblOut.setText("운전면허가"+(cb.getState()?"있다":"없다"));
  
 }

 //////////////////////버튼 누를때 실행되는 액션
 @Override
 public void actionPerformed(ActionEvent e)
 {
  Object ob=e.getSource();
  
  if(ob==btn1)
   card.show(pCenter,"Name");
  else if(ob==btn2)
   card.show(pCenter,"license");
  else if(ob==tfName)
  {
   lblName.setText("내 이름은"+tfName.getText()+" 입니다.");
   tfName.setText("");
   tfName.requestFocus();
  }
 }
 
 
 
 
 public static void main(String[] args)
 {
  new CardTest("연습");

 }

}

실행하면 버튼1이 제대로 작동을 하지 않는다.....
어디가 에러인지 알고 싶다.

제대로 작동하는 모습


하지만 내꺼는...
버튼2는 잘되지만 1번은 메롱

분홍색 출력용 창이 없다.


package awt;

import java.awt.*;
import java.awt.event.*;

public class CardTest extends Frame implements ActionListener, ItemListener
{
 
 CardLayout card=new CardLayout();
 Panel p1=new Panel(); //패널 생성
 Panel p2=new Panel(); //패널 생성
 Panel pCenter=new Panel(card);
 
 
 
 TextField tfName; //패널1에 들어가는 내용
 Label lblName;
 
 Checkbox cb; //체크박스 생성, 패널2에 들어가는 내용
 Label lblOut;
 
 Button btn1, btn2; //버튼 생성
 
 public CardTest(String title)
 {
  super(title);
  this.addWindowListener(new WindowAdapter()
  {

   @Override
   public void windowClosing(WindowEvent e) //윈도우 종료
   {
    System.exit(0);
   }
   
  });
  this.setDesign();
  this.setLocation(300,100);
  this.setSize(300, 300);
  this.setVisible(true);
 }
 public void setDesign()
 {
  //card1
  p1.setLayout(null);
  p1.setBackground(Color.orange);
  tfName=new TextField(50);
  lblName=new Label();
  lblName.setBackground(Color.pink);
  tfName.setBounds(50,100,150,30); //set 바운즈 속성 공부하기 API 볼것
  lblName.setBounds(50,150,150,30); //뿌려주는 위치가 동일해서 안보였었음
  tfName.addActionListener(this);//엔터를 누르면 액션 발생함
    
  p1.add(tfName);
  p1.add(lblName);
  
  
  //card 2
  p2.setLayout(null);
  p2.setBackground(Color.green);
  
  cb=new Checkbox("운전면허");
  lblOut=new Label();
  
  cb.addItemListener(this);  
  
  cb.setBounds(50,50,100,30);
  lblOut.setBounds(50,100,150,30); //set바운즈 속성 공부하기
  
  p2.add(cb);
  p2.add(lblOut);
  
  //center 에 패널 추가 (카드 레이아웃)

  pCenter.add("name",p1);
  pCenter.add("license",p2);
  
  //card.show(pCenter, "license");
  card.show(pCenter, "name");
  this.add("Center",pCenter);
  
  //South 에 패널 하나 추가해서 버튼 두개 만들고, 화면에 표시
  
  Panel pBottom=new Panel();
  btn1=new Button("card1");
  btn2=new Button("card2");
  
  btn1.addActionListener(this);
  btn2.addActionListener(this);
  
  pBottom.add(btn1);
  pBottom.add(btn2);
  
  this.add("South",pBottom);
  
 }
 //////////////////////////
 @Override
 public void itemStateChanged(ItemEvent e)
 {
  lblOut.setText("운전면허가"+(cb.getState()?"있다":"없다"));
  
 }

 //////////////////////버튼 누를때 실행되는 액션
 @Override
 public void actionPerformed(ActionEvent e)
 {
  Object ob=e.getSource();
  
  if(ob==btn1)
   card.show(pCenter,"name"); //소문자로 수정
  else if(ob==btn2)
   card.show(pCenter,"license");
  else if(ob==tfName)
  {
   lblName.setText("내 이름은"+tfName.getText()+" 입니다.");
   tfName.setText("");
   tfName.requestFocus();
  }
 }
 
 
 
 
 public static void main(String[] args)
 {
  new CardTest("연습");

 }

}