How to position the form in the center screen?

后端 未结 9 1859
小鲜肉
小鲜肉 2021-01-30 05:05

I\'m a .Net developer but somehow I was task to create a simple application in java for some extra reason. I was able to create that application but my problem is how can i cent

相关标签:
9条回答
  • 2021-01-30 05:30
    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();
        }
    
    }
    }
    
    0 讨论(0)
  • 2021-01-30 05:32

    Simply set location relative to null after calling pack on the JFrame, that's it.

    e.g.,

      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);
    
    0 讨论(0)
  • 2021-01-30 05:32

    Change this:

    public FrameForm() { 
        initComponents(); 
    }
    

    to this:

    public FrameForm() {
        initComponents();
        this.setLocationRelativeTo(null);
    }
    
    0 讨论(0)
  • 2021-01-30 05:32

    Using this Function u can define your won position

    setBounds(500, 200, 647, 418);
    
    0 讨论(0)
  • 2021-01-30 05:33

    Actually, you dont really have to code to make the form get to centerscreen.

    Just modify the properties of the jframe
    Follow below steps to modify:

    • right click on the form
    • change FormSize policy to - generate resize code
    • then edit the form position X -200 Y- 200

    You're done. Why take the pain of coding. :)

    0 讨论(0)
  • 2021-01-30 05:35

    The following example centers a frame on the screen:

    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);
                }
            });       
        }
    }
    

    In order to center a frame on a screen, we need to get the local graphics environment. From this environment, we determine the center point. In conjunction with the frame size, we manage to center the frame. The setLocation() is the method that moves the frame to the central position.

    Note that this is actually what the setLocationRelativeTo(null) does:

    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);
    }
    
    0 讨论(0)
提交回复
热议问题