简体   繁体   English

Java切换策略FocusTraversalPolicy

[英]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设置为遍历策略。

you may want to take a look at here 您可能要在这里看看

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM