Swing之为什么继承JFrame类便能实现一个窗体程序?

1. javax.swing.JFrame

1.1 JFrame 的层次结构

  • Class JFrame
    • java.lang.Object
      • java.awt.Component
        • java.awt.Container
          • java.awt.Window
            • java.awt.Frame
              • javax.swing.JFrame

在java api 8 中对Class JFrame是这样描述的:
java.awt.frame的扩展版本,它为JFC / Swing组件架构添加了支持。
Warning: Swing is not thread safe. For more information see Swing’s Threading Policy. Swing不是线程安全的。

1.2 JFrame中的 Nested Classes (内部类)

  • JFrame.AccessibleJFrame

此类实现JFrame类的可访问性支持。

 protected class AccessibleJFrame extends AccessibleAWTFrame {

        // AccessibleContext methods
        /**
         * Get the accessible name of this object.
         *
         * @return the localized name of the object -- can be null if this
         * object does not have a name
         */
        public String getAccessibleName() {
            if (accessibleName != null) {
                return accessibleName;
            } else {
                if (getTitle() == null) {
                    return super.getAccessibleName();
                } else {
                    return getTitle();
                }
            }
        }

        /**
         * Get the state of this object.
         *
         * @return an instance of AccessibleStateSet containing the current
         * state set of the object
         * @see AccessibleState
         */
        public AccessibleStateSet getAccessibleStateSet() {
            AccessibleStateSet states = super.getAccessibleStateSet();

            if (isResizable()) {
                states.add(AccessibleState.RESIZABLE);
            }
            if (getFocusOwner() != null) {
                states.add(AccessibleState.ACTIVE);
            }
            // FIXME:  [[[WDW - should also return ICONIFIED and ICONIFIABLE
            // if we can ever figure these out]]]
            return states;
        }
    } // inner class AccessibleJFrame

AccessibleJFrame 实现了awt中的AccessibleAWTFrame接口
此类实现窗口类的辅助功能支持。
它提供适合窗口用户界面元素的Java可访问性API的实现。

  • public String getAccessibleName() 对象的本地化名称,可以为空
    Component 接口的实现方法

实现逻辑是首先获取 accessibleName 不为空则返回accessibleName,否则获取窗体的title ,title为空则拿父类的accessibleName ,否则返回title

  • public AccessibleStateSet getAccessibleStateSet() 获取当前对象的状态 覆写了父类awt中Fram的方法
   public AccessibleStateSet getAccessibleStateSet() {
            AccessibleStateSet states = super.getAccessibleStateSet();

            if (isResizable()) {
                states.add(AccessibleState.RESIZABLE);
            }
            if (getFocusOwner() != null) {
                states.add(AccessibleState.ACTIVE);
            }
            // FIXME:  [[[WDW - should also return ICONIFIED and ICONIFIABLE
            // if we can ever figure these out]]]
            return states;
        }

逻辑:父类对象调用拿到窗口状态对象引用,获取表示此窗口是否可供用户调整,是则添加一个AccessibleState.RESIZABLE表示当前窗口不是固定的。获取该窗口有焦点,有则返回该窗口的子组件对象不为空时添加为AccessibleState.ACTIVE表示当前窗口为活动窗口。

1.3 JFram中的Fields (属性域)

  • protected AccessibleContextaccessibleContext 可访问的上下文属性。
  • static int EXIT_ON_CLOSE 退出应用程序默认窗口关闭操作。
  • protected JRootPane rootPane管 理ContentPane和此窗口的可选菜单中的JrootPane实例以及Glasspane。
  • protected boolean rootPaneCheckingEnabled 如果true,则将添加和setLayout调用将转发到ContentPane。

contentPane

swing中的四大重量级组件 JFrame, JDialog, JWindow, and JApplet swing中的四大重量级组件
swing中的四大重量级组件结构
JROOTPANE使用的自定义布局管理器可确保:
Glasspane填充了jrootpane的整个可视区域(边界 - 插孔)。
DiallayPane填充了Jrootpane的整个可视区域。
(边界 - 插入)
培养箱位于层盖的上边缘。
ContentPane填充整个可视区域,如果存在,减去菜单栏。

1.4 JFram Constructors (构造器)

  • JFrame() 初始无参构造器
  • JFrame(GraphicsConfiguration gc) 在屏幕设备的指定图形配置和空白标题中创建窗体
  • JFrame(String title) 创建一个具有指定标题的新初始隐形窗体
  • JFrame(String title, GraphicsConfiguration gc) 使用指定的标题和屏幕设备的指定图形配置创建JFrame。

1.5 JFram Method Summary (只描述常用方法)

  • public void repaint(long time,
    int x,
    int y,
    int width,
    int height)
 public void repaint(long time, int x, int y, int width, int height) {
        if (RepaintManager.HANDLE_TOP_LEVEL_PAINT) {
            RepaintManager.currentManager(this).addDirtyRegion(
                              this, x, y, width, height);
        }
        else {
            super.repaint(time, x, y, width, height);
        }
    }

在时间毫秒内重新粉刷此组件的指定矩形。
time : 更新前的最长时间以毫秒为单位
x,y坐标、宽度和高度

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李同学va

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值