Как сделать что бы нажимать можно было лишь на какую-то часть в JFrame? - Java
Формулировка задачи:
Как сделать что бы нажимать можно было лишь на какую-то часть в JFrame? А за её пределами нельзя, пока она открыта.
Решение задачи: «Как сделать что бы нажимать можно было лишь на какую-то часть в JFrame?»
textual
Листинг программы
import javax.swing.*;
import java.awt.*;
public class Main implements Runnable {
private Robot robot = new Robot();
private JFrame frame;
private Point p;
private boolean b = false;
private Main() throws AWTException {
frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
frame.setUndecorated(true);
frame.setPreferredSize(new Dimension(640, 420));
frame.setAlwaysOnTop(true);
frame.setFocusable(true);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[]args) {
try {
Main main = new Main();
Thread thread = new Thread(main);
thread.start();
} catch (AWTException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true) {
Point mousePos = MouseInfo.getPointerInfo().getLocation();
Rectangle bounds = frame.getBounds();
bounds.setLocation(frame.getLocationOnScreen());
if (!bounds.contains(mousePos)) {
p = frame.getLocationOnScreen();
robot.mouseMove(p.x * 2, p.y * 2);
frame.setExtendedState(JFrame.NORMAL);
}
}
}
}