Gor*_*les 5 java swing window jframe
我将以“这更像是一个答案”开头。
就像,我自己回答这个问题。
我用标签搜索,我用谷歌搜索,我用标签和纯文本的组合(在这个网站上)搜索,但我仍然找不到这个该死的问题。有些人粘贴来源或链接来源作为答案。
这是(其中一个)原始来源,它遍布网络。
http://bit.ly/1eYKNXS(code.google.com域,如有必要,我可以使用完整 URL 进行编辑)
这并不完美。0 的偏移量似乎并不总是有效(在我的家用机器上和此处测试,使用多显示器设置和不使用),而 5 的偏移量。它没有多显示器支持。捕捉行为有点令人兴奋(它一直在发射)。
答案就是这个问题的答案。随意锁定,存档等。只是想帮忙:)
所以,看吧,改进的代码。希望它对人们有所帮助,并且它不会太冗长或阅读起来很痛苦。
package widgets;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
public class WindowSnapper extends ComponentAdapter {
private boolean locked = false;
// feel free to modify; set based on my own preferences
// incorporate as user option?
private int sd = 30;
private GraphicsDevice[] screenList = GraphicsEnvironment
.getLocalGraphicsEnvironment().getScreenDevices();
// clamping at 5 seems correct, 0 clamps at -5 beyond screen boundary
public void componentMoved(ComponentEvent evt) {
// gets current display device
Window myWindow = new Window((Frame) evt.getComponent());
GraphicsConfiguration config = myWindow.getGraphicsConfiguration();
GraphicsDevice myScreen = config.getDevice();
// matches against active display
for(GraphicsDevice gd : getScreenList()) {
// this will set the display to a new display if the window is moved to a new display
if(gd.equals(myScreen)) {
myScreen = gd;
break;
}
}
// minimising calls to stack
int screenWidth = myScreen.getDefaultConfiguration().getBounds().width;
int screenHeight = myScreen.getDefaultConfiguration().getBounds().height;
int compWidth = evt.getComponent().getWidth();
int compHeight = evt.getComponent().getHeight();
int nx = evt.getComponent().getX();
int ny = evt.getComponent().getY();
// setting offsets in case of different screen
int currentX = myScreen.getDefaultConfiguration().getBounds().x;
int currentY = myScreen.getDefaultConfiguration().getBounds().y;
// see end of method
// OR conditions seem to stabilise movement when close to screen edge
if(locked
|| nx == currentX + 5
|| ny == currentY + 5
|| nx == currentX + screenWidth - compWidth - 5
|| ny == currentY + screenHeight - compHeight - 5)
return;
// left
if(nx < (currentX + sd) && nx > (currentX + 5)) {
nx = currentX + 5;
}
// top
if(ny < (currentY + sd) && ny > (currentY + 5)) {
ny = currentY + 5;
}
// right
if(nx > currentX + screenWidth - compWidth - sd
&& nx < currentX + screenWidth - compWidth - 5) {
nx = currentX + screenWidth - compWidth - 5;
}
// bottom
if(ny > currentY + screenHeight - compHeight - sd
&& ny < currentY + screenHeight - compHeight - 5) {
ny = currentY + screenHeight - compHeight - 5;
}
// make sure we don't get into a recursive loop when the
// set location generates more events
locked = true;
evt.getComponent().setLocation(nx, ny);
locked = false;
}
public int returnSD() {
return sd;
}
public void setSD(int sd) {
this.sd = sd;
}
public GraphicsDevice[] getScreenList() {
return screenList;
}
public void setScreenList(GraphicsDevice[] screenList) {
this.screenList = screenList;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1152 次 |
| 最近记录: |