【问题标题】:How do I make a panel visible inside frame in Java AWT?如何在 Java AWT 中使面板在框架内可见?
【发布时间】:2017-05-07 16:25:35
【问题描述】:

我正在学习 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);
        }
    }

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

【问题讨论】:

  • 扩展 Frame 或扩展 JFrame、aaand TextArea 或 JTextArea、Panel 或 JPanel,请使用 Swing 而不是旧的 AWT
  • 阅读 How to Use Text Areas 上的 Swing 教程部分。 TextDemo 代码将向您展示如何更好地构建代码。不需要所有扩展类或静态方法。从教程中的工作代码开始并进行更改..
  • @camickr 他不是在问 Swing,他想知道如何用 AWT 来做。
  • @theProgrammer101,无论你使用AWT还是Swing,逻辑都是一样的,只是组件名称发生了变化。我的评论是因为当前代码的设计很糟糕。没有理由不能使用 Swing 教程中的代码结构。仅仅因为有人提出问题并不意味着他们知道他们在说什么。为什么在提出问题时 OP 包含“Swing 标签”?无论如何,如果建议不合适,OP 会回复并澄清问题。
  • @theProgrammer101 是不是因为 OP 不理解 AWT 和 Swing 之间的区别?建议 OP 重新考虑他们对至少仍在使用的 API 的选择是否不可取? OP是否有理由必须使用AWT?由于 Swing 位于 AWT 之上,并且完全缺乏 AWT 教程,因此将 Swing 引用为可行的信息源并没有错

标签: java swing awt panel frame


【解决方案1】:

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

所见代码存在两个基本问题,可以通过更改以下内容来解决:

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

    虽然可以在容器可见后将组件添加到容器中,但它们需要特殊处理,在这种情况下没有必要。

  2. 将文本区域添加到面板!

这是代码,通过添加 main(String[]) 方法转换为 Minimal, Complete, and Verifiable example,实现了这两个更改,以及对代码其他方面的更多解释性 cmets。

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 的 cmets:

  1. 为什么要使用 AWT?请参阅this answer,了解放弃 AWT 组件以支持 Swing 的许多充分理由。
  2. Composition over inheritance
  3. 在 GUI 中使用 static 更容易导致问题,而不是修复问题。大多数(如果不是全部)标记为静态的方法应简化为非静态方法,代码使用对象实例调用该方法。

【讨论】:

    猜你喜欢
    • 2022-08-05
    • 1970-01-01
    • 1970-01-01
    • 2010-10-16
    • 2016-06-30
    • 2013-04-19
    • 2023-03-19
    • 2011-10-01
    • 1970-01-01
    相关资源
    最近更新 更多