gpt4 book ai didi

java - 如何在启动时将 JFrame 定位到屏幕的右上角

转载 作者:行者123 更新时间:2023-11-29 07:04:42 26 4
gpt4 key购买 nike

如何将JFrame放在TOP_RIGHT上?

我知道居中,正常的在左上角,但如何放在右上角?

最佳答案

this answer: positioning to the bottom-right 为例, 我做了调整让它出现在右上角

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TopRightFrame {

private void display() {
JFrame f = new JFrame("Top-Right Frame");
f.add(new JPanel() {

@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);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
new TopRightFrame().display();
}
});
}
}

关于java - 如何在启动时将 JFrame 定位到屏幕的右上角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21169487/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com