ActionListener/ WindowListener/ Dialog

|


----- setBounds()/ dispose() -----

import java.awt.*;

 

class FTest
{

 public static void main(String[] args)
 {

  Frame f = new Frame();
 
  f.setBounds(20,20,300,100);                   // 사이즈를 지정 setSize와 비슷

  f.setVisible(true);

 

  try
  {
   Thread.sleep(5000);
  }catch(InterruptedException e)
  {}

 

  f.setVisible(false);
 
  f.dispose();                                         // 메모리 해제
 }
}



----- ActionListener/ WindowListener -----

 

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

 

public class FTest1 extends Frame implements ActionListener 
{
 Panel p1,p2;
 Button b1,b2,b3,b4;

 

 FTest1()
 {
  Frame f = new Frame();                         //프레임 생성
  f.setLayout(new FlowLayout());

 

  p1 = new Panel();
  p2 = new Panel();                               //panel생성

 

  f.add(p1);
  f.add(p2);
 
  p1.setBackground(Color.yellow);
  p2.setBackground(Color.red);               //panel배경색 지정
 
  b1 = new Button("b1");
  b2 = new Button("b2");
  b3 = new Button("b3");
  b4 = new Button("b4");                       // 버튼생성

 

  p1.add(b1);
  p1.add(b2);
  p2.add(b3);
  p2.add(b4);                         

 

  f.setVisible(true);
  f.setSize(500,300);

 

  b1.addActionListener(this);
  b2.addActionListener(this);
  b3.addActionListener(this);
  b4.addActionListener(this);                               //Button Event 선언
 
  f.addWindowListener(new WindowAdapter ()         //Window Event구현
  {
   public void windowClosing(WindowEvent e)
   {
    dispose();
    System.exit(0);
   }
  });
 }


 public static void main(String[] args)
 {
  FTest1 ft = new FTest1();
 }

 

 public void actionPerformed(ActionEvent e)        //Action Event 처리하는 함수
 {

  Button bt = (Button)e.getSource();

  String str = bt.getLabel();

 

  if(str.equals("b1"))
  {
   p2.setVisible(true);
  }
  else if(str.equals("b2"))
  {
   p2.setVisible(false);
  }
  else if(str.equals("b3"))
  {
   p1.setVisible(true);
  }
  else if(str.equals("b4"))
  {
   p1.setVisible(false);
  }

 }

}  

 

------ Dialog -------

 

class Dia extends Dialog implements ActionListener
{
 String msg;

 

 Dia(Frame f, String s)
 {
  super(f, "Notice", true);
  msg = s;
 }
 
 public void show()
 {
  Button b = new Button("OK");
  b.addActionListener(this);
 
  Panel p = new Panel();
 
  add("North", new Label(msg, Label.CENTER));  //dialog는 기본 배치가 관리자

                                                                  //BorderLayout이다

  p.add(b);

  add("South",p);

  setSize(200,300);
 
  super.show();
 }

 public void actionPerformed(ActionEvent e)
 {
  dispose();                                           //버튼이 눌리면 메모리해제(창닫기)
 }
}

 

---------------------------------------------------------------------------

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

 

public class Diat extends Frame implements ActionListener
{
 Dia d;

 

 public Diat()
 {
  super("다이얼로그 테스트");         //Frame에 제목 설정
 
  d = new Dia(this,"ggg");
 
  Button b1 = new Button("다이얼 열기");
  Panel p1 = new Panel();
 

  p1.add(b1);

  b1.addActionListener(this);

  add(p1);
 
  setSize(300,200);  

  show();
 }

 

 public void actionPerformed(ActionEvent e) // 버튼이 눌리면
 {
  d.show();                                          // Dia클래스의 메소드 호출
 }

 

 public static void main(String[] args)
 {
  Diat dt = new Diat();
 }

 

And