本文介绍了高分辨率(MS Surface)上的错误Swing UI缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个涉及Swing GUI的小型Java应用程序。在我的开发PC上,一切看起来都不错,但是当我在MS Surface上运行它时,某些图标似乎对于组件而言太大(或者组件对于图标而言太小)。

I'm currently working on a little Java application, that involves Swing GUIs. On my development PC everything looks fine but when I run it on my MS Surface, some icons seem to be too large for the components (or the components too small for the icons).

这就是我的意思:

Google研究让我得出结论,这是由于Surface的高分辨率和Win8的缩放让一些项目显得更大。所以我将缩放重置为100%,它实际上修复了不良缩放。

Google research has lead me to conclude that this is due to Surface's high resolution and Win8's zooming to let some items appear a little larger. So I reset that zoom to 100% and it actually fixed the bad scaling.

不幸的是,这并没有真正解决我的问题。没有变焦,一切都太小了,所以我宁愿不禁用它。但有没有聪明的方法来解决这个问题?我可以只缩放我的程序或Java的图标吗?理想情况下,我甚至想要升级整个帧,因为一切都很小。

Unfortunately, this doesn't really fix my problem. Everything is far too small without the zoom, so I'd rather not disable it. But is there any clever way to solve this? Can I just "unscale" my program's or Java's icons? Ideally, I would even like upscale the entire frame, because everything is rather small.

编辑:很明显,我也试过调整实际的JFrame,但它没有对对话框大小的影响。我正在调用对话框

obviously, I've also tried just resizing the actual JFrame but it has no effect on the dialog size. I'm calling the dialog by

JOptionPane.showMessageDialog(frame, msg, "Information", JOptionPane.INFORMATION_MESSAGE);

推荐答案

这是一个讨厌的,黑客的,快速修复的解决方案,它可以通过将Swing自己的图标大小调整为80%来阻止令人讨厌的裁剪。

Here is a nasty, hacky, quick-fix solution which will stop the nasty cropping by resizing Swing's own icons to 80%.

In main ,添加:

String[] iconOpts = {"OptionPane.errorIcon", 
  "OptionPane.informationIcon", 
  "OptionPane.warningIcon", 
  "OptionPane.questionIcon"};
for (String key : iconOpts) {
  ImageIcon icon = (ImageIcon) UIManager.get(key);
  Image img = icon.getImage();
  BufferedImage bi = new BufferedImage(
          img.getWidth(null), img.getHeight(null), 
          BufferedImage.TYPE_INT_ARGB);
  java.awt.Graphics g = bi.createGraphics();
  g.drawImage(img, 0, 0, 
          (int) (img.getWidth(null) * 0.8), 
          (int) (img.getHeight(null) * 0.8), null);
  ImageIcon newIcon = new ImageIcon(bi);
  UIManager.put(key, newIcon);
}

您可能需要先检查是否确实需要这样做 - Windows 8/10默认为125%,但有些人会将其切换回100%。我没有找到一种优雅的方法来做到这一点,但是这些方面的内容会给你一个想法:

You might want to first check whether this is actually required - Windows 8/10 defaults to 125% but some people will switch it back to 100%. I haven't found an elegant way to do this, but something along these lines will give you an idea:

java.awt.Font font = (java.awt.Font) UIManager.get("Label.font");
if (font.getSize() != 11) {
    //resize icons in here
}

这篇关于高分辨率(MS Surface)上的错误Swing UI缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 14:22