我正在开发 Swing 项目,在 CentOS 上运行。我在调用 setLocation
时遇到问题方法JFrame
。我的屏幕尺寸是1920 * 1080和JFrame
窗口大小为 900 * 300。我想将 JFrame 窗口的部分从右侧移出屏幕,因此我将 1820 和 0 作为参数传递给 setLocation
方法,但是运行结果是JFrame
窗口没有超出屏幕,而不是JFrame
窗口右边框与屏幕右边框对齐。
有谁知道出了什么问题吗?
最佳答案
您当前的方法会隐藏窗口的拖动栏,这可能会阻止移动窗口。平台的窗口管理器可能会禁止这样做。相反,让平台进行调整,然后相对于 GraphicsDevice
边界移动窗口。
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
* @see http://stackoverflow.com/a/34651163/230513
* @see http://stackoverflow.com/a/9755371/230513
*/
public class UpperRightFrame {
private static final int VISIBLE = 100;
private void display() {
JFrame f = new JFrame("UpperRightFrame");
f.add(new Box(BoxLayout.Y_AXIS) {
{
add(new JLabel(System.getProperty("os.name")));
add(new JLabel(System.getProperty("os.version")));
}
@Override // placeholder for actual content
public Dimension getPreferredSize() {
return new Dimension(320, 240);
}
});
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();
int x = (int) rect.getMaxX() - f.getWidth();
int y = 0;
f.setLocation(x, y);
f.setVisible(true);
x = (int) rect.getMaxX() - VISIBLE;
y = f.getLocation().y;
f.setLocation(x, y);
}
public static void main(String[] args) {
EventQueue.invokeLater(new UpperRightFrame()::display);
}
}
关于java - Swing 设置框架在 CentOS 中的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34650545/