简体   繁体   English

如何在JFrame中居中放置对象

[英]How to center an Object in the JFrame

I'm not sure how i should center this. 我不知道该如何确定这一点。 I tried to use locationRelativeTo null but the gameBoard stays in the top left corner. 我尝试使用locationRelativeTo为null,但gameBoard停留在左上角。 But i need to center it. 但是我需要居中。

 public static void main(String[] args) {
        JFrame game = new JFrame();
        game.setTitle("Game2048");
        game.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        game.setSize(420, 700);
        game.setResizable(false);
        game.add(new Game2048());
        // help here >>>>  game.setLocation();    
        game.setVisible(true);
    }

See the javadoc of method setLocationRelativeTo(Component) : 请参见方法setLocationRelativeTo(Component)的javadoc:

If the component is null , or the GraphicsConfiguration associated with this component is null , the window is placed in the center of the screen. 如果该组件为null ,或者与此组件关联的GraphicsConfigurationnull ,则该窗口将放置在屏幕的中央。 The center point can be obtained with the GraphicsEnvironment.getCenterPoint method. 中心点可以通过GraphicsEnvironment.getCenterPoint方法获得。

That means, you can see simply use 这意味着,您可以看到简单的使用

game.setLocationRelativeTo(null);

and your JFrame game will be placed in the center of the screen. 并且您的JFrame game将放置在屏幕中央。

You can center two things here. 您可以在此处集中两件事。 First of all you can center the complete program window. 首先,您可以将整个程序窗口居中。 This should be possible with 这应该是可能的

game.setLocationRelativeTo(null);

The second thing to center is the component in the JFrame, in your case the Game2048 component. 要居中的第二件事是JFrame中的组件,在您的情况下是Game2048组件。 Therefore I can recommend the tutorial A Visual Guide to Layout Managers 因此,我可以推荐教程“布局管理器视觉指南”

When it comes to dimensions it isn't always simple to understand how they're used in Swing. 关于尺寸,要了解它们在Swing中的用法并不总是那么简单。

I would recommend you set your dimensions in the component Game2048 and let the JFrame apply the dimensions. 我建议您在组件Game2048中设置尺寸,并让JFrame应用尺寸。

If you use a JPanel you could also use the setAlignment methods 如果使用JPanel,则也可以使用setAlignment方法

panel.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.setAlignmentY(Component.CENTER_ALIGNMENT);

Only the object that i added. 只有我添加的对象。

Then the easiest way is to use a GridBagLayout for the content pane of the frame (instead of the default BorderLayout). 然后,最简单的方法是将GridBagLayout用于框架的内容窗格(而不是默认的BorderLayout)。

//game.add(new Game2048());
game.setLayout( new GridBagLayout() );
game.add(new Game2048, new GridBagConstraints());

Using the default constraints will cause the component to be centered in the space available. 使用默认约束将使组件在可用空间中居中。

Read the section from the Swing tutorial on How to use GridBagLayout for more information, especially the section on using the weightx/weighty constraints to understand why this works. 阅读Swing教程中有关如何使用GridBagLayout的部分以获取更多信息,尤其是有关使用weightx / weighty约束的部分以了解其工作原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM