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

本文介绍了JDK5.0中引入的重要新特性,包括增强的for循环,使遍历数组更加简洁高效;以及Swing组件中的改进,如JFrame类的使用变化和JComponent类新增的方法,例如设置弹出菜单等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值