如何防止在Java中调整面板的大小

问题描述

我已经应用了surface.setResizable (false)方法; 如何在面板上做同样的事情?我一直这样:

stage.setMinWidth (w);
stage.setMinHeight (h);
stage.setWidth (w);
stage.setHeight (h);
stage.setMaxWidth (w);
stage.setMaxHeight (h);

pane.setMaxSize (700,420);
pane.setPrefSize (700,420);
pane.setMinSize (700,420);

这里还有另一个小问题!如何使标题居中?

surface.setTitle ("HELLO WORLD");

enter image description here

enter image description here

解决方法

在处理中,GLWindow是P3D渲染器可调整大小的可能存在问题。从提供的少量信息看来,根本不需要使用P3D。使用默认(JAVA2D)渲染器,并且窗口将无法调整大小:这是默认设置。

关于这个小问题,我也有一个小问题:为什么? :)

在可用性方面,理想情况下,您希望您的应用保持一致。

如果真的很想居中,将文本居中的一种方法是在“ HELLO WORLD”之前添加一些空格。您将需要根据系统字体(在每个OS上和用户首选项可能不同)来移动字符串多少像素,并且如果它是等宽字体(多数情况下不是),会更容易。有很多事情要考虑,如果您想使它达到完美像素,那么很多事情都会出错,而所有这些努力会带来什么收获呢?

以下是测试草图:

void setup(){
  size(300,300,JAVA2D);
  background(0,192,0);
  // don't need this with JAVA 2D: it's the default
  //surface.setResizable(false);
  
  String title = "Hello World";
  // estimated char width: assumes font is monospaced (Arial/Verdana it isn't)
  int charWidth = 6;
  // calculate left margin
  int leftMargin = (width - (title.length() * charWidth)) / 2;
  // calculate the number of chars to prepent
  int chars = leftMargin / charWidth;
  // prepend spaces to move text to the right
  for(int i = 0 ; i < chars; i++){
    title = " " + title;
  }
  
  surface.setTitle(title); 
}

您甚至可以为其设置动画:

void draw(){
  String title = "->";
  for(int i = 0 ; i < (int)map(sin(frameCount * 0.1),-1,1,21); i++){
    title = "-" + title;
  }
  surface.setTitle(title);
}

真的值得付出努力并浪费CPU周期吗?可能不是:)

,

您会尝试这些属性吗?

    frame.setResizable(false);
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

确保在调用包之前始终调用setResizable

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...