Java/Java

Java 16. GUI( Graphic User Interface )

shin96bc 2022. 3. 11. 17:28

(1) 배치 
    1) 모양 
             Component 1, 2, ....
         --------------------
              Container 

           ex)

                class A extends JFrame {
                     JButton b;
                     void init(){
                          b = new JButton("자바의 버튼");
                          /*ActionListener listener = new ActionListener(){
                               public void actionPerformed(ActionEvent e){
                                    b.setText("이벤트발생!! 클릭하셨네요^^");
                               }
                          };*/
                          ActionListener listener = (e) -> {
                               b.setText("람다식으로 이벤트 처리됨");
                          };
                          b.addActionListener(listener);
                          add(b);
                          setUI();
                     }
                     void setUI(){
                          setTitle("GUI Test Ver 1.0");
                          setSize(300, 200);
                          setLocation(200, 100);
                          setVisible(true);
                          setResizable(false);
                          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                      }

                }

 

     2) 배치관리자 ( LayoutManager ) 
          <1> BorderLayout 
          <2> GridLayout 
          <3> CardLayout 
          <4> FlowLayout 
               ex) 

                    class B extends JFrame {
                         Container cp;
                         JButton bE, bW, bS, bN, bC;
                         JPanel pN;
                         JButton bWpN;
                         JButton bEpN;
                         JPanel pC;
                         JButton bCpC;

                         void init(){
                              pN = new JPanel();
                              pN.setLayout(new BorderLayout());
                              bWpN = new JButton("서쪽at북패널");
                              bEpN = new JButton("동쪽at북패널");
                              pN.add(bWpN, BorderLayout.WEST);
                              pN.add(bEpN, BorderLayout.EAST);

                              pC = new JPanel();
                              pC.setLayout(new BorderLayout());
                              bCpC = new JButton("입력창");
                              pC.add(bCpC, BorderLayout.SOUTH);

                              bE = new JButton("동쪽");
                              bW = new JButton("서쪽");
                              bS = new JButton("남쪽");
                              bN = new JButton("북쪽");
                              pN.add(bN);
                              bC = new JButton("모니터창");
                              pC.add(bC);

                              cp = getContentPane();
                              cp.add(bE, BorderLayout.EAST);
                              cp.add(bW, BorderLayout.WEST);
                              cp.add(bS, BorderLayout.SOUTH);
                              //cp.add(bN, BorderLayout.NORTH);
                              cp.add(pN, BorderLayout.NORTH);
                              //cp.add(bC, BorderLayout.CENTER);
                              cp.add(pC, BorderLayout.CENTER);

                              setUI();
                         }

                    }

 

     3) Event 
          <1> 위임형 이벤트처리 모델 ( Delegation Event Model ) 
               1> 유명 내부 클래스

                    ex)

                         class A1 extends JFrame {
                              JButton b;

                              class MyHandler implements ActionListener {
                                   public void actionPerformed(ActionEvent e){
                                        b.setText("클릭됨 by 유명내부클래스");
                                   }
                              }
                              void init(){
                                   b = new JButton("버튼");
                                   b.addActionListener(new MyHandler());
                                   add(b); 
                                   setUI();
                              }

                         }

 

               2> 익명 내부 클래스

                    ex) 

                         class A2 extends JFrame {
                              JButton b;

                              void init(){
                                   b = new JButton("버튼");

                                   // 방법 1
                                   /*b.addActionListener(new ActionListener(){
                                           public void actionPerformed(ActionEvent e){
                                                b.setText("클릭됨 by 무(익)명내부클래스");
                                           }
                                      });*/

                                   // 방법 2
                                   /*ActionListener listener = new ActionListener(){
                                        public void actionPerformed(ActionEvent e){
                                             b.setText("클릭됨 by 무(익)명내부클래스");
                                        }

                                        b.addActionListener(listener);
                                     };*/

                                   // 방법 3
                                   /*ActionListener listener = (e) -> {
                                          b.setText("클릭됨 by 무(익)명내부클래스");
                                     };
                                     b.addActionListener(listener);
                                   */

                                   // 방법 4
                                   b.addActionListener((e) -> {
                                        b.setText("클릭됨 by 무(익명내부클래스");
                                   });
                              }

                         }

 

               3> 제 3 클래스

                    ex) 

                         class A3 extends JFrame {
                              JButton b;

                              void init(){
                                   b = new JButton("버튼");
                                   b.addActionListener(new MyHandler(this));
                              }

                         }

 

                        class MyHandler implements ActionListener {
                             A3 a3;
                             MyHandler(A3 a3){
                                  this.a3 = a3;
                             }
                             public void actionPerformed(ActionEvent e){
                                  //Object obj = e.getSource();
                                  //JButton b = (JButton)obj;
                                  a3.b.setText("클릭됨 by 제3클래스");
                             }
                        }

 

          <2>  자신 이벤트처리 모델 ( Self Event Model )
               ex) 

                    class A4 extends JFrame implements ActionListener {
                         JButton b;

                         void init(){
                              b = new JButton("버튼");
                              b.addActionListener(this);

                         }
                         @Override
                         public void actionPerformed(ActionEvent e){
                              b.setText("클릭됨 by 자신의 객체");
                         }

 

(2) 응용

     1) GUI1: https://github.com/SHIN96BC/KOSMO-2021-2022-JAVA/tree/main/16_GUI1

 

     2) GUI2: https://github.com/SHIN96BC/KOSMO-2021-2022-JAVA/tree/main/17_GUI2

'Java > Java' 카테고리의 다른 글

Java 17. String.format & SimpleDateFormat  (0) 2022.05.24
Java 15. NetWork  (0) 2022.03.11
Java 14. IO( Input Output )  (0) 2022.03.11
Java 13. 람다식  (0) 2022.03.11
Java 12. 쓰레드 ( Thread )  (0) 2022.03.11