package ch11;

import java.awt.Frame;
import java.awt.Label;
import java.awt.Rectangle;
import java.awt.Button;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Ch1106 extends Frame {

 private static final long serialVersionUID = 1L;
 private Label label1 = null;
 //서브윈도우에서 접근할 수도록 하기 위해...
 public TextField txtId = null;
 private Button button1 = null;
 
 //서브윈도우에 전달할 클래스 자기자신을 멤버변수로
 //선언
 private Ch1106 main;
 /**
  * This is the default constructor
  */
 public Ch1106() {
  super();
  //클래스 자기자신을 멤버변수main에 대입
  main=this;
  initialize();
 }

 /**
  * This method initializes this
  *
  * @return void
  */
 private void initialize() {
  label1 = new Label();
  label1.setBounds(new Rectangle(18, 43, 45, 23));
  label1.setText("아이디");
  this.setLayout(null);
  this.setSize(300, 200);
  this.setTitle("메인윈도우");

  this.setVisible(true);
  this.add(label1, null);
  this.add(getTxtId(), null);
  this.add(getButton1(), null);
  this.addWindowListener(new WindowAdapter() {
   @Override
   public void windowClosing(WindowEvent e) {
    // TODO 자동 생성된 메소드 스텁
    dispose();
   }
  });
 }

 /**
  * This method initializes txtId
  *  
  * @return java.awt.TextField
  */
 private TextField getTxtId() {
  if (txtId == null) {
   txtId = new TextField();
   txtId.setBounds(new Rectangle(73, 45, 128, 23));
   txtId.setEditable(false);
  }
  return txtId;
 }

 /**
  * This method initializes button1
  *  
  * @return java.awt.Button
  */
 private Button getButton1() {
  if (button1 == null) {
   button1 = new Button();
   button1.setBounds(new Rectangle(213, 45, 58, 23));
   button1.setLabel("ID검사");
   button1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
     // TODO 자동 생성된 메소드 스텁
     new Ch1106sub(main);
    }
   });
  }
  return button1;
 }

}

****** Ch1106sub.java *******
package ch11;

import java.awt.Frame;
import java.awt.Label;
import java.awt.Rectangle;
import java.awt.Button;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Ch1106sub extends Frame {

 private static final long serialVersionUID = 1L;
 private Label label1 = null;
 private TextField txtId = null;
 private Button button1 = null;
 //메인윈도우를 보관할 멤버변수의 선언
 private Ch1106 main;
 /**
  * This is the default constructor
  */
 public Ch1106sub() {
  super();
 }
 
 public Ch1106sub(Ch1106 main)
 {
  //인자없는 생성자 호출
  this();
  //메인윈도우개체를 멤버변수에 보관
  this.main=main;
  initialize();
 }

 /**
  * This method initializes this
  *
  * @return void
  */
 private void initialize() {
  label1 = new Label();
  label1.setBounds(new Rectangle(18, 43, 45, 23));
  label1.setText("아이디");
  this.setLayout(null);
  this.setSize(300, 200);
  this.setTitle("서브윈도우");

  this.setVisible(true);
  this.add(label1, null);
  this.add(getTxtId(), null);
  this.add(getButton1(), null);
  this.addWindowListener(new WindowAdapter() {
   @Override
   public void windowClosing(WindowEvent e) {
    // TODO 자동 생성된 메소드 스텁
    dispose();
   }
  });
 }

 /**
  * This method initializes txtId
  *  
  * @return java.awt.TextField
  */
 private TextField getTxtId() {
  if (txtId == null) {
   txtId = new TextField();
   txtId.setBounds(new Rectangle(73, 45, 128, 23));
  }
  return txtId;
 }

 /**
  * This method initializes button1
  *  
  * @return java.awt.Button
  */
 private Button getButton1() {
  if (button1 == null) {
   button1 = new Button();
   button1.setBounds(new Rectangle(213, 45, 58, 23));
   button1.setLabel("ID검사");
   button1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
     // TODO 자동 생성된 메소드 스텁
     //메인윈도우의 id에 현재 서브윈도우의 id의 값을 대입
     main.txtId.setText(txtId.getText());
     dispose();
    }
   });
  }
  return button1;
 }

}

Posted by 말없제이
,