这行代码有什么明显的错误吗?我希望无论窗口大小如何,矩形都保持居中。但由于某种原因这不起作用,矩形保持在同一个位置。
public void run() {
setSize(800, 800);
createEntireFigure();
}
private void createEntireFigure(){
int centerOfWindowWidth = getWidth() / 2;
int centerOfWindowHeight = getHeight() / 2;
GRectWithGLabel ("A String",centerOfWindowWidth, centerOfWindowHeight);
}
最佳答案
您的矩形大小代码仅在创建矩形时调用,因此如果重新调整 GUI 的大小,矩形的位置不会改变,这是有道理的。您需要以某种方式监听 GUI 中的大小变化,并调用代码来重新定位矩形,然后才能使其正常工作。您使用什么图形库?
关于JAVA:框架大小和图形对象居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12236165/