簡體   English   中英

如何在中心屏幕中定位表單?

[英]How to position the form in the center screen?

我是一名.Net開發人員,但不知何故,我的任務是在java中創建一個簡單的應用程序,原因還有一些。 我能夠創建該應用程序但我的問題是如何在應用程序啟動時將窗體置於屏幕中心?

這是我的代碼:

private void formWindowActivated(java.awt.event.WindowEvent evt) 
{
        // Get the size of the screen
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        // Determine the new location of the window
        int w = this.getSize().width;
        int h = this.getSize().height;
        int x = (dim.width-w)/2;
        int y = (dim.height-h)/2;

        // Move the window
        this.setLocation(x, y);
}

上面的代碼工作正常,但問題是我已經看到表單從最頂層移動到中心屏幕。 我還嘗試在formWindowOpened事件中添加該代碼,並仍然顯示相同的操作。 有更好的方法嗎? 就像在.NET Application一樣,有一個CenterScreen Position 或者,如果上面的代碼是正確的,我將把它放在什么事件上?

感謝您閱讀本文。

在JFrame上調用pack后,只需設置相對於null的位置,就是這樣。

例如,

  JFrame frame = new JFrame("FooRendererTest");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.getContentPane().add(mainPanel); // or whatever...
  frame.pack();
  frame.setLocationRelativeTo(null);  // *** this will center your app ***
  frame.setVisible(true);

以下示例在屏幕上居中顯示框架:

package com.zetcode;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import javax.swing.JFrame;


public class CenterOnScreen extends JFrame {

    public CenterOnScreen() {

        initUI();
    }

    private void initUI() {

        setSize(250, 200);
        centerFrame();
        setTitle("Center");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void centerFrame() {

            Dimension windowSize = getSize();
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            Point centerPoint = ge.getCenterPoint();

            int dx = centerPoint.x - windowSize.width / 2;
            int dy = centerPoint.y - windowSize.height / 2;    
            setLocation(dx, dy);
    }


    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                CenterOnScreen ex = new CenterOnScreen();
                ex.setVisible(true);
            }
        });       
    }
}

為了在屏幕上居中框架,我們需要獲得本地圖形環境。 從這個環境,我們確定中心點。 結合框架尺寸,我們設法使框架居中。 setLocation()是將幀移動到中心位置的方法。

請注意,這實際上是setLocationRelativeTo(null)作用:

public void setLocationRelativeTo(Component c) {
    // target location
    int dx = 0, dy = 0;
    // target GC
    GraphicsConfiguration gc = getGraphicsConfiguration_NoClientCode();
    Rectangle gcBounds = gc.getBounds();

    Dimension windowSize = getSize();

    // search a top-level of c
    Window componentWindow = SunToolkit.getContainingWindow(c);
    if ((c == null) || (componentWindow == null)) {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
        gcBounds = gc.getBounds();
        Point centerPoint = ge.getCenterPoint();
        dx = centerPoint.x - windowSize.width / 2;
        dy = centerPoint.y - windowSize.height / 2;
    }

  ...

  setLocation(dx, dy);
}

改變這個:

public FrameForm() { 
    initComponents(); 
}

對此:

public FrameForm() {
    initComponents();
    this.setLocationRelativeTo(null);
}
public class Example extends JFrame {

public static final int WIDTH = 550;//Any Size
public static final int HEIGHT = 335;//Any Size

public Example(){

      init();

}

private void init() {

    try {
        UIManager
                .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

        SwingUtilities.updateComponentTreeUI(this);
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        setSize(WIDTH, HEIGHT);

        setLocation((int) (dimension.getWidth() / 2 - WIDTH / 2),
                (int) (dimension.getHeight() / 2 - HEIGHT / 2));


    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (UnsupportedLookAndFeelException e) {
        e.printStackTrace();
    }

}
}

我希望這會有所幫助。

把它放在源代碼的頂部:

import java.awt.Toolkit;

然后編寫此代碼:

private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
    int lebar = this.getWidth()/2;
    int tinggi = this.getHeight()/2;
    int x = (Toolkit.getDefaultToolkit().getScreenSize().width/2)-lebar;
    int y = (Toolkit.getDefaultToolkit().getScreenSize().height/2)-tinggi;
    this.setLocation(x, y);
}

祝好運 :)

實際上,你真的不需要編寫代碼來使表單進入中心屏幕。

只需修改jframe的屬性即可
按照以下步驟進行修改:

  • 右鍵單擊表單
  • FormSize策略更改為 - 生成調整大小代碼
  • 然后編輯表格位置X -200 Y-200

你完成了。 為什么要承擔編碼的痛苦。 :)

如果您使用NetBeans IDE右鍵單擊表單

屬性 - >代碼 - >簽出生成中心

試試這個:

this.setBounds(x,y,w,h);

使用此功能,您可以定義您的贏家位置

setBounds(500, 200, 647, 418);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM