2017-05-07 13 views
0

GUI 응용 프로그램을 작성하기 위해 Java AWT를 연구 중입니다. 프레임 안의 패널을 볼 수없는 아래의 코드를 작업 중입니다. 여기 내 코드는 다음과 같습니다.Java AWT에서 프레임 내부에 패널을 표시하려면 어떻게합니까?

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

     /** 
     * 
     * @author kiran 
     */ 
     public class UserInterface 
     { 
      Frame UI; 
      private static double UIWidth, UIHeight; 



      /** 
      * Constructs User Interface 
      */ 
      public UserInterface() 
      { 
       UI = new Frame("frame"); 
       Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
       UIWidth = screenSize.getWidth(); 
       UIHeight = screenSize.getHeight(); 
       buildFrame(); 
       buildMessageInputArea(); 
      } 
      /** 
      * returns the width of the UI 
      * @return returns the width of the UI 
      */ 
      public static double getUIWidth() 
      { 
       return UIWidth; 
      } 

      /** 
      * returns the width of the UI 
      * @return returns the width of the UI 
      */ 
      public static double getUIHeight() 
      { 
       return UIHeight; 
      } 

      /** 
      * Builds the frame 
      */ 
      private void buildFrame() 
      { 
       UI.setSize((int)UIWidth,(int)UIHeight*96/100); 
       UI.setVisible(true); 
       UI.setLayout(new FlowLayout()); 
       UI.addWindowListener(new Actions()); 
      } 

      private void buildMessageInputArea() 
      { 
       Panel current = new TextAreaPanel().getPanel(); 
       current.setVisible(true); 
       UI.add(current); 

      } 
     } 

     class TextAreaPanel extends Frame 
     { 
      private Panel textAreaPanel; 
      TextArea msgInputArea; 

      public TextAreaPanel() 
      { 
       textAreaPanel = new Panel(); 
       msgInputArea = new TextArea(1000,(int)UserInterface.getUIWidth() * 80/100); 
      } 

      private void addTextArea() 
      { 
       textAreaPanel.add(msgInputArea); 
      } 

      public Panel getPanel() 
      { 
       return textAreaPanel; 
      } 

    } 

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

프레임 내부에서 패널을 어떻게 보이게 할 수 있습니까?

+4

이 프레임을 확장하거나 JFrame의, aaand 텍스트 영역 또는 JTextArea에, 패널 또는 JPanel을 확장, [텍스트 영역을 사용하는 방법 (HTTP에 스윙 튜토리얼에서 섹션을 읽어 – mKorbel

+2

된 AWT보다는 스윙을 사용하시기 바랍니다 : // 문서 .oracle.com/javase/tutorial/uiswing/components/textarea.html). 'TextDemo' 코드는 코드를 더 잘 구조화하는 방법을 보여줍니다. 모든 확장 클래스 나 정적 메소드가 필요하지 않습니다. 자습서의 작업 코드로 시작하여 변경하십시오. – camickr

+1

@camickr 그는 Swing에 대해 묻지 않습니다. AWT로 어떻게하는지 알고 싶어합니다. – CodingNinja

답변

2

Java AWT에서 프레임 내에서 패널을 볼 수있게하려면 어떻게해야합니까?

변경하여 고정 할 수 있습니다와 같이 코드에 두 가지 근본적인 문제가 있었다 다음

  1. 최상위 수준에라고 setVisible(true) 전에 GUI에 패널/텍스트 영역을 추가 컨테이너.

    구성 요소를 표시 한 후에 컨테이너에 구성 요소를 추가 할 수는 있지만 특별한 처리가 필요하므로이 경우 필요하지 않습니다.

  2. 패널에 텍스트 영역을 추가하십시오! 여기

코드는 두 구현 변화뿐만 아니라, 코드의 다른 부분에 대한 설명 주석 더불어 main(String[]) 방법을 추가하여 Minimal, Complete, and Verifiable example 설정되어 있습니다.

  1. 를 사용하는 이유 AWT :

    import java.awt.*; 
    import java.awt.event.*; 
    
    public class UserInterface { 
    
        Frame UI; 
        private static double UIWidth, UIHeight; 
    
        public static void main(String[] args) { 
         Runnable r =() -> { 
          new UserInterface(); 
         }; 
         EventQueue.invokeLater(r); 
        } 
    
        /** 
        * Constructs User Interface 
        */ 
        public UserInterface() { 
         UI = new Frame("frame"); 
         // setting a GUI to full screen while accounting for the task 
         // bar can be achieved in a single line of code. 
         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
         UIWidth = screenSize.getWidth(); 
         UIHeight = screenSize.getHeight(); 
         // these need to be called in the reverse order to ensure the 
         // components are added before the GUI is set visible. 
         buildMessageInputArea(); 
         buildFrame(); 
        } 
    
        /** 
        * returns the width of the UI 
        * 
        * @return returns the width of the UI 
        */ 
        public static double getUIWidth() { 
         return UIWidth; 
        } 
    
        /** 
        * returns the width of the UI 
        * 
        * @return returns the width of the UI 
        */ 
        public static double getUIHeight() { 
         return UIHeight; 
        } 
    
        /** 
        * Builds the frame 
        */ 
        private void buildFrame() { 
         UI.setSize((int) UIWidth, (int) UIHeight * 96/100); 
         UI.setVisible(true); 
         UI.setLayout(new FlowLayout()); 
         UI.addWindowListener(new Actions()); 
        } 
    
        private void buildMessageInputArea() { 
         Panel current = new TextAreaPanel().getPanel(); 
         current.setVisible(true); 
         UI.add(current); 
        } 
    } 
    
    // does not need to be a fram 
    //class TextAreaPanel extends Frame { 
    class TextAreaPanel { 
    
        private Panel textAreaPanel; 
        TextArea msgInputArea; 
    
        public TextAreaPanel() { 
         textAreaPanel = new Panel(); 
         // these number represent columns and rows, not pixels! 
         //msgInputArea = new TextArea(1000, (int) UserInterface.getUIWidth() * 80/100); 
         msgInputArea = new TextArea(40, 60); 
         // add the text area to the panel! 
         textAreaPanel.add(msgInputArea); 
        } 
    
        /** not called by anything else 
        private void addTextArea() { 
         textAreaPanel.add(msgInputArea); 
        } 
        **/ 
    
        public Panel getPanel() { 
         return textAreaPanel; 
        } 
    } 
    
    // This can be achieved in a single line of code 
    class Actions extends WindowAdapter { 
    
        @Override 
        public void windowClosing(WindowEvent c) { 
         System.exit(0); 
        } 
    } 
    

    @mKorbel & @camickr의 의견을 확장/추가하려면? Swing을 위해 AWT 구성 요소를 포기해야하는 많은 이유에 대해서는 this answer을 참조하십시오.

  2. Composition over inheritance을 참조하십시오.
  3. GUI에서 static을 사용하면 문제를 수정하는 것보다 일반적으로 문제가 발생합니다. static으로 표시된 메소드의 대부분 (전부는 아님)은 메소드를 호출하는 객체의 인스턴스를 사용하는 코드로 비 정적으로 축소되어야합니다.