Java 21 Swing Nimbus在Win/Linux高分辨率屏GUI缩放异常求助
Swing Nimbus高DPI缩放问题修复求助(Java 21)
关于Java在Windows/Linux系统的缩放问题已有诸多旧提问但无明确答案,特向Swing专家咨询Nimbus相关的最新解决方案。我在1920×1080分辨率设备上开发了一款基于Swing Nimbus的Java 21应用,在高分辨率屏幕运行时GUI显示极小。查阅JEP 263得知问题已解决,但不知如何修改代码;尝试了相关JVM参数后,按建议制作的小测试示例可正常缩放,最终定位到背景图片绘制代码存在问题,现求助如何修复该代码。
Nimbus配置代码
NimbusLookAndFeel nimbus = new NimbusLookAndFeel(); UIManager.setLookAndFeel(nimbus); UIManager.put("control", Color.WHITE); UIManager.put("nimbusBlueGrey", ApplicationColors.getLightGrayGold()); UIManager.put("nimbusBase", ApplicationColors.getDarkGold()); UIManager.put("textForeground", Color.BLACK); UIManager.put("nimbusFocus", ApplicationColors.getSunflowerYellow()); UIManager.put("ToolBar:Button.contentMargins", new Insets(5, 15, 5, 15)); UIManager.put("TextField.background", ApplicationColors.getLightYellow()); UIManager.put("ComboBox.forceOpaque", false); UIManager.put("TitledBorder.border", new Insets(10, 10, 10, 10)); UIManager.put("TitledBorder.position", TitledBorder.ABOVE_BOTTOM); UIManager.put("TitledBorder.font", ApplicationFonts.getGermanFont(16F)); UIManager.put("TitledBorder.titleColor", Color.GRAY); UIManager.put("Table.opaque", false); UIManager.put("List.opaque", false); UIManager.put("Table.cellRenderer", false); UIManager.put("OptionPane.buttonFont", ApplicationFonts.getGermanFont(16F)); UIManager.put("OptionPane.cancelButtonText", translator.realisticTranslate(Translation.ABBRECHEN)); UIManager.put("OptionPane.yesButtonText", translator.realisticTranslate(Translation.JA)); UIManager.put("OptionPane.noButtonText", translator.realisticTranslate(Translation.NEIN)); UIManager.put("OptionPane.titleText", translator.realisticTranslate(Translation.BILD_LOESCHEN)); UIManager.put("FileChooser.openButtonText", translator.realisticTranslate(Translation.OEFFNEN)); UIManager.put("FileChooser.cancelButtonText", translator.realisticTranslate(Translation.ABBRECHEN)); UIManager.put("FileChooser.saveButtonText", translator.realisticTranslate(Translation.SPEICHERN)); UIManager.put("FileChooser.cancelButtonToolTipText", translator.realisticTranslate(Translation.ABBRECHEN_DER_AUSWAHL)); UIManager.put("FileChooser.saveButtonToolTipText", translator.realisticTranslate(Translation.AUSGEWAEHLTE_DATEI_SPEICHERN)); UIManager.put("FileChooser.openButtonToolTipText", "Ausgewählte Datei öffnen"); UIManager.put("FileChooser.upFolderToolTipText", "Eine Ebene höher"); UIManager.put("FileChooser.homeFolderToolTipText", "Home"); UIManager.put("FileChooser.newFolderToolTipText", "Neuen Ordner erstellen"); UIManager.put("FileChooser.listViewButtonToolTipText", "Liste"); UIManager.put("FileChooser.detailsViewButtonToolTipText", "Details"); UIManager.put("FileChooser.lookInLabelText", "Suchen in:"); UIManager.put("FileChooser.fileNameLabelText", "Dateiname:"); UIManager.put("FileChooser.filesOfTypeLabelText", "Dateityp:"); UIManager.put("FileChooser.acceptAllFileFilterText", "Alle Dateien (*.*)"); UIManager.put("FileChooser.folderNameLabelText", "Ordnername:"); UIManager.put("FileChooser.openDialogTitleText", translator.realisticTranslate(Translation.OEFFNEN)); UIManager.put("FileChooser.saveDialogTitleText", translator.realisticTranslate(Translation.SPEICHERN)); UIManager.put("OptionPane.background", ApplicationColors.getWhite());
问题背景图片绘制代码
public void paintComponent(Graphics g) { super.paintComponent(g); if (ApplicationImages.getImage() != null) { float factorWidth = getParent().getWidth() / 1280F; float factorHeight = getParent().getHeight() / 859F; if (factorWidth < factorHeight) { int width = (int) (1280F * factorHeight); int x = getParent().getWidth() / 2 - width / 2; g.drawImage( ApplicationImages.getImage().getScaledInstance(width, getParent().getHeight(), BufferedImage.SCALE_SMOOTH), x, 0, this); } else { int height = (int) (859F * factorWidth); int y = getParent().getHeight() / 2 - height / 2; g.drawImage(ApplicationImages.getImage().getScaledInstance( getParent().getWidth(), height, BufferedImage.SCALE_SMOOTH), 0, y, this); } } }
内容的提问来源于stack exchange,提问作者dragoness
相关产品推荐
相关产品推荐

