简体   繁体   English

JFrame如何工作? 在内心深处,它如何绘制东西?

[英]How does JFrame work? Deep inside, how does it draw stuff?

Typically, when I create a class, for example Customer , I give it some data fields, ie public int IdNumber; 通常,当我创建一个类,例如Customer ,我给它一些数据字段,即public int IdNumber; and some methods, ie public String getName(){...} . 和一些方法,即public String getName(){...} But that's pretty much it. 但这就是它。 I can't go beyond that and start playing with graphics - I can only manipulate and organize data as far as the class allows. 我不能超越它并开始玩图形 - 我只能操纵和组织数据,只要类允许。

I can't get my head around what is happening inside JFrame. 我无法理解JFrame中发生的事情。 Whoever wrote the class JFrame, how did they write a class that can make a box appear on screen? 谁写了JFrame类,他们是怎么写一个可以让一个盒子出现在屏幕上的类? What is happening internally that causes this to happen? 内部会发生什么导致这种情况发生? Is there anyway to emulate it? 无论如何都要模仿它吗?

The same question applies to all graphics-based Java classes. 同样的问题适用于所有基于图形的Java类。 I'm really curious to know how it works, as it bothers me each time I use one of them. 我真的很想知道它是如何工作的,因为每次我使用其中一个都会困扰我。

If you are curious about how java is implemented you should take a look at the source code. 如果您对Java的实现方式感到好奇,那么您应该看一下源代码。 http://openjdk.java.net/projects/jdk7/ would be a start. http://openjdk.java.net/projects/jdk7/将是一个开始。

Of course this would only give you insight into that particular implementation and doesn't mean that your java is implemented the same way. 当然,这只会让您深入了解该特定实现,并不意味着您的java以相同的方式实现。

Java started with awt (Abstract Windowing Toolkit) and later introduced swing . Java以awt (Abstract Windowing Toolkit)开始,后来引入了swing

In AWT the platform event handling loop is hooked into, and events are packed in own java classes and one single (non-parallel) event handling queue/thread handles them, one after another. AWT中 ,平台事件处理循环被挂钩,事件被打包在自己的java类中,一个(非并行)事件处理队列/线程一个接一个地处理它们。 Swing inherits this. Swing继承了这一点。

In AWT every GUI component, like radio button or menu item, has a native code "peer" control, the platform provided component. AWT中,每个GUI组件(如单选按钮或菜单项)都具有本机代码“对等”控件,即平台提供的组件。 There is a parallel set of java classes and their C counterpart. 有一组并行的java类和它们的C对应物。 Especially interesting is the java Graphics class which allows custom drawing of lines, rectangles and such. 特别有趣的是java Graphics类,它允许自定义绘制线条,矩形等。 It is peered under Windows with a CDC (Device Context) - presumably. 据推测,它在Windows下与CDC(设备上下文)对等。

In Swing most of the platform components are emulated, that is, recreated oneself: the drawing, the mouse handling, and so on. Swing中,大多数平台组件都是模拟的,即自己重新创建:绘图,鼠标处理等。 So the native part is simpler, say maybe a CWnd (Window component) with custom drawing. 所以原生部分更简单,比如可能是带有自定义绘图的CWnd(Window组件)。

Swing can achieve a more consistent and more feature rich functionality. Swing可以实现更一致且功能更丰富的功能。 You can imagine that setting a backgroud color on an AWT radio button might not be possible, or using HTML on a label or tool tip. 您可以想象在AWT单选按钮上设置背景颜色可能是不可能的,或者在标签或工具提示上使用HTML。 Also Swing can do skinning, themes, LookAndFeels. Swing也可以做皮肤,主题,LookAndFeels。 The System look and feel being a close imitation of the platform components. 系统的外观和感觉是对平台组件的近似模仿。 Especially Swing components are more light weight , as not every component has a native peer control to be handled in C. 特别是Swing组件的重量更轻 ,因为并非每个组件都有一个本地对等控件可以在C中处理。

Now SWT was a later initiative of IBM realized in eclipse for AWT reloaded. 现在SWT是IBM在eclipse中为AWT重新加载实现的后来的一项计划。 Not as customizable as Swing but intended to be platform near. 不像Swing那样可定制,但打算靠近平台。

You should forget using AWT components, and if not programming for eclipse RCP also SWT. 您应该忘记使用AWT组件,如果没有为eclipse RCP编程也可以使用SWT。

So: global platform events like mouse click, repaint request are translated to Java events. 所以:鼠标点击,重绘请求等全局平台事件被转换为Java事件。 There is a container hierarchy of JFrame, JPanels, JScrollPanes, JComponents. 有一个JFrame,JPanels,JScrollPanes,JComponents的容器层次结构。 An event is dispatched to the handling components, on which for example paintComponent is called: 将事件分派给处理组件,例如调用paintComponent:

@Override
public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g; // A later introduced class that can do more.
    g2.draw...
}

With JavaFX there comes a new player, which is IMHO not yet fully mature, but usable in non-production code. 有了JavaFX ,就会出现一个新的播放器,即恕​​我直言,尚未完全成熟,但可用于非生产代码。 It enables effects/animations, rotations, transformations, light. 它可以实现效果/动画,旋转,变换,光。 So a 2D - 4D rendering, based on like platform rendering. 所以2D-4D渲染,基于类似平台渲染。 Also it is property based, so a check box would not necessarily be bound to a boolean, but a boolean property observing and notifying changes. 它也是基于属性的,因此复选框不一定会绑定到布尔值,而是一个布尔属性,用于观察和通知更改。 I need still some practical experience, to conceive an optimal architecture with it. 我还需要一些实践经验,用它构思一个最佳的架构。

How does a box appear on a screen? 框如何出现在屏幕上? This functionality is offered by the operating system to the JVM (by the X Window System on Linux). 操作系统将此功能提供给JVM(通过Linux上的X Window系统)。

On the Java level, JFrame inherits from java.awt.Window, which has the "native peers" provided by the native windowing system . 在Java级别,JFrame继承自java.awt.Window,它具有本机窗口系统提供的“本机对等”。

If you really want to understand it, it is better if you try to create some windows using C only. 如果您真的想了解它,那么最好只尝试使用C创建一些窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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