如何让一个JFrame启动时在桌面的中心显示出来?

lion_boy 2003-09-12 07:57:31
如何让一个JFrame启动时在桌面的中心显示出来?
...全文
157 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
pqds 2003-09-13
  • 打赏
  • 举报
回复
window = new SketchFrame("画图", this); // Create the app window
Toolkit theKit = window.getToolkit(); // Get the window toolkit
Dimension wndSize = theKit.getScreenSize(); // Get screen size
window.getContentPane().setBackground(Color.black);
// Set the position to screen center & size to 2/3 screen size
window.setBounds(wndSize.width/6, wndSize.height/6, // Position
2*wndSize.width/3, 2*wndSize.height/3); // Size
ice119103 2003-09-13
  • 打赏
  • 举报
回复
frame.setLocation((screenSize.width-testSize.width)/2,(screenSize.height-testSize.height)/2);
同意楼上的方法
qiyongjun2003 2003-09-13
  • 打赏
  • 举报
回复
Dimension frameSize = frame.getSize();
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screenSize=kit.getScreenSize();
int width=screenSize.width;
int height=screenSize.height;
setLocation((width-frameSize.width)/2,(height-frameSize.height)/2);
瓦力1981 2003-09-13
  • 打赏
  • 举报
回复
忘了说了,SIMSFrame 为自定义的一个Frame类
瓦力1981 2003-09-13
  • 打赏
  • 举报
回复
其实很简单,JBuilder会自动生成:
代码如下:
public BSSIMSApp() {
SIMSFrame frame = new SIMSFrame();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
applezh 2003-09-12
  • 打赏
  • 举报
回复
学习!!!
sam79 2003-09-12
  • 打赏
  • 举报
回复
你用JBuilder开发一个应用程序,它自动帮你设置好了,很省事的。
yixin22 2003-09-12
  • 打赏
  • 举报
回复
你可以这么做,假设您的JFrame 的类名称为Frame1

Frame1 test=new Frame1;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension testSize = frame.getSize();
frame.setLocation((screenSize.width-testSize.width)/2,(screenSize.height-testSize.height)/2);
显示的窗口就是在左面的中心
rekcah_ph 2003-09-12
  • 打赏
  • 举报
回复
ToolKit.getDefaultToolkit()。getScreenSize()
然后根据你的JFrame的大小计算,应该不是很复杂的计算吧?
noratong 2003-09-12
  • 打赏
  • 举报
回复
JFrame和Frame都有一个setLocation()方法,里面带两个参数,就是你想要这个窗体在屏幕中显示时的左上角的X和Y点的坐标值。如果想在屏幕中央显示就把值设为200和300左右吧,准确的话再换。

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧