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 analyze the Swing Set example

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

How to carry on the analysis of Swing Set example, aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

The Swing toolkit provides a variety of tools for creating a user interface and an almost dazzling selection of options for modifying the interface during the lifetime of the program. Careful use of these features can enable the interface to adapt to the needs of users and simplify the interaction process. Careless use of the same functionality can lead to programs that are very confusing or completely unavailable. You will modify the source code of the Swing Set-based sample application provided with Sun JDK; the UI of this application uses many dynamic features and can be an excellent starting point for understanding them.

Disable widgets

The simplest form of dynamic UI is a UI that grays out unavailable menu items or buttons. Disabling UI widgets is the same way as disabling all widgets. The setEnabled () function is a function of the Component class. Listing 1 shows the code for the disable button:

Listing 1. Disable button

Button.setEnabled (false)

As you can see, it's very simple. The key question is when a button should be enabled or disabled. A common design decision is to disable the button when it is not available. For example, many programs disable the Save button (and any corresponding menu item) when a file has not been modified since it was last saved.

An important warning about disable buttons is to remember to re-enable them at the appropriate time. For example, if there is a confirmation step between clicking the button and completing the action of the button, the button should be re-enabled even if the confirmation fails.

Adjustment range

Sometimes an application needs to dynamically adjust the scope of a numeric widget, such as Spinner or Slider. This may be a lot more complicated than it looks. In particular, Slider has secondary functions-- ticks, tick intervals, and labels-- which may need to be adjusted as the scope adjusts to avoid disaster.

The Swing Set example doesn't make any adjustments, so you need to modify it by connecting ChangeListener to a slider that can modify other sliders. Enter a new SliderChangeListener class, as shown in listing 2:

Listing 2. Change the range of the slider

Class SliderChangeListener implements ChangeListener {JSlider h; SliderChangeListener (JSlider h) {this.h = h;} public void stateChanged (ChangeEvent e) {JSlider js = (JSlider) e.getSource (); int I = js.getValue (); h.setMaximum (I); h.repaint ();}}

This seemingly meaningless operation actually makes a big difference. The label of the slider is generated whenever the label table is set. There is no special callback to the table for modification, so the new value added to the table does not have to have an effect; obviously, the null operation has the side effect of letting Swing know that it must update the display. (in case you think I invented this myself, note that the original Swing Set sample code includes such a call.)

The answer to the question on how to analyze the Swing Set example is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel to learn more about it.

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