gpt4 book ai didi

javafx - Stage.setIconified() 和 Stage.isIconified() 运行不正常

转载 作者:行者123 更新时间:2023-12-04 12:51:43 25 4
gpt4 key购买 nike

我正在尝试编写一个简单的方法来切换我的窗口是否最小化(即图标化)。我有奇怪的行为。下面是说明问题的可运行代码。

我在 Gnome 3.20.4 和 XFCE 4.12 上得到了相同的结果。我还没有在任何其他环境中测试过这个。

  • 如果窗口未最大化,则代码按预期工作,但有时报告的状态不正确。这是下面代码的输出。我在文本输出与视觉输出不匹配的行旁边放置了注释( <-- )。


  • Windows 未最大化行为
    Before call  (Note: Window appears iconified)
    isIconified(): true
    isMaximized(): true

    Setting iconified to false

    After call (Note: Window now appears restored, not maximized)
    isIconified(): true <-- The window is visually not iconified
    isMaximized(): true <-- The window is visually not maximized


    Before call (Note: Window still appears restored, not maximized)
    isIconified(): false <-- One second later, the report is accurate
    isMaximized(): false

    Setting iconified to true

    After call (Note: Window now appears iconified)
    isIconified(): true
    isMaximized(): true

  • 如果窗口最大化,则代码不会按预期工作。相反,它是一个三步过程。第一个调用图标化,第二个调用恢复,第三个调用没有任何视觉变化。


  • Windows 最大化行为
    Before call (Note: Window appears iconified)
    isIconified(): true
    isMaximized(): true

    Setting iconified to false

    After call (Note: Window appears maximized)
    isIconified(): true <-- Does not match visual
    isMaximized(): true


    Before call (Note: Window appears maximized)
    isIconified(): true <-- Does not match visual
    isMaximized(): true

    Setting iconified to false

    After call (Note: Window appears maximized)
    isIconified(): false
    isMaximized(): false <-- Does not match visual


    Before call (Note: Window appears maximized)
    isIconified(): false
    isMaximized(): false <-- Does not match visual

    Setting iconified to true

    After call (Note: Window appears iconified)
    isIconified(): true
    isMaximized(): true

    <Now it loops>

    Before call (Note: Window appears iconified)
    isIconified(): true
    isMaximized(): true

    Setting iconified to false

    After call (Note: Window appears maximized)
    isIconified(): true <-- Does not match visual
    isMaximized(): true

    ...

    这是可运行的代码:
    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.stage.Stage;

    public class StageTest extends Application {

    Stage stage;

    public static void main ( String[] args ) {
    launch ( args );
    }

    @Override
    public void start ( Stage stage ) throws Exception {
    this.stage = stage;
    stage.setResizable( true );
    stage.show( );

    Thread thread = new Thread ( () -> {
    while ( true ) {
    Platform.runLater( () -> {
    toggleMinimized();
    } );
    try {
    Thread.sleep ( 1000 );
    } catch ( InterruptedException e ) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    });

    thread.setDaemon( true );
    thread.start();

    }

    public void toggleMinimized() {

    System.out.println ( "Before call" );
    System.out.println ( "\tisIconified(): " + stage.isIconified() );
    System.out.println ( "\tisMaximized(): " + stage.isIconified() );
    System.out.println ();

    if ( stage.isIconified() ) {
    System.out.println ( "Setting iconified to false" );
    System.out.println ();
    stage.setIconified( false );
    } else {
    System.out.println ( "Setting iconified to true" );
    System.out.println ();
    stage.setIconified( true );
    }


    System.out.println ( "After call" );
    System.out.println ( "\tisIconified(): " + stage.isIconified() );
    System.out.println ( "\tisMaximized(): " + stage.isMaximized() );
    System.out.println ();
    System.out.println ();
    }
    }

    最佳答案

    看来你的程序有错别字你有 2 个 stage.isIconified()'s 当你应该有一个

    public void toggleMinimized() {
    System.out.println ( "Before call" );
    System.out.println ( "\tisIconified(): " + stage.isIconified() );
    System.out.println ( "\tisMaximized(): " + stage.isIconified() );

    它应该是
    public void toggleMinimized() {
    System.out.println ( "Before call" );
    System.out.println ( "\tisIconified(): " + stage.isIconified() );
    System.out.println ( "\tisMaximized(): " + stage.isMaximized() );

    关于javafx - Stage.setIconified() 和 Stage.isIconified() 运行不正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45269173/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com