简体   繁体   中英

How do I make a panel visible inside frame in Java AWT?

I am studying Java AWT to create GUI applications. I am working on the below code where I cannot make the panel visible inside the frame. Here is my code:

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

How can I make the panel visible inside the frame?

How do I make a panel visible inside frame in Java AWT?

There were two fundamental problems with the code as seen, which can be fixed by changing the following:

  1. Add the panel/text area to the GUI before setVisible(true) is called on the top level container.

    While it is possible to add components to a container after it has been made visible, they require special handling, and it is not necessary in this case.

  2. Add the text area to the panel!

Here is the code, turned into a Minimal, Complete, and Verifiable example by adding a main(String[]) method, with those two changes implemented, as well as more explanatory comments on other aspects of the code.

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

To add to / expand on the comments of @mKorbel & @camickr:

  1. Why use AWT? See this answer for many good reasons to abandon AWT components in favor of Swing.
  2. See Composition over inheritance .
  3. The use of static in GUIs more commonly causes problems, than fixes them. Most (if not all) of the methods marked as static should be reduced to non-static with the code using an instance of the object to call the method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM