In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use Swing dynamic interface design technology, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
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. This paper introduces the technology and system of Swng dynamic interface design, and provides help to build an effective interface. You will modify the source code of the SwingSet2-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 Swing dynamic interface design is 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
1 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 SwingSet2 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
1 class SliderChangeListener implements ChangeListener {2 JSlider h; 3 4 SliderChangeListener (JSlider h) {5 this.h = h; 6} 7 8 public void stateChanged (ChangeEvent e) {9 JSlider js = (JSlider) e.getSource (); 10 int I = js.getValue (); 11 h.setMaximum (I); 12 h.repaint () 13} 14}
When you create a third horizontal slider (the slider in the initial example is marked at each unit, labeled at 5, 10, 11, and so on), a new SliderChangeListener is also created, which passes the slider as a constructor parameter. When a third vertical slider is created (ranging from 0 to 100), the new SliderChangeListener is added to it as a change listener. This works roughly as expected: adjusting the vertical slider changes the range of the horizontal slider.
Unfortunately, ticks and labels don't work well at all. When the range becomes not too large, the label at every five ticks works correctly, but the extra tag at tick 11 quickly becomes a usability issue, as shown in figure 1:
Figure 1. Labels that run together
Update ticks and labels
In Swing dynamic interface design, the obvious solution is to simply set the tick interval on the horizontal slider whenever the slider's * * value is updated, as shown in listing 3:
Listing 3. Set the tick interval
1 / / DOES NOT WORK 2 int tickMajor, tickMinor; 3 tickMajor = (I > 5)? (I / 5): 1; 4 tickMinor = (tickMajor > 2)? (tickMajor / 2): tickMajor; 5 h.setMajorTickSpacing (tickMajor); 6 h.setMinorTickSpacing (tickMinor); 7 h.repaint ()
Listing 3 is correct so far, but it doesn't cause any changes to the labels drawn on the screen. Labels must be set separately using setLabelTable (). Add an extra line to fix it:
1 h.setLabelTable (h.createStandardLabels (tickMajor))
This still occurs when there is an error such as the label that was originally set at scale 11. Of course, the original intention was to always have a label at the far right end of the slider. You can do this by deleting the old tag (before setting the new * value) and then adding a new one. This code "almost" works:
Listing 4. Replace label
1 public void stateChanged (ChangeEvent e) {2 JSlider js = (JSlider) e.getSource (); 3 int I = js.getValue (); 4 5 / / clear old label for top value 6 h.getLabelTable (). Remove (h.getMaximum ()); 7 8 h.setMaximum (I); 9 10 int tickMajor, tickMinor; 11 tickMajor = (I > 5)? (I / 5): 1; 12 tickMinor = (tickMajor > 2)? (tickMajor / 2): tickMajor; 13 h.setMajorTickSpacing (tickMajor); 14 h.setMinorTickSpacing (tickMinor); 15 h.setLabelTable (h.createStandardLabels (tickMajor)); 16 h.getLabelTable (). Put (new Integer (I), 17 new JLabel (new Integer (I). ToString (), JLabel.CENTER)); 18 h.repaint (); 19}
If I had told you once, I would have told you twice.
What I use almost means that although the code in listing 4 removes the tag at tick 11, it doesn't add a new tag at tick I; instead, it only sees tags with intervals of tickMajor. First of all, this solution is quite annoying:
Listing 5. Force display updates
1 h.setLabelTable (h.getLabelTable ())
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 empty operation in listing 5 has the side effect of letting Swing know that it must update the display. (in case you think I invented it myself, note that the original SwingSet code includes such a call.)
This leaves only one problem. The very reasonable expectation that the label appears at the end of the slider sometimes causes the two labels to be directly adjacent to or even overlap each other, as shown in figure 2:
Figure 2. Overlapping labels at the end of the slider
Many ways to solve this problem are feasible. Write your own code to populate the tag table with values and stop the previous sequence so that the * tags in the sequence are somewhat isolated from the end of the slider. I'll leave this to you as homework.
In many cases, it is practical to restrict menu changes in order to enable and disable menu items. This method is vulnerable to general warnings for disabled items: avoid making the program unavailable by accidentally disabling important items.
It is also possible to add or remove menu items or submenus. Modifying JMenuBar is not so easy; there is no interface to remove and replace individual menus from the toolbar. If you want to modify the toolbar (instead of adding a menu to the far right), you need to make a new toolbar and replace the old one with it.
Modifying a single menu takes effect immediately; you don't have to build a menu before connecting it to a toolbar or another menu. When you need to modify the selection of menu options, the easiest way is to modify the selected menu. You may still want to add or remove complete menus, and it is not particularly difficult to do so. Listing 6 shows a simple example of the method before inserting a menu into a given index in the menu bar. This example assumes that the JMenuBar you want to replace is connected to the JFrame object, but anything that enables you to get and set menu bars works the same way:
Listing 6. Insert a menu into the menu bar
1 public void insertMenu (JFrame frame, JMenu menu, int index) {2 JMenuBar newBar = new JMenuBar (); 3 JMenuBar oldBar = frame.getJMenuBar (); 4 MenuElement [] oldMenus = oldBar.getSubElements (); 5 int count = oldBar.getMenuCount (); 6 int I; 7 8 for (I = 0; I < count + + I) {9 if (I = = index) 10 newBar.add (menu); 11 newBar.add ((JMenu) oldMenus [I]); 12} 13 frame.setJMenuBar (newBar); 14}
I didn't try to compile the above code like this at the beginning; this is the final version, which has been well fixed so that it can run and reflect some interesting strange things. At first glance, the obvious way to do this seems to be to use getComponentAtIndex (), but this approach has been opposed. Fortunately, getSubElements () is good enough. Casting to JMenu for newBar.add () might be safe, but I don't like it. The getSubElements () interface operates not only on the menu bar but also on the menu, which may have several types of child elements, and JMenu is the only element that can be added to the JMenuBar. So you must cast the element to JMenu to pass it to the JMenuBar.add () method. Unfortunately, if future API revisions allow elements other than JMenu types to be added to JMenuBar, you no longer need to cast JMenu for returned elements.
The code in listing 6 reflects another rather subtle interface oddity; the number of menus must be cached in advance. When menus are added to the new menu bar, they are removed from the old menu bar. Although it looks similar, the code in listing 7 does not work because the loop ends prematurely:
Listing 7. The cycle ends too early.
1 / / DOES NOT WORK 2 for (I = 0; I < oldBar.getMenuCount (); + + I) {3 if (I = = index) 4 newBar.add (menu); 5 newBar.add ((JMenu) oldMenus [I]); 6}
The loop in listing 7 copies only half the number of menus. For example, if there are four menus on the start menu bar, it copies the first two menus. After copying * * menus, I has a value of 1 and getMenuCount () returns 3; after copying the second menu, I has a value of 2 and getMenuCount () returns 2, so the loop ends. I didn't find any documentation that describes features such as removing menus from another menu bar by adding menus to the menu bar, so it may not be intentional. However, it is easy to achieve this goal.
It's a little easier to delete a menu from the menu bar; just copy all other menus from the old menu bar to the new menu bar and delete the menu. It's easy!
If the interface uses many dynamic menu updates, it might be better to create a set of menu bars and switch between them instead of updating them dynamically all the time. However, if you change the menu so quickly, it may drive the user completely crazy.
Corrigendum: in the draft phase of this article, I ignored the list of inheritance methods of the JMenuBar class. In fact, it has remove and add methods that can be used to delete and insert at the specified index. Another lesson is to look at inherited methods rather than just class-specific methods.
Resize the window
Fortunately, for most cases, window resizing is automatic. But you need to consider some of the effects of resizing. In very small windows, button bars, menu bars, and similar functions can become problematic. The graphics panel of the hypervisor itself needs to respond to resizing events. Let Swing handle the wrapper around the UI element, but keep a close eye on the size of the component; don't get the size of the component once and then use these values all the time.
More subtly, some design decisions, such as the density of ticks on the slider, may be moderately updated in response to window resizing events. A slider that is 100 pixels wide cannot have as many readable tags as a slider that is 400 pixels wide. You may want to add new and useful features to make UI more useful on large monitors.
However, in most cases, window resizing can be ignored. What you should not do is block or rewrite it unnecessarily. Convenient tools on the side of the window in the layout code are not required. The smallest window size may be understandable, but it is important for people to adjust the window to the size they need.
General principles
The Swing toolkit provides a great deal of flexibility in Swing dynamic interface design. If used carefully, the option to dynamically update the interface can greatly simplify the interface; for example, the user can easily display the menu only when the menu option is applied. Unfortunately, some API features may cause this approach to produce some bizarre behavior, and side effects and interactions are not always documented as you might expect. If you have the idea of using a dynamic interface, be prepared to spend some extra time on debugging. You may get out of the predicament of the Swing library and find yourself dealing with unexpected behavior and / or bug.
Don't let the lack of obvious implementation discourage you. As the JMenuBar example in this article shows, you can implement a task yourself, though somewhat indirectly, even when API does not support it. Try not to go to extremes. Dynamic UI works only when it makes users aware of their inherent limitations. Ideally, users may not even notice interface changes. If the only time they can use the program's Object menu is when they make an object selected, then the rest of the time they won't mind that the menu doesn't exist.
On the other hand, if there is a possibility that the user cannot guess why an option is not available, it might be better for the user to try and get a message containing information. This is especially important for some operations. If the save option is disabled and I want to save the data, then this will not work. The program may think that the data has been saved, but why not let me save it anyway? If there is a special reason why the file cannot be saved, I may want to know why.
Although it has been studied for many years, interface design is still a relatively new field in many aspects, and few experiments have been carried out. Swing dynamic interface design is a good feature, which can make UI clearer, simpler and more responsive. Adding dynamic features can range from a few minutes of work to a lot of time.
These are all the contents of the article "how to use Swing dynamic Interface Design Technology". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more 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.
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.