在 setResizable(false) 不起作用后强制 JFrame 不调整大小
Forcing JFrame to not resize after setResizable(false) not working
我在让它工作时遇到了一些麻烦。我创建了一个游戏,在该游戏中我有一个道具,可以让 window 调整大小几秒钟。一切正常,window 从不可调整大小变为可调整大小几秒钟。应该发生的是,在几秒钟后,window 应该停止接受用于调整 window 大小的输入(即:不应调整大小)。唯一的问题是,每当它应该设置为不可调整大小时,如果您将光标拖到 window 上以调整它的大小,它就会不断调整大小。它只会在您放开 window 后激活 window 的不可调整大小状态。我的问题是,在您放开 window、取消您对调整大小的控制权之前,一旦计时器启动,我该如何实现这一点?
这是一个简化的案例:(您有 6 秒的时间来调整 window 的大小并使用它)
package test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;
public class Test {
/**
* @param args the command line arguments
*/
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
class 强制释放鼠标。我在下面修改了您的示例代码:
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();
}
我在让它工作时遇到了一些麻烦。我创建了一个游戏,在该游戏中我有一个道具,可以让 window 调整大小几秒钟。一切正常,window 从不可调整大小变为可调整大小几秒钟。应该发生的是,在几秒钟后,window 应该停止接受用于调整 window 大小的输入(即:不应调整大小)。唯一的问题是,每当它应该设置为不可调整大小时,如果您将光标拖到 window 上以调整它的大小,它就会不断调整大小。它只会在您放开 window 后激活 window 的不可调整大小状态。我的问题是,在您放开 window、取消您对调整大小的控制权之前,一旦计时器启动,我该如何实现这一点?
这是一个简化的案例:(您有 6 秒的时间来调整 window 的大小并使用它)
package test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;
public class Test {
/**
* @param args the command line arguments
*/
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
class 强制释放鼠标。我在下面修改了您的示例代码:
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();
}