我已经编写了一个Swing应用程序.我想将JFrame设置为在用户屏幕的中心.我可以使用以下方法将其设置为屏幕的大致中心:
JFrame frame = new JFrame("Test Frame");
Rectangle r = new Rectangle(600, 300, 400, 400);
frame.setBounds(r);
有没有一种方法可以定义Rectangle r,使其中心位于任何屏幕的中心?
setBounds()是错误地动态更改帧位置的方法吗?
解决方法:
您是否想在屏幕上居中放置JFrame?如果是这样,那么只需在调用pack()之后在JFrame上调用setLocationRelativeTo(null).如果可以避免的话,您确实不想设置JFrame的大小,而是让布局管理器为您做大小设置. pack()方法将要求容器的布局管理器布置其组件并为其组件设置最佳尺寸.
编辑
关于您的问题:
Thanks, that’s great. Any chance you could tell me where I could find the method details within docs.oracle
由于可以在JFrame上调用此方法,因此只需转到JFrame API.如果这是父类的方法,则API会告诉您并提供到父类及其方法的链接.
标签:swing,position,java
来源: https://codeday.me/bug/20191101/1980339.html