2017-05-07 11 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のTextAreaのかJTextAreaに、パネルまたはのJPanel、 – mKorbel

+2

は上のSwingのチュートリアルからのセクションを読んではなく、古いAWTよりもスイングを使用することを喜ば[テキスト領域を使用する方法](のhttp://ドキュメント.oracle.com/javase/tutorial/uiswing/components/textarea.html)。 'TextDemo'コードはあなたのコードをより良く構造化する方法を示します。すべての拡張クラスや静的メソッドは必要ありません。チュートリアルの作業コードから始め、変更を加えてください。 – camickr

+1

@camickr彼はSwingについて質問していません。AWTでそれを行う方法を知りたがっています。 – CodingNinja

答えて

2

Java AWTでフレーム内にパネルを表示するにはどうすればよいですか?

変更することで固定することができます見られるようにコードを持つ2つの根本的な問題がありましたが、以下:

  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を使用すると、一般的に問題が発生します。静的としてマークされたメソッドのほとんど(すべてではないが)は、メソッドの呼び出しにオブジェクトのインスタンスを使用してコードで非静的に縮小する必要があります。
関連する問題