N스크린하이브리드앱과정/JAVA

[6주차 과제] JAVA: 1~5단계 만들기, 계산기 만들기

광천스러움 2013. 8. 25. 23:01

public class Test1_1 {
public static void main(String[] args) {
// 1단계
Frame f = new Frame();
f.setSize(300, 200);
f.setVisible(true);
}
}

 * 위의 기본셋팅을 가지고 1~5단계로 창닫기가 가능하도록 구현하라!

 

1. 1단계
public class Test1_1 {
public static void main(String[] args) {
// 1단계
Frame f = new Frame();
f.setSize(300, 200);
f.setVisible(true);
Iframe f1 = new Iframe();
f.addWindowListener(f1);
}
}

class Iframe extends Frame implements WindowListener{

@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}

@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}

@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}

@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}

@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}

@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}

@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
}

 


2. 2단계
public class Test1_2 {
public static void main(String[] args) {
// 2단계
Frame f = new Frame();
f.setSize(300, 200);
f.setVisible(true);
WinAdapter wa = new WinAdapter();
f.addWindowListener(wa);
}
}

class WinAdapter extends WindowAdapter{
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}

 

3. 3단계

public class Test_Stage3 extends Frame {
 public static void main(String[] args) {
  // 3단계 : 내부 클래스 사용
  Frame f = new Frame();
  f.setSize(300, 200);
  f.setVisible(true);
  
  Test_Stage3 outer = new Test_Stage3();
  Test_Stage3.InnerWinAdapter inner = outer.new InnerWinAdapter();
  
  f.addWindowListener(inner);
 }

 class InnerWinAdapter extends WindowAdapter {

  @Override
  public void windowClosing(WindowEvent e) {
   System.exit(0);
  }
}
}

 

 

4. 4단계

public class Test_Stage4 extends Frame {

 public static void main(String[] args) {
  // 4단계 : 내부 익명 클래스 사용
  Frame f = new Frame();
  f.setSize(300, 200);
  f.setVisible(true);  
  
  WindowAdapter wa = new WindowAdapter() {
   @Override
   public void windowClosing(WindowEvent e) {
    System.exit(0);
   }
  };
  f.addWindowListener(wa);
 }
}

 

5. 5단계

public class Test1_5 {
public static void main(String[] args) {
// 5단계
Frame f = new Frame();
f.setSize(300, 200);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}

 

☆ 계산기 만들기 

public class Test2 extends Frame implements ActionListener {
TextArea calc;
Button a0;
Button a1;
Button a2;
Button a3;
Button a4;
Button a5;
Button a6;
Button a7;
Button a8;
Button a9;
Button a10;
Button a11;
Button a12;
Button a13;
Button a14;
Button a15;
Button a16;
Button a17;
Button a18;
Button a19;
Button a20;
Button a21;
Button a22;
Button a23;
Button a24;
Button a25;
Button a26;
Button a27;
Button a28;
Button a29;
public static void main(String[] args) {
// 주말숙제(2) : 계산기 만들기
// 통째로 : border 레이아웃
// 위쪽 : north에 텍스트필드 넣기
// center : 그리드 레이아웃해서 버튼 넣기
// 기간 : 월요일까지
Test2 ex = new Test2("KKC의 계산기");
ex.setSize(300,400);
ex.setVisible(true);
ex.addWindowListener(new WindowAdapter() {

@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
Test2(String title){
super(title);
calc = new TextArea(3, 35);
a0 = new Button("MC");
a1 = new Button("MR");
a2 = new Button("MS");
a3 = new Button("M+");
a4 = new Button("M-");
a5 = new Button("<-");
a6 = new Button("CE");
a7 = new Button("C");
a8 = new Button("");
a9 = new Button("");
a10 = new Button("7");
a11 = new Button("8");
a12 = new Button("9");
a13 = new Button("/");
a14 = new Button("%");
a15 = new Button("4");
a16 = new Button("5");
a17 = new Button("6");
a18 = new Button("*");
a19 = new Button("1/x");
a20 = new Button("1");
a21 = new Button("2");
a22 = new Button("3");
a23 = new Button("-");
a24 = new Button("");
a25 = new Button("0");
a26 = new Button("");
a27 = new Button(".");
a28 = new Button("+");
a29 = new Button("=");
a0.addActionListener(this);
a1.addActionListener(this);
a2.addActionListener(this);
a3.addActionListener(this);
a4.addActionListener(this);
a5.addActionListener(this);
a6.addActionListener(this);
a7.addActionListener(this);
a8.addActionListener(this);
a9.addActionListener(this);
a10.addActionListener(this);
a11.addActionListener(this);
a12.addActionListener(this);
a13.addActionListener(this);
a14.addActionListener(this);
a15.addActionListener(this);
a16.addActionListener(this);
a17.addActionListener(this);
a18.addActionListener(this);
a19.addActionListener(this);
a20.addActionListener(this);
a21.addActionListener(this);
a22.addActionListener(this);
a23.addActionListener(this);
a24.addActionListener(this);
a25.addActionListener(this);
a26.addActionListener(this);
a27.addActionListener(this);
a28.addActionListener(this);
a29.addActionListener(this);
setLayout(new BorderLayout());
Panel p1 = new Panel();
Panel p2 = new Panel();
p1.add(calc);
p2.add(a0);
p2.add(a1);
p2.add(a2);
p2.add(a3);
p2.add(a4);
p2.add(a5);
p2.add(a6);
p2.add(a7);
p2.add(a8);
p2.add(a9);
p2.add(a10);
p2.add(a11);
p2.add(a12);
p2.add(a13);
p2.add(a14);
p2.add(a15);
p2.add(a16);
p2.add(a17);
p2.add(a18);
p2.add(a19);
p2.add(a20);
p2.add(a21);
p2.add(a22);
p2.add(a23);
p2.add(a24);
p2.add(a25);
p2.add(a26);
p2.add(a27);
p2.add(a28);
p2.add(a29);
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.CENTER);
p2.setLayout(new GridLayout(6,5));
}

@Override
public void actionPerformed(ActionEvent e) {
calc.setText(e.getActionCommand());
}
}