现在的位置: 首页 > 综合 > 正文

JDK5.0的新东西(边学边总结)

2013年09月15日 ⁄ 综合 ⁄ 共 3113字 ⁄ 字号 评论关闭

1.javax.swing.JFrame里

The part that most concerns Swing programmers is the content pane. When designing a frame, you add components into the content pane, using code such as the following:

Container contentPane = frame.getContentPane();
Component c = . . .;
contentPane.add(c);
Up to JDK 1.4, the add method of the JFrame class was defined to throw an exception with the message "Do not use JFrame.add(). Use JFrame.getContentPane().add() instead". As of JDK 5.0, the JFrame.add method has given up trying to reeducate programmers, and it simply calls add on the content pane.
Thus, as of JDK 5.0, you can simply use the call
frame.add(c);

2 The "for each" Loop

//Core Java 2 Volumn I,Chapter 3 Fundamental Programming Structures in Java,Array

JDK 5.0 introduces a powerful looping construct that allows you to loop through each element in an array (as well as other collections of elements) without having to fuss with index values.

Theenhanced for loop

for (variable : collection) statement

sets the given variable to each element of the collection and then executes the statement (which, of course, may be a block). Thecollection expression must be an array or an object of a class that implements the Iterable interface, such as ArrayList. We discuss array lists in Chapter5 and the Iterable interface in Chapter 2 of Volume 2.

For example,

for (int element : a)
   System.out.println(element);

prints each element of the array a on a separate line.

You should read this loop as "for each element in a". The designers of the Java language considered using keywords such as foreach and in. But this loop was a late addition to the Java language, and in the end nobody wanted to break old code that already contains methods or variables with the same names (such as System.in).

Of course, you could achieve the same effect with a traditionalfor loop:

for (int i = 0; i < a.length; i++)
   System.out.println(a[i]);

However, the "for each" loop is more concise and less error prone. (You don't have to worry about those pesky start and end index values.)

NOTE

The loop variable of the "for each" loop traverses the elements of the array, not the index values.

The "for each" loop is a pleasant improvement over the traditional loop if you need to process all elements in a collection. However, there are still plenty of opportunities to use the traditional for loop. For example, you may not want to traverse the entire collection, or you may need the index value inside the loop.

 

3 javax.swing.JComponent

新方法:

setComponentPopupMenu

public void setComponentPopupMenu(JPopupMenu popup)
Sets the JPopupMenu for this JComponent. The UI is responsible for registering bindings and adding the necessary listeners such that the JPopupMenu will be shown at the appropriate time. When the JPopupMenu is shown depends upon the look and feel: some may show it on a mouse event, some may enable a key binding.

If popup is null, and getInheritsPopupMenu returns true, then getComponentPopupMenu will be delegated to the parent. This provides for a way to make all child components inherit the popupmenu of the parent.

This is a bound property.

Parameters:
popup - - the popup that will be assigned to this component may be null
Since:
1.5
See Also:
getComponentPopupMenu()
getComponentPopupMenu

public JPopupMenu getComponentPopupMenu()
Returns JPopupMenu that assigned for this component. If this component does not have a JPopupMenu assigned to it and getInheritsPopupMenu is true, this will return getParent().getComponentPopupMenu() (assuming the parent is valid.)

Returns:
JPopupMenu assigned for this component or null if no popup assigned
Since:
1.5
See Also:
setComponentPopupMenu(javax.swing.JPopupMenu)

抱歉!评论已关闭.