在J2SE中,我们可以通过计算屏幕的大小和窗口的大小,然后调整窗口的位置达到让窗口居中的效果。
实例1:<wbr style="line-height:25px"><div style="line-height:25px">
<div style="line-height:25px; color:rgb(51,102,255)"> JFrame fileDeleter=new FileDeleter();</div>
<div style="line-height:25px">
<span style="color:#3366FF; line-height:25px"> </span><span style="color:#993300; line-height:25px">Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();</span>
</div>
<div style="line-height:25px; color:rgb(51,102,255)"> Dimension frameSize =fileDeleter.getSize();</div>
<div style="line-height:25px; color:rgb(51,102,255)"> if (frameSize.height > screenSize.height) {</div>
<div style="line-height:25px; color:rgb(51,102,255)"> frameSize.height = screenSize.height;</div>
<div style="line-height:25px; color:rgb(51,102,255)"> }</div>
<div style="line-height:25px; color:rgb(51,102,255)"> if (frameSize.width > screenSize.width) {</div>
<div style="line-height:25px; color:rgb(51,102,255)"> frameSize.width = screenSize.width;</div>
<div style="line-height:25px; color:rgb(51,102,255)"> }</div>
<div style="line-height:25px">
<span style="line-height:25px; color:rgb(51,102,255)"> </span><span style="color:#993300; line-height:25px">fileDeleter.setLocation((screenSize.width - frameSize.width) / 2,</span>
</div>
<div style="line-height:25px"><span style="color:#993300; line-height:25px"> (screenSize.height - frameSize.height) / 2);</span></div>
<div style="line-height:25px; color:rgb(51,102,255)"> fileDeleter.setVisible(true);</div>
</div>
</wbr>
Java窗口居中
最新推荐文章于 2025-11-26 15:59:01 发布
1822

被折叠的 条评论
为什么被折叠?



