[英]Java toggle policy, FocusTraversalPolicy
I have a question, does this chunk of code set the newPolicy to null when togglePolicy.isSelected() ? 我有一个问题,当togglePolicy.isSelected()时,这段代码会将newPolicy设置为null吗?
if ("toggle".equals(e.getActionCommand())) {
frame.setFocusTraversalPolicy(togglePolicy.isSelected() ?
newPolicy : null);
}
}
Nope, it sets frame focusTraversalPolicy to null
when isSelected()
is false
(when it is not selected). 不,当
isSelected()
为false
时(未选择时),它将frame focusTraversalPolicy设置为null
。 if isSelected()
was true
it would set the newPolicy as the traversal policy. 如果
isSelected()
为true
,则将newPolicy设置为遍历策略。
I guess you stumbled upon the ternary operator . 我猜你偶然发现了三元运算符 。 It works as follows:
其工作方式如下:
<condition> ? <result if true> : <result if false>
In your case, you evaluate togglePolicy.isSelected()
first. 在您的情况下,您首先评估
togglePolicy.isSelected()
。 If the evaluation returns true
, than you set the policy to newPolicy
. 如果评估返回
true
,则将策略设置为newPolicy
。 Else the policy will be set to null
. 否则,该策略将设置为
null
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.