Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the display effect of Swing container components

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to achieve the display effect of Swing container components, the article is very detailed, has a certain reference value, interested friends must read it!

The demonstration in this article uses the following lines of code to achieve the fade-in and fade-out effect of the container display:

/ * *

* A container that fades in and out to display content components

* @ author William Chen

, /

Public class GlassBox extends JComponent implements ActionListener {

/ / constant definition

Private static final int ANIMATION_FRAMES=50

Private static final int ANIMATION_INTERVAL=10

/ / frame index

Private int frameIndex

/ / clock

Private Timer timer

/ * * Creates a new instance of GlassBox * /

Public GlassBox () {

}

Public void paint (Graphics g) {

If (isAnimating ()) {

/ / content components that display the current transparency based on the current frame

Float alpha= (float) frameIndex/ (float) ANIMATION_FRAMES

Graphics2D g2d = (Graphics2D) g

G2d.setComposite (AlphaComposite.getInstance

(AlphaComposite.SRC_OVER, alpha))

/ / Renderer rendering mechanism

Super.paint (G2D)

} else {

/ / if it is * times, start the animation clock

FrameIndex=0

Timer=new Timer (ANIMATION_INTERVAL, this)

Timer.start ()

}

}

/ / determine whether animation is currently in progress

Private boolean isAnimating () {

Return timerplates null & & timer.isRunning ()

}

/ / turn off the clock and reinitialize

Private void closeTimer () {

If (isAnimating ()) {

Timer.stop ()

FrameIndex=0

Timer=null

}

}

/ / Animation clock handles events

Public void actionPerformed (ActionEvent e) {

/ / one frame forward

FrameIndex++

If (frameIndex > = ANIMATION_FRAMES)

/ / * one frame, close the animation

CloseTimer ()

Else// updates the current frame

Repaint ()

}

}

Using the GlassBox class as the ContentPane of JFrame can achieve the fade-in and fade effect of window content display. A more common way to use GlassBox is as a normal container, anywhere in the application. The demo program that accompanies this article adds GlassBox to JFrame's ContentPane as a normal container.

Swing's flexible and powerful extension and customization features benefit from its excellent model and architecture design. If the program is limited to a few simple applications such as Label and Button stacked together, it is difficult to realize the advantages of Swing container components, but will feel that it is not as easy to write as SWT because of its flexibility and complexity. However, when you face the complex and flexible interface requirements in real applications, the ability of Swing has gradually emerged.

The above is all the content of the article "how to achieve the display effect of Swing container components". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report