2017-05-07 35 views
0

我正在學習Java AWT來創建GUI應用程序。我正在處理下面的代碼,我無法在框架內顯示面板。這是我的代碼:如何在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的,請使用搖擺,而不是老AWT – mKorbel

+2

閱讀從Swing教程中的部分上[如何使用文本區域(HTTP://文檔.oracle.com/JavaSE的/教程/ uiswing /組件/ textarea.html)。 'TextDemo'代碼將告訴你如何更好地構建你的代碼。不需要所有擴展類或靜態方法。從教程中的工作代碼開始,並進行更改.. – camickr

+1

@camickr他並不是在問Swing,他想知道如何用AWT做到這一點。 – CodingNinja

回答

2

如何在Java AWT框架中顯示面板?

有與所觀察到的代碼的兩個基本的問題,這可以通過改變固定在以下:

  1. 面板/文本區域添加到GUI setVisible(true)之前被稱爲在頂層容器。

    儘管可以在容器可見之後向組件添加組件,但它們需要特殊處理,在這種情況下不是必需的。

  2. 將文本區域添加到面板!

下面是代碼,變成了Minimal, Complete, and Verifiable example通過加入main(String[])方法,與實施這兩個改變,以及對代碼的其它方面更說明性註釋。

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評論:

  1. 爲什麼要使用AWT?看到this answer有很多很好的理由放棄AWT組件,轉而支持Swing。
  2. 請參閱Composition over inheritance
  3. 在GUI中使用static更常見的問題是修復它們。大多數(如果不是全部的話)標記爲靜態的方法應該通過使用對象實例調用方法的代碼簡化爲非靜態。