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

huangapple go评论64阅读模式
英文:

How to prevent resizing of a panel in Java

问题

I have already applied the surface.setResizable(false) method;
我已经应用了surface.setResizable(false)方法;

How to do the same with the panel? I've been like this:
如何在面板上做同样的事情?我一直这样做:

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);

And another little question here! How to make the title centered?
还有一个小问题!如何使标题居中?

surface.setTitle("HELLO WORLD");

英文:

I have already applied the surface.setResizable (false) method;
How to do the same with the panel? I've been like this:

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);

And another little question here! How to make the title centered?

surface.setTitle ("HELLO WORLD");

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

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

答案1

得分: 1

这里有一个关于在Processing中将GLWindow设置为P3D渲染器并使其可调整大小的问题。根据提供的信息,似乎根本不需要使用P3D。使用默认的(JAVA2D)渲染器,窗口将不可调整大小:这是默认设置。

对于我提出的小问题,我也有一个小问题:为什么呢? 如何防止在Java中调整面板大小

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

如果您真的非常想要居中文本的一种方法是在“HELLO WORLD”之前添加一些空格。您需要根据系统字体(在每个操作系统和用户首选项可能不同的情况下)来确定要将字符串移动多少像素,如果它是等宽字体(在大多数情况下它不是)。需要考虑的事情很多,如果您想完美地掌握像素,很多事情都可能出错,那么所有这些努力又有什么好处呢?

这是一个测试草图:

void setup(){
  size(300,300, JAVA2D);
  background(0,192,0);
  // 使用JAVA 2D时不需要这个:它是默认的
  //surface.setResizable(false);
  
  String title = "Hello World";
  // 估计字符宽度:假设字体是等宽的(Arial/Verdana它不是)
  int charWidth = 6;
  // 计算左边距
  int leftMargin = (width - (title.length() * charWidth)) / 2;
  // 计算要添加的字符数
  int chars = leftMargin / charWidth;
  // 在文本前面添加空格以向右移动文本
  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, 0, 21); i++){
    title = "-" + title;
  }
  surface.setTitle(title);
}

真的值得花费这个精力和浪费CPU周期吗?可能不值得 如何防止在Java中调整面板大小

英文:

There might be an issue making the GLWindow the P3D renderer in Processing resizable. From the little information provided it appears there's no need to use P3D at all. Use the default (JAVA2D) renderer and window will not be resizable: it's the default.

Regarding the little question I have little question too: why ? 如何防止在Java中调整面板大小

In terms of usability ideally you want your app to be consistent.

One way to centre the text, if you really really really want to is to prepend some spaces before "HELLO WORLD". You will need to workout how many pixels to move the string based on the system font (which on each OS and user preferences may differ) and it would be easier if it's a monospaced font (which in most cases it isn't). A lot of things to consider, a lot that can go wrong if you want to nail it pixel perfect and what would be that gain from all that effort ?

Here's a test sketch regardless:

void setup(){
  size(300,300, JAVA2D);
  background(0,192,0);
  // don&#39;t need this with JAVA 2D: it&#39;s the default
  //surface.setResizable(false);
  
  String title = &quot;Hello World&quot;;
  // estimated char width: assumes font is monospaced (Arial/Verdana it isn&#39;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 &lt; chars; i++){
    title = &quot; &quot; + title;
  }
  
  surface.setTitle(title); 
}

You can even animate it you want:

void draw(){
  String title = &quot;-&gt;&quot;;
  for(int i = 0 ; i &lt; (int)map(sin(frameCount * 0.1), -1, 1, 0, 21); i++){
    title = &quot;-&quot; + title;
  }
  surface.setTitle(title);
}

Is it really worth the effort and wasting CPU cycles ? Probably not 如何防止在Java中调整面板大小

答案2

得分: 0

尝试使用一些属性吗?

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

确保在调用pack之前始终调用setResizable。

英文:

Would you try this some of properties?

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

Make sure you always call setResizable BEFORE calling pack

huangapple
  • 本文由 发表于 2020年8月13日 14:38:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63389453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定