我想寻求有关以下代码的帮助。
目标:
每当我将 JFrame 拖到屏幕顶部时,我应该能够将其放大到全屏。同样,如果 JFrame 处于全屏模式,每当我向下拖动它时,它应该返回到正常模式,具体取决于最小尺寸参数。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DraggableFrame extends JFrame {
private int posX = 0, posY = 0;
private boolean isDraggingDown = false;
public DraggableFrame() {
// Initialize the JFrame
setUndecorated(true);
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Add mouse listener for dragging
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
posX = e.getX();
posY = e.getY();
isDraggingDown = false;
}
});
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
int x = e.getXOnScreen() - posX;
int y = e.getYOnScreen() - posY;
// Handle dragging while in full screen
if (getExtendedState() == JFrame.MAXIMIZED_BOTH) {
if (y > 0 && !isDraggingDown) {
isDraggingDown = true;
setExtendedState(JFrame.NORMAL);
setSize(800, 600); // Restore to initial size
setLocation(x, y);
} else if (y <= 0 && !isDraggingDown) {
// If the frame is dragged to the top while maximized, do nothing
return;
}
} else {
setLocation(x, y);
// Check if dragged to the top
if (getY() <= 0) {
// Enlarge window to fullscreen without covering taskbar
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = env.getDefaultScreenDevice();
Rectangle screenBounds = device.getDefaultConfiguration().getBounds();
Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(device.getDefaultConfiguration());
int taskbarHeight = screenInsets.bottom;
setBounds(screenBounds.x, screenBounds.y, screenBounds.width, screenBounds.height - taskbarHeight);
}
}
}
});
// Restore to normal size if dragged down while in fullscreen
addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if (getY() == 0 && getExtendedState() == JFrame.MAXIMIZED_BOTH) {
setExtendedState(JFrame.NORMAL);
setSize(800, 600); // Restore to initial size
setLocationRelativeTo(null);
}
}
});
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
DraggableFrame frame = new DraggableFrame();
frame.setVisible(true);
});
}
}
我尝试应用一些在互联网上找到的代码,但似乎没有一个能按我的预期工作。
我想要的是以下内容:
每当我将 JFrame 拖到屏幕顶部时,我应该能够将其放大到全屏。同样,如果 JFrame 处于全屏模式,每当我向下拖动它时,它应该返回到正常模式,具体取决于最小尺寸参数。
问题是这样的情况:
// Handle dragging while in full screen
if (getExtendedState() == JFrame.MAXIMIZED_BOTH) {
您永远不会将帧扩展状态设置为
JFrame.MAXIMIZED_BOTH
,因此该条件永远不会成立,并且您永远不会到达将帧切换回正常状态的代码。
由于将框架设置为
JFrame.MAXIMIZED_BOTH
会遮挡任务栏(您似乎想避免这种情况),因此当框架最大化时,您需要另一个指示。
您可以通过向
maximized
添加布尔字段 DraggableFrame
并在必要时进行调整来实现此目的:
public class DraggableFrame extends JFrame {
private boolean maximized = false;
// ...
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
int x = e.getXOnScreen() - posX;
int y = e.getYOnScreen() - posY;
// Handle dragging while in full screen
if (maximized) {
if (y > 0 && !isDraggingDown) {
isDraggingDown = true;
setExtendedState(JFrame.NORMAL);
setSize(800, 600); // Restore to initial size
setLocation(x, y);
maximized = false;
} else if (y <= 0 && !isDraggingDown) {
// If the frame is dragged to the top while maximized, do nothing
return;
}
} else {
setLocation(x, y);
// Check if dragged to the top
if (getY() <= 0) {
// Enlarge window to fullscreen without covering taskbar
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = env.getDefaultScreenDevice();
Rectangle screenBounds = device.getDefaultConfiguration().getBounds();
Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(device.getDefaultConfiguration());
int taskbarHeight = screenInsets.bottom;
setBounds(screenBounds.x, screenBounds.y, screenBounds.width, screenBounds.height - taskbarHeight);
maximized = true;
}
}
}
});
// Restore to normal size if dragged down while in fullscreen
addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if (getY() == 0 && maximized) {
setExtendedState(JFrame.NORMAL);
setSize(800, 600); // Restore to initial size
setLocationRelativeTo(null);
maximized = false;
}
}
});
}
// ...
}
更好的是要么不使用未装饰的框架(因为它们与操作系统的窗口管理器一起工作),要么(如果您需要未装饰的框架,因为您想制作全屏游戏)使窗口从一开始就全屏.