문제

I was wondering, would it be more efficient to do something like this:

   setVisible(false) // if the component is invisible

or like this:

   if(isVisible()){
      setVisible(false)
   }
도움이 되었습니까?

해결책

This has nothing to do with efficiency. Use the first one, it's simpler and probably already contains the visibility check inside the setVisible() method.

When you write code, don't try to think about efficiency, especially in ridiculously trivial cases like this. You're running on a multi GHz computer, so you're only wasting your time on micro-optimization like this.

다른 팁

setVisible() as implemented by JComponent already calls isVisible() so it's not more efficient to add a redundant call. In any case, it would be really improbable that it would be any kind of a performance bottle neck.

The only case in which your second example would make sense, is in a case of switching visibility on and off.

if(isVisible()){
   setVisible(false);
}else{
   setVisible(true);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top