Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JavaSwing中自定义和显示屏幕中心的框架窗口?_Java_Swing_Jframe - Fatal编程技术网

如何在JavaSwing中自定义和显示屏幕中心的框架窗口?

如何在JavaSwing中自定义和显示屏幕中心的框架窗口?,java,swing,jframe,Java,Swing,Jframe,您好,我正在使用swing开发一个桌面应用程序,当我执行框架窗口时,它显示在屏幕的左侧底部。我可以自定义它并显示在屏幕的中心吗 “是的,我正在使用gui builder” 只需将setLocationRelativeTo(null)放在构造函数的initComponents()之后 public class MyFrame extends JFrame { public MyFrame() { initComponents(); setLocationRe

您好,我正在使用swing开发一个桌面应用程序,当我执行框架窗口时,它显示在屏幕的左侧底部。我可以自定义它并显示在屏幕的中心吗

“是的,我正在使用gui builder”

只需将
setLocationRelativeTo(null)
放在构造函数的
initComponents()之后

public class MyFrame extends JFrame {
    public MyFrame() {
        initComponents();
        setLocationRelativeTo(null);
    }
}

JFrame
作为
Window
的子类继承了
Window
方法。可以看到API。上面使用的方法如下所示

  • -根据以下场景设置窗口相对于指定组件的位置。
    如果组件为空,或与此组件关联的图形配置为空,则窗口将位于屏幕中央。

    • 以下代码将为您提供屏幕的宽度和高度

      Toolkit=Toolkit.getDefaultToolkit()

      toolkit.getScreenSize().width

      toolkit.getScreenSize().height

      然后使用此高度和宽度,您可以使用以下功能设置框架的位置

      this.setLocation(intx,inty)

      //第一个变量x是水平方向上相对于ur屏幕左上角的位置


      //第二个变量y是垂直方向上相对于左上角的位置。setLocationRelativeTo(null)
      ?实际上,我无法编辑生成的代码以插入此代码。您使用GUI Builder吗?是的,我使用GUI Builder设置所有属性,仅用于帧定位,您不能通过
      setLocationByPlatform(true)
      访问。有关演示,请参阅@AndrewThompson您的评论措辞是否正确?我对它和它的含意有点迷惑:[好吧,它假定:“不,不要把它放在屏幕的中间,看起来像“溅屏””,而是……“被理解了。”安德鲁汤普森“你不能通过平台(Struts)通过StestOffice来……”你的意思是“你可以”?哦,对不起,“不能通过”的意思是“不能放弃”或“不能没有”P