开发者

How can the screen be adjusted such that the cursor should point to the highlighted text

开发者 https://www.devze.com 2022-12-17 13:47 出处:网络
How can the screen be adjusted such that开发者_运维知识库 the cursor should point to the highlighted text

How can the screen be adjusted such that开发者_运维知识库 the cursor should point to the highlighted text on maximizing/minimizing or expanding the window size in java UI


You should look at the java.awt.Robot class, which can be used to move the mouse pointer using the mouseMove(int x, int y) method.

The it is a simply a case of obtaining the bounds of the Component containing the text and relocating the mouse to the center of the bounds; e.g.

// Application frame definition:
JFrame frame = ...

// Text field embedded within frame:
JTextField txtFld = ...

// Add WindowListener responsible for detecting when window state changes.
frame.addWindowListener(new WindowAdapter() {
  public void windowStateChanged(WindowEvent e) {
    // Reposition mouse over text field providing window isn't iconified.
    if (frame.getExtendedState() != JFrame.ICONIFIED) {
      Rectangle bounds = txtFld.getBounds();
      Robot.moveMouse(bounds.x / 2, bounds.y / 2);
    }
  }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消