簡體   English   中英

Java - 顯示最小化的JFrame窗口

[英]Java - Show a minimized JFrame window

如果JFrame窗口最小化,有沒有辦法讓它重新聚焦?

我試圖讓它點擊某一點,然后恢復它。

            while (isRunning) {
                start = System.currentTimeMillis();
                frame.setState(Frame.ICONIFIED);
                robot.mouseMove(clickX, clickY);
                robot.mousePress(InputEvent.BUTTON1_MASK);
                frame.setState(Frame.NORMAL);
                Thread.sleep(clickMs - (System.currentTimeMillis() - start));
            }

如果您想從iconified恢復它,您可以將其狀態設置為normal

JFrame frame = new JFrame(...);
// Show the frame
frame.setVisible(true);

// Sleep for 5 seconds, then minimize
Thread.sleep(5000);
frame.setState(java.awt.Frame.ICONIFIED);

// Sleep for 5 seconds, then restore
Thread.sleep(5000);
frame.setState(java.awt.Frame.NORMAL);

這里的例子。

每當狀態發生變化時都會觸發WindowEvent ,以及處理這些觸發器的WindowListener接口。在這種情況下,您可以使用:

public class YourClass implements WindowListener {
  ...
  public void windowDeiconified(WindowEvent e) {
    // Do something when the window is restored
  }
}

如果您想要檢查另一個程序的狀態更改,則不存在“純Java”解決方案,只需要獲取窗口的ID

您可以將狀態設置為正常:

frame.setState(NORMAL);

完整示例:

public class FrameTest extends JFrame {

    public FrameTest() {
        final JFrame miniFrame = new JFrame();
        final JButton miniButton = new JButton(
          new AbstractAction("Minimize me") {
            public void actionPerformed(ActionEvent e) {
                miniFrame.setState(ICONIFIED);
            }
        }); 

        miniFrame.add(miniButton);
        miniFrame.pack();
        miniFrame.setVisible(true);

        add(new JButton(new AbstractAction("Open") {
            public void actionPerformed(ActionEvent e) {
                miniFrame.setState(NORMAL);
                miniFrame.toFront();
                miniButton.requestFocusInWindow();
            }
        }));

        pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM