2017-05-07 62 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更常见的问题是修复它们。大多数(如果不是全部的话)标记为静态的方法应该通过使用对象实例调用方法的代码简化为非静态。