2011년8월8일_비주얼한 JAVA 프로그래밍(swing)



시간 뉴뉴





참조(Reference)




DSCN3817 DSCN3818 DSCN3819



● swing에 관해 알아보기

image 

[출처] http://ko.wikipedia.org/wiki/Swing






● WindowExample1 프로젝트

//1

   1: //WindowExample1.java
   2: //간단한 윈도우를 만들고 label을 추가한다.
   3: package kr.ac.busanit.window;
   4:  
   5:  
   6: import java.awt.Container;
   7: import javax.swing.JFrame;
   8: import javax.swing.JLabel;
   9:  
  10: public class WindowExample1 {
  11:  
  12:     /**
  13:      * @param args
  14:      */
  15:     public static void main(String[] args) {
  16:         // TODO Auto-generated method stub
  17:         // 1
  18:         JFrame frame = new JFrame("Hello Java Program");
  19:         Container contentPane = frame.getContentPane();
  20:         JLabel label = new JLabel("Hello, Java");
  21:         contentPane.add(label);
  22:         frame.setSize(300,300);
  23:         frame.setVisible(true);
  24:         
  25:         frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
  26:         
  27:     }
  28:  
  29: }


<실행결과>

image


//2

   1: //WindowExample1.java
   2: //간단한 윈도우를 만들고 label을 추가한다.
   3: package kr.ac.busanit.window;
   4:  
   5:  
   6: import java.awt.BorderLayout;
   7: import java.awt.Container;
   8:  
   9: import javax.swing.JButton;
  10: import javax.swing.JFrame;
  11: import javax.swing.JLabel;
  12:  
  13: public class WindowExample1 {
  14:  
  15:     /**
  16:      * @param args
  17:      */
  18:     public static void main(String[] args) {
  19:         // TODO Auto-generated method stub
  20:         JFrame frame = new JFrame("My First Program");
  21:         Container contentPane = frame.getContentPane();        
  22:         JLabel label = new JLabel("안녕하세요!!");    //라벨 생성
  23:         JButton button = new JButton("OK");
  24:         
  25:         //contentPane.setLayout(new FlowLayout());
  26:         contentPane.setLayout(new BorderLayout());
  27:         
  28:         contentPane.add(label, BorderLayout.CENTER);            //content pane에 라벨 추가
  29:         contentPane.add(button, BorderLayout.WEST);        //content pane에 버튼 추가
  30:         
  31:         frame.setSize(300, 300);        //윈도우 크기
  32:         frame.setVisible(true);            //윈도우가 보이도록
  33:         frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
  34:         
  35:     }
  36:  
  37: }


<실행결과>

image


//문제

   1: //WindowExample1.java
   2: //간단한 윈도우를 만들고 label을 추가한다.
   3: package kr.ac.busanit.window;
   4:  
   5:  
   6: import java.awt.Container;
   7: import javax.swing.JFrame;
   8: import javax.swing.JLabel;
   9:  
  10: public class WindowExample1 {
  11:  
  12:     /**
  13:      * @param args
  14:      */
  15:     public static void main(String[] args) {
  16:         // TODO Auto-generated method stub
  17:         JFrame frame = new JFrame("My First Program");
  18:         Container contentPane = frame.getContentPane();
  19:         JLabel label = new JLabel("안녕하세요!!");    //라벨 생성
  20:         contentPane.add(label);
  21:         frame.setSize(300, 300);        //윈도우 크기
  22:         frame.setVisible(true);            //윈도우가 보이도록
  23:         frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
  24:         
  25:     }
  26:  
  27: }


<실행결과>

image



frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); 가 없다면…

image 

윈도우를 닫아도 종료되지 않아 빨간 사각형의 아이콘을 눌러 프로세스를 강제 종료해야함.