In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces what the core components of Swing are, which can be used for reference by interested friends. I hope you can learn a lot after reading this article.
When it comes to comparing awt components with Swing components, the first thing that is mentioned is that Swing is lightweight. Specifically, its buttons, frames and menus do not use localization control (native controls). All components, including rendering and event handling, are controlled by pure java. This gives us a lot of ways to create truly platform-independent components, and creating a custom component that looks the same on all platforms is not an easy task. this article demonstrates the process of creating custom components and highlights highlights, steps, and mistakes.
Basic part
Swing architecture overview this article provides a very good high-level overview of Swing structure and development (high-level overview). Although it is slightly cumbersome to follow some rules to create a component, the code will be easier to understand in the end. It follows the principle of "do not reinvent the wheel". Initially you will want to centralize everything into one class, including extension API, model processing (state and notification), transaction processing, layout, and drawing. But dividing it into multiple classes according to the MVC (model-view-controller) structure makes your component code easier to understand and easier to extend in the long run.
The main components of all Swing core components are as follows:
The ◆ component (component) class itself, which is responsible for providing API for creating, modifying, and querying the status of the component
The default implementation of the ◆ model interface and the model interface, which handles component business logic and component change notification
◆ UI delegate handles component layout, event handling (mouse and keyboard events), and component drawing.
This article will show how to create a custom component, similar to the new view slider in WINDOWS Vista Explorer. This component embeds a pop-up menu that looks like a slider. But it is different from the conventional JSlider, first of all, it will contain associated tags (labels) and icons (icon) options (control points), second, if the range is adjacent, (such as Small Icons and Medium Icons), can dynamically modify the icon size, if the range is not associated (such as Tiles-Details), the slider can only slide to these options, not the position between these options.
Component classes: UI Delegate assembly
The * classes of a custom component are the API of the component itself, which is simple enough and delegates most of the business logic to the model (see the next chapter). In addition, in order to set the appropriate UI delegate, you need to add a template (boilerplate) (see Enhancing Swing Applications for details). In the end, your code should look something like this:
PrivatestaticfinalStringuiClassID= "FlexiSliderUI"; publicvoidsetUI (FlexiSliderUIui) {super.setUI (ui);} publicvoidupdateUI () {if (UIManager.get (getUIClassID ())! = null) {setUI ((FlexiSliderUI) UIManager.getUI (this));} else {setUI (newBasicFlexiSliderUI ());}} publicFlexiSliderUIgetUI () {return (FlexiSliderUI) ui;} publicStringgetUIClassID () {returnuiClassID;}
One thing to note here is that you need to provide a reliable UI delegate. If the currently installed look and feel does not provide a special UI delegate, this UI delegate will handle component drawing, layout and event handling.
Model interface
This is probably the most important interface for this component. It will represent your component functions at the business level. The model interface should not contain any methods related to interface drawing (such as setFont or getPreferredSize). Our component will follow the LinearGradientPaint API and define the model as some range sequences:
PublicstaticclassRange {privatebooleanisDiscrete; privatedoubleweight; publicRange (booleanisDiscrete,doubleweight) {this.isDiscrete=isDiscrete; this.weight=weight;}...}
Set and query the API of range in the model
Public void setRanges (Range... Range); public int getRangeCount (); public Range getRange (int rangeIndex)
The model also provides get and set methods for the current value object:
Part of the model interface is the method of adding / removing change listeners (ChangeListeners), which follows the model interface style of Swing core components (see BoundedRangeModel)
Void addChangeListener (ChangeListener x); void removeChangeListener (ChangeListener x)
Model realization
The implementation class of the model is very simple, refer to DefaultBoundedRangeModel, and the change listener (ChangeListeners) is saved using EventListenerList. ChangeEvent is triggered when the model value is changed:
ProtectedvoidfireStateChanged () {ChangeEventevent=newChangeEvent (this); Object [] listeners=listenerList.getListenerList (); for (inti=listeners.length-2;i > = 0 ChangeListener [I] = ChangeListener.class) {if (listeners [I] = = ChangeListener.class) {((ChangeListener) listeners [I + 1]) .stateChanged (event) } Thank you for reading this article carefully. I hope the article "what are the core components of Swing" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and follow the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.