问题描述
我有一个简单的Atari突破程序,长话短说,我的一个功能是允许用户调整窗口大小几秒钟,然后再次使窗口不可调整大小。一切正常,窗口从不可调整大小到可调整大小几秒钟。应该发生的是,在几秒钟之后,窗口应该停止接受用于调整窗口大小的输入(IE:不应该可调整大小)。唯一的问题是,无论何时将其设置为不可调整大小,如果您将光标拖动到窗口上以调整其大小,它将继续调整大小。它只会在您放开窗口后激活窗口的不可调整大小的状态。我的问题是,在你放开窗户之前我该如何做到这一点,一旦计时器启动就取消你对调整大小的控制?
I have a simple Atari breakout program, and long story short, one of my powerups is to allow the user to resize the window for a few seconds, then make the window non-resizable again.Everything works fine, and the window goes from being not-resizable, to being resizable for a few seconds. What's supposed to happen, is after the few seconds are up, the window should stop accepting input for resizing the window (IE: should not be resizable). The only problem, is that whenever it's supposed to be set to non-resizable, if you keep your cursor dragging on the window to resize it, it keeps resizing. It will only activate the non-resizable state of the window after you let go of the window. My question, is how do I make this happen before you let go of the window, taking away your control of resizing, once the timer is up?
PS:我想要编程,以便在调用命令后立即阻止您调整窗口大小,而不是等待您放开鼠标。有什么建议吗?
P.S: I want to program to immediately keep you from resizing the window once the command is called, not waiting for you to let go of the mouse. Any suggestions?
这是一个简化的案例:(你有6秒的时间调整窗口大小并玩它)
Here is a simplified case: (You are given 6 seconds to resize the window and play with it)
package test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;
public class Test {
public static void main(String[] args) {
JFrame testFrame = new JFrame();
testFrame.setResizable(true);
testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
long endingTime = System.currentTimeMillis() + 6000;
Timer testTimer = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if((endingTime - System.currentTimeMillis()) < 0){
testFrame.setResizable(false);
}
}
});
testFrame.setVisible(true);
testTimer.start();
}
}
推荐答案
使用Java的 Robot
类强制释放鼠标。我已经修改了下面的示例代码:
Use Java's Robot
class to force a mouse release. I've modified your example code below:
public static void main(String[] args) {
JFrame testFrame = new JFrame();
testFrame.setResizable(true);
testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Timer testTimer = new Timer(6000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
testFrame.setResizable(false);
Robot r;
try {
r = new Robot();
r.mouseRelease( InputEvent.BUTTON1_DOWN_MASK);
} catch (AWTException ex) {
ex.printStackTrace();
}
}
});
testFrame.setVisible(true);
testTimer.start();
}
这篇关于在setResizable(false)之后强制JFrame不调整大小。命令不会工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!