在nimbus上更改DesktopIcon.width

我正在尝试更改JInternalFrame图标大小,我尝试使用javax.swing.UIManager.put("DesktopIcon.width", 300);更改L&F默认值javax.swing.UIManager.put("DesktopIcon.width", 300); 但它不适用于Nimbus,我也尝试用。更改DesktopIconUI

 javax.swing.UIManager.put("DesktopIconUI", javax.swing.plaf.basic.BasicDesktopIconUI.class.getName()); 

但是一旦我最小化JInternalFrame它就消失了,任何消化?

您可以覆盖DefaultDesktopManagergetBoundsForIconOf(JInternalFrame)方法:

 import java.awt.*; import javax.swing.*; public class DesktopIconWidthTest { public JComponent makeUI() { JDesktopPane desktop = new JDesktopPane(); desktop.setDesktopManager(new DefaultDesktopManager() { @Override protected Rectangle getBoundsForIconOf(JInternalFrame f) { Rectangle r = super.getBoundsForIconOf(f); //System.out.println(r); r.width = 300; return r; } }); desktop.add(createFrame(0)); desktop.add(createFrame(1)); return desktop; } private JInternalFrame createFrame(int i) { JInternalFrame f = new JInternalFrame("title" + i, true, true, true, true); //TEST: //f.setDesktopIcon(new JInternalFrame.JDesktopIcon(f) { // @Override public Dimension getPreferredSize() { // Dimension d = super.getPreferredSize(); // d.width = 300; // return d; // } // @Override public Rectangle getBounds() { // Rectangle r = super.getBounds(); // r.width = 300; // return r; // } //}); f.setSize(300, 200); f.setVisible(true); f.setLocation(10 + 60 * i, 10 + 40 * i); return f; } public static void main(String... args) { EventQueue.invokeLater(() -> { try { for (UIManager.LookAndFeelInfo laf: UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(laf.getName())) { UIManager.setLookAndFeel(laf.getClassName()); break; } } //UIManager.put("DesktopIcon.width", 500); } catch (Exception e) { e.printStackTrace(); } JFrame f = new JFrame(); f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); f.getContentPane().add(new DesktopIconWidthTest().makeUI()); f.setSize(640, 480); f.setLocationRelativeTo(null); f.setVisible(true); }); } } 

  • 另一种选择是使用JInternalFrame.JDesktopIcon
    • 更改最小化的JInternalFrameFont
 import java.awt.*; import javax.swing.*; //import javax.swing.plaf.basic.*; import javax.swing.plaf.synth.*; public class DesktopIconWidthTest2 { public JComponent makeUI() { JDesktopPane desktop = new JDesktopPane(); desktop.add(createFrame(0)); desktop.add(createFrame(1)); desktop.add(createFrame(2)); return desktop; } private JInternalFrame createFrame(int i) { JInternalFrame f = new JInternalFrame("title: " + i, true, true, true, true); f.setDesktopIcon(new JInternalFrame.JDesktopIcon(f) { @Override public Dimension getPreferredSize() { return new Dimension(300, 40); } @Override public void updateUI() { super.updateUI(); if (getUI() instanceof SynthDesktopIconUI) { //NimbusLookAndFeel: setUI(new SynthDesktopIconUI() { @Override protected void installComponents() { super.installComponents(); iconPane.setFont(getFont()); } }); } } //BasicLookAndFeel work fine @Override public Font getFont() { Font font = UIManager.getFont("InternalFrame.titleFont"); return font.deriveFont(30f); } }); //TEST: //Container c = ((BasicInternalFrameUI) f.getUI()).getNorthPane(); //c.setFont(new Font("Monospaced", Font.BOLD, 50)); f.setSize(300, 200); f.setVisible(true); f.setLocation(10 + 60 * i, 10 + 40 * i); return f; } public static void main(String... args) { EventQueue.invokeLater(() -> { try { for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(laf.getName())) { UIManager.setLookAndFeel(laf.getClassName()); break; } } //TEST: //UIManager.put("DesktopIcon.width", 500); //UIManager.put("InternalFrame.useTaskBar", Boolean.TRUE); //Font font = UIManager.getFont("InternalFrame.titleFont"); //UIManager.put("InternalFrame.titleFont", font.deriveFont(60f)); } catch (Exception e) { e.printStackTrace(); } JFrame f = new JFrame(); f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); f.getContentPane().add(new DesktopIconWidthTest2().makeUI()); f.setSize(640, 480); f.setLocationRelativeTo(null); f.setVisible(true); }); } }