JDK5.0的新東西(邊學邊總結)

來源:互聯網
上載者:User

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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.