简体   繁体   English

JAVA:框架大小和居中图形对象

[英]JAVA: size of frame and centering graphical objects

Is there anything obvious wrong with this line of code? 这行代码有什么明显的错误吗? I want rectangle to stay centered regardless the size of the window. 无论窗口的大小如何,我都希望矩形保持居中。 But this don´t work for some reason, the rectangle stays the same place. 但这由于某些原因无法正常工作,矩形会保留在同一位置。

    public void run() {
    setSize(800, 800);
    createEntireFigure();


}
private void createEntireFigure(){
    int centerOfWindowWidth = getWidth() / 2;
    int centerOfWindowHeight = getHeight() / 2;

    GRectWithGLabel ("A String",centerOfWindowWidth, centerOfWindowHeight); 
}

Your rectangle size code is only called on rectangle creation, and so it makes sense that the rectangle's position will not change if the GUI is re-sized. 您的矩形大小代码仅在创建矩形时调用,因此,如果重新调整GUI的大小,则矩形的位置不会改变。 You need to somehow listen for size changes in your GUI and call code to re-position the rectangle then for this to work. 您需要以某种方式在GUI中侦听大小变化,并调用代码以重新放置矩形,然后才能正常工作。 What graphics library are you using? 您正在使用哪个图形库?

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

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