简体   繁体   English

Java Swing:居中组件

[英]Java Swing: Centering components

I'm working on a Swing UI in which I want to center multiple components (JDialogs and JFrames). 我正在使用一个Swing UI,希望在其中将多个组件(JDialogs和JFrames)居中。 I know that the following code will calculate the user's screen size, and from there, I can easily center a component: 我知道以下代码将计算用户的屏幕尺寸,从那里,我可以轻松地将组件居中:

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

For efficiency's sake, I should only calculate this once and store it in some kind of constant so it can be reused in any part of the project. 为了提高效率,我应该只计算一次并将其存储在某种常量中,以便可以在项目的任何部分中重复使用。 What is the best practice for storing this for later reuse so it is accessible across multiple classes? 将其存储以供以后重用以便跨多个类访问的最佳实践是什么?

(Furthermore, if there is a better way of calculating screen size for centering, I'd be open to hearing that as well) (此外,如果有一种更好的方法来计算屏幕大小以进行居中,我也很乐意听到)

java.awt.Window.setLocationRelativeTo(null) will center it on the screen while setLocationRelativeTo(someComponent) will center it relative to the java.awt.Component, someComponent . java.awt.Window.setLocationRelativeTo(null)将其在屏幕上居中,而setLocationRelativeTo(someComponent)将其相对于java.awt.Component, someComponent居中。

One thing to consider with the alternate of storing the center, is that if a user adjusts their resolution while the program is running than the stored constants will no longer be valid. 备用存储中心时要考虑的一件事是,如果用户在程序运行时调整其分辨率,则存储的常量将不再有效。 Is recalling the getScreenSize function actually expensive? 调用getScreenSize函数实际上是昂贵的吗? (I do not know whether or not it is) (不知道是不是)

This puts the upper left corner of the component on center, but not the whole component 这样会将组件的左上角放在中心,而不是整个组件

This means the size of the dialog/frame is (0, 0), Your basic code should be: 这意味着对话框/框架的大小为(0,0),您的基本代码应为:

frame.add( .... );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );

For centering the object you should try: 为了使对象居中,您应该尝试:

The frame position in X = (half frame width) - (half screen size width) Almost the same for Y = (half frame height) - (half screen size height) X中的帧位置=(半帧宽度)-(半屏尺寸宽度)Y的几乎相同=(半帧高度)-(半屏尺寸高度)

You can easily stores the values in the main class with public access, so you don't need to read them several times 您可以通过公共访问轻松地将值存储在主类中,因此您无需多次读取它们

Also, if you do it yourself, you need to factor in the screen's insets using Toolkit.getScreenInsets to account for things like the task bar, which might be on any screen edge and be of any size. 另外,如果您自己执行此操作,则需要使用Toolkit.getScreenInsets来考虑屏幕的插图,以考虑任务栏之类的事情,该任务栏可能在任何屏幕边缘且大小不限。

Before I could target Java 1.4, I used: 在定位Java 1.4之前,我使用了:

static public void centerWindow(Window wnd, Component relcom) {
    Rectangle                           scrbnd=getScreenBounds(wnd);
    Dimension                           wndsiz=wnd.getSize();
    Container                           root=null;
    int                                 px,py;

    if(relcom!=null) {
        if(relcom instanceof Window || relcom instanceof java.applet.Applet) {
            root=(Container)relcom;
            }
        else {
            Container parent;
            for(parent=relcom.getParent(); parent!=null; parent=parent.getParent()) {
                if(parent instanceof Window || parent instanceof java.applet.Applet) {
                    root=parent;
                    break;
                    }
                }
            }
        }

    if(relcom==null || !relcom.isShowing() || root==null || !root.isShowing()) {
        px=(scrbnd.x+((scrbnd.width -wndsiz.width )/2));
        py=(scrbnd.y+((scrbnd.height-wndsiz.height)/2));
        }
    else {
        Point       relloc=relcom.getLocationOnScreen();
        Dimension   relsiz=relcom.getSize();

        px=(relloc.x+((relsiz.width -wndsiz.width )/2));
        py=(relloc.y+((relsiz.height-wndsiz.height)/2));
        }

    if((px+wndsiz.width )>(scrbnd.x+scrbnd.width )) { px=((scrbnd.x+scrbnd.width )-wndsiz.width ); }
    if((py+wndsiz.height)>(scrbnd.y+scrbnd.height)) { py=((scrbnd.y+scrbnd.height)-wndsiz.height); }
    if(px<scrbnd.x) { px=scrbnd.x; }
    if(py<scrbnd.y) { py=scrbnd.y; }
    wnd.setLocation(px,py);
    }

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

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