Differences between components and lightweight/heavyweight

后端 未结 4 1309
野的像风
野的像风 2020-12-16 18:24

What is the difference between JPanel and JFrame and relationship to lightweight, heavyweight?

相关标签:
4条回答
  • 2020-12-16 18:58

    JPanel is container that allows to put several UI components together. JFrame is a window written using Swing.

    All Swing components are so-called "lightwight" components because they are written in java. If for example you run Swing application and try to analyze it using UI analyzing tool (e.g. WinSpy in windows) you see only one element: the window (JFrame) itself. All other components are drawn from OS point of view.

    Heavyweight API - AWT uses portable elements provided by OS. Since java must be portable among various operating system AWT is very limited. It implements only the minimal subset of screen elements supported by all platforms. However the AWT elements are mapped directly to the appropriate platform elements, so UI discovery tool will see them. These elements are named "heavy weight".

    0 讨论(0)
  • 2020-12-16 19:06

    Heavyweight components like "AWT" components must be drawn using native GUI on a specific platform.

    Where lightweight components like "Swing" components are drawn by java and don't rely on native GUI.

    0 讨论(0)
  • 2020-12-16 19:07

    The basic difference between Swing and AWT is that Swing APIs are purely Java libraries i.e. they don't at all depend on the native libraries to draw graphical components. Because of this feature they provide a consistent look and feel on all platforms. AWT libraries require the support of native graphics libraries and some of their GUI components look different on different platforms. Moreover, Swing components are not inherently Thread safe, you explicitly have to write synchronized code to manipulate or redraw them whereas AWT components can be trusted in a multithreaded environment. AWT Components are called heavyweight components because of their dependency on native libraries. Swing components are called lightweight due to their independence of native libraries. Hence Swing operations are much faster because each and every operation is taken care by the Java runtime env and no delegation of events or commands to the native libraries is required.

    0 讨论(0)
  • 2020-12-16 19:18

    A JFrame is a Swing container with an interface box, and can be a standalone application (it has the top box with abilities to minimize, maximize, and exit) whereas a JPanel is everything a JFrame is (a Swing container) minus the ability to be a standalone. For a JPanel to work, it must be inside something like a frame, like a JFrame.

    The other answer has a good definition of lightweight and heavyweight components.

    use: I use JPanels within JFrames to easily run multithreaded programs that integrate together (I have a JPanel runnable class inside my class that extends JFrame, I run that as its own thread and the JPanel class can change the value of variables in the JFrame class). I use it to easily get different threads to interact with each other. **You can have multiple JPanels within a JFrame.

    0 讨论(0)
提交回复
热议问题