如何在Java AWT中使面板在框架内可见?

时间:2017-05-07 16:25:36

标签: java swing awt panel frame

我正在研究Java AWT来创建GUI应用程序。我正在处理下面的代码,我无法在框架内看到面板。这是我的代码:

        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);
        }
    }

如何在框架内看到面板?

1 个答案:

答案 0 :(得分:2)

  

如何在Java AWT中使面板在框架内可见?

看到的代码有两个基本问题,可以通过更改以下内容来修复:

  1. 在顶级容器上调用setVisible(true)之前,将面板/文本区域添加到GUI。

    虽然可以在容器可见之后将其添加到容器中,但它们需要特殊处理,在这种情况下不需要。

  2. 将文本区域添加到面板中!
  3. 以下是代码,通过添加main(String[])方法变为Minimal, Complete, and Verifiable example,实现了这两个更改,以及对代码其他方面的更多解释性注释。

    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通常会导致问题,而不是解决问题。标记为static的大多数(如果不是全部)方法应该使用对象实例调用方法,将代码简化为非静态方法。
相关问题